1. Create a class called Person with the following properties: x The Person clas
ID: 3871801 • Letter: 1
Question
1. Create a class called Person with the following properties: x The Person class should have getters and setters for all properties other than id. (Use Eclipse to help you auto-generate these.) x Person class should also have a getter for id x In addition to these getters and setters, Person should have the following instance methods: x toString – returns full name, like "Edgar Allen Poe" x getEmailDomain – returns the domain portion of an email (after the @), as in "xyz.com" x getLast4SSN – returns the last four digits of the Social Security Number x Add some validation to age, email and ssn. You may have done this via the setters in the past. However, this time, let's add class (static) methods to validate these fields as best you can. Give some thought to what advantages this approach would have.
2. Create a driver program PersonApp to test the class x Prompt user for all Person properties except id. You can hardcode any id you like, but use the id as the HashMap key. x Accept any value for the name fields x Only accept valid values for age, email and ssn x Create a Person with the valid input
3. Notes on validation. Here is a list of validation checks you can perform. Some are easier to implement than others, and there are many ways to correctly validate these aspects of the fields. Implement as many as you can. Age entered by user x Must be an integer greater than 16 Email address string entered by user x String must contain a '@' and at least one '.' x A '.' must follow the '@' x String must only contain one '@' Social Security Number string entered by user x String must contain two '-' characters in the correct position x The length of the string must be 11 x String must contain only digits or numbers. If you encounter invalid data, log it using the logging API of Java. (Do not use System.out.println(); ) Property Data Type id Integer firstName String middleName String lastName String email String ssn String age Integer
4. Static variables x Keep track of the highest age entered for a Person in a static variable.
5. Create subclasses for Instructor and Student x Instructors should have a Department x Students should have a Major x Generate getters and setters.
6. Add the Person, the Student and the Instructor to a HashMap. x In the driver program, create a Student and an Instructor (you don't need to prompt for input) x Add these three instances to a HashMap
7. Output the following after creating each person: x Loop through the HashMap and display x The person's full name and what kind of Person are they x The person's email domain x The last 4 digits of the person's Social Security Number x Whether the person is the oldest in the collection x The major for the Student, the department for the Instructor
Explanation / Answer
PROGRAM CODE:
Person.java
package com;
public class Person {
private static int ID = 1000;
public static int HIGHEST_AGE = 0;
private int id;
private String firstName;
private String lastName;
private String SSN;
private int age;
private String email;
public Person(String firstname, String lastname, int age, String email, String ssn) {
id = ID++;
this.firstName = firstname;
this.lastName = lastname;
this.email = email;
this.SSN = ssn;
this.age = age;
if(age > HIGHEST_AGE)
HIGHEST_AGE = age;
}
public int getHIGHEST_AGE() {
return HIGHEST_AGE;
}
public static void setHIGHEST_AGE(int hIGHEST_AGE) {
HIGHEST_AGE = hIGHEST_AGE;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setSSN(String sSN) {
SSN = sSN;
}
public void setAge(int age) {
this.age = age;
if(age > HIGHEST_AGE)
HIGHEST_AGE = age;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSSN() {
return SSN;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
public int getLast4SSN()
{
return Integer.valueOf(SSN.substring(SSN.length()-4, SSN.length()));
}
public String getEmailDomain()
{
int index = email.indexOf('@');
return email.substring(index+1, email.length());
}
@Override
public String toString() {
// TODO Auto-generated method stub
return firstName + " " + lastName;
}
public static boolean isValidEmail(String email)
{
if(email.contains("@") && email.contains("."))
{
if(email.indexOf('.') < email.indexOf('@'))
{
String email1 = new String(email);
email1.replace('@', ' ');
if(email.contains("@"))
return false;
else return true;
}
}
return false;
}
public static boolean isValidAge(int age)
{
if(age > 16)
return true;
else return false;
}
public static boolean isValidSSN(String ssn)
{
if(ssn.length() == 11)
{
for(int i=0; i<ssn.length(); i++)
{
if(i==3 || i==6)
if(ssn.charAt(i) != '-')
return false;
else if(ssn.charAt(i) <= 0 && ssn.charAt(0) >9)
{
return false;
}
}
}
else return false;
return true;
}
}
Student.java
package com;
public class Student extends Person{
private String Major;
public Student(String firstname, String lastname, int age, String email, String ssn, String major) {
super(firstname, lastname, age, email, ssn);
this.Major = major;
}
public String getMajor() {
return Major;
}
public void setMajor(String major) {
Major = major;
}
}
Instructor.java
package com;
public class Instructor extends Person{
private String Department;
public Instructor(String firstname, String lastname, int age, String email, String ssn, String department) {
super(firstname, lastname, age, email, ssn);
this.Department = department;
}
public String getDepartment() {
return Department;
}
public void setDepartment(String department) {
Department = department;
}
}
PersonApp.java
package com;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class PersonApp {
public static void main(String[] args) {
HashMap<Integer, Person> map = new HashMap<>();
Person person1 = new Person("Ella", "Rogan", 23, "ella@edc.com", "234-78-2342");
map.put(person1.getId(), person1);
Student student = new Student("Martin", "Freeman", 19, "martin@stc.com", "193-22-4892", "English");
map.put(student.getId(), student);
Instructor instructor = new Instructor("Phillip", "Reager", 21, "phillip@qst.com", "922-48-1874", "Electronics");
map.put(instructor.getId(), instructor);
Set<Entry<Integer, Person>> keys = map.entrySet();
Iterator<Entry<Integer, Person>> itr = keys.iterator();
while(itr.hasNext())
{
System.out.println();
Entry<Integer, Person> entry = itr.next();
System.out.println(entry.getValue());
if(entry.getValue() instanceof Person)
System.out.println("Type: Person");
else if(entry.getValue() instanceof Student)
{
Student stud = (Student) entry.getValue();
System.out.println("Type: Student");
System.out.println("Major: " + stud.getMajor());
}
else if(entry.getValue() instanceof Instructor)
{
Instructor ins = (Instructor)entry.getValue();
System.out.println("Type: Instructor");
System.out.println("Instructor: " + ins.getDepartment());
}
System.out.println("Email domain: " + entry.getValue().getEmailDomain());
System.out.println("Last 4 digits of SSN: " + entry.getValue().getLast4SSN());
if(entry.getValue().getAge() == entry.getValue().getHIGHEST_AGE())
System.out.println(entry.getValue() + " has the highest age");
else System.out.println(entry.getValue() + " does not have the highest age");
}
}
}
OUTPUT:
Ella Rogan
Type: Person
Email domain: edc.com
Last 4 digits of SSN: 2342
Ella Rogan has the highest age
Martin Freeman
Type: Person
Email domain: stc.com
Last 4 digits of SSN: 4892
Martin Freeman does not have the highest age
Phillip Reager
Type: Person
Email domain: qst.com
Last 4 digits of SSN: 1874
Phillip Reager does not have the highest age
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.