In: Computer Science
Java Programming Language
Scenario: write a program that will prompt a user for 10 legendary people/characters. After the entries are complete the program will display all 10 characters information. Store these characters in an array.
Requirements:
Create a user-defined class with the fields below:
Create a main-source that will use the user-defined class and do the prompting.
Create get/set methods in your user-defined class and methods in the main-source.
Use an array to store the 10 characters
Create methods that will display the information for each character in the array
import java.util.*;
//Characters class
class Characters
{
//private data member declaration
String First;
String Last;
String Nickname;
String Role;
String Origin;
//dafault constructor
Characters()
{
}
//get method
public String getFirst()
{
return First;
}
public String getLast()
{
return Last;
}
public String getNickname()
{
return Nickname;
}
public String getRole()
{
return Role;
}
public String getOrigin()
{
return Origin;
}
//set method
public void setFirst(String first)
{
First = first;
}
public void setLast(String last)
{
Last = last;
}
public void setNickname(String nickname)
{
Nickname = nickname;
}
public void setRole(String role)
{
Role = role;
}
public void setOrigin(String origin)
{
Origin = origin;
}
public String toSttring()
{
return "\nFirst: "+First+"\nLast: "+Last+"\nNickname:
"+Nickname+"\nRole: "+Role+"\nOrigin: "+Origin;
}
}
public class Main
{
public static void main(String[] args)
{
//scanner declaration for input
Scanner input = new Scanner(System.in);
Characters c[] = new Characters[10];
for(int i=0; i<10; i++)
{
c[i] = new Characters();
}
//input characters info
for(int i=0; i<2; i++)
{
System.out.print("\n\nEnter the details of character
"+(i+1)+": ");
System.out.print("\nFirst: ");
c[i].setFirst(input.nextLine());
System.out.print("Last: ");
c[i].setLast(input.nextLine());
System.out.print("Nickname: ");
c[i].setNickname(input.nextLine());
System.out.print("Role: ");
c[i].setRole(input.nextLine());
System.out.print("Origin: ");
c[i].setOrigin(input.nextLine());
}
//display character info
for(int i=0; i<2; i++)
{
System.out.print("\n\nThe details of character
"+(i+1)+": ");
System.out.println(c[i].toSttring());
}
}
}
OUTPUT:
Enter the details of character 1:
First: Tom
Last: Jarry
Nickname: TJ
Role: Dancer
Origin: US
Enter the details of character 2:
First: Sam
Last: s singh Singh
Nickname: SS
Role: Writer
Origin: JD AUS
The details of character 1:
First: Tom
Last: Jarry
Nickname: TJ
Role: Dancer
Origin: US
The details of character 2:
First: Sam
Last: Singh
Nickname: SS
Role: Writer
Origin: AUS