Create a Person class conforming to the diagram above. A person is assigned an i
ID: 3791669 • Letter: C
Question
Create a Person class conforming to the diagram above. A person is assigned an immutable social security number at birth, and a name which can be changed later. People are born with an age of zero. The setAge method should set the person's age. A senior citizen is someone over 65 years old. Create a driver that asks the user, in a loop, for a person's name, ssn, and age. You should create an instance of your Person class and print the new person to the console. You should stop when the user enters the word "quit".Explanation / Answer
// Person.java
import java.util.Scanner;
public class Person
{
String name;
String ssn;
int age;
public void setName( String name )
{
this.name = name;
}
public void setSSN( String ssn )
{
this.ssn = ssn;
}
public void setAge( int age )
{
this.age = age;
}
public String getName()
{
return name;
}
public String getSSN()
{
return ssn;
}
public int getAge()
{
return age;
}
public String toString()
{
String s = " SSN: " + getSSN() + " Name: " + getName() + " Age: " + getAge() + " ";
return s;
}
public static void main ( String [] args )
{
Scanner sc = new Scanner(System.in);
Person p = new Person();
while (true)
{
System.out.print(" Enter name: ");
String name = sc.next();
if(name.equals("quit"))
break;
System.out.print("Enter ssn: ");
String ssn = sc.next();
System.out.print("Enter age: ");
int age = sc.nextInt();
p.setName(name);
p.setSSN(ssn);
p.setAge(age);
System.out.println(p.toString());
}
}
}
/*
output:
Enter name: peter
Enter ssn: 321
Enter age: 32
SSN: 321
Name: peter
Age: 32
Enter name: kallis
Enter ssn: 3456
Enter age: 21
SSN: 3456
Name: kallis
Age: 21
Enter name: quit
*/