Add comments before the beginning of the class. Include: Your name. The name of
ID: 3832703 • Letter: A
Question
Add comments before the beginning of the class. Include: Your name. The name of the Java class. The core concept for this assignment (found below). The date you created the file. Modify your Person class from Assignment 11: Change the encapsulation so that all instance variables and constants, and the method to calculate the users age can be accessed by the Person class and all sub classes. Create an ID class that inherits from the Person class. Do not add a Scanner object or take any input in this class.: Add a variable to hold a String value to be used to store the users id number. Add a variable to store if the user is a staff member or a student. Enforce encapsulation so both variables can only be accessed from within this class. Add getter and setter methods for both variables. Add a toString() method that will display the name, campus, age, id number, and if the person is a student or staff member. Do not add any variables or methods for name, campus, or age to the ID class. Instead use the variables and methods from the super (Person) class for those values when building the String. Format the String so it is easy to read. Include appropriate output messages. Create a test class. Name it Test. Add a main method. Create two objects of type ID. Do not create any objects of type Person. Ask the user for their name, campus, birth year, id number, and if they are a student or a staff member. Assign all input to the first ID object. Repeat for a second user assigning all input the second ID object. Call the toString() method for the first ID object. Call the toString() method for the second ID object.
Explanation / Answer
Below is your code: -
Person.java
import java.time.Year;
/*Name: -
* Class Name: - Person
* Date:-
* Concept: - To hold the details of the Person and way to access them
*/
public class Person {
// Protected access modifier so that they can be accessed in sub classes
// also.
protected String name;
protected String campus;
protected int yearOfBirth;
protected String getName() {
return name;
}
protected void setName(String name) {
this.name = name;
}
protected String getCampus() {
return campus;
}
protected void setCampus(String campus) {
this.campus = campus;
}
protected int getYearOfBirth() {
return yearOfBirth;
}
protected void setYearOfBirth(int yearOfBirth) {
this.yearOfBirth = yearOfBirth;
}
protected int getAge() {
int currentYear = Year.now().getValue();
return currentYear - yearOfBirth;// returns current age of user.
}
}
ID.java
/*Name: -
* Class Name: - ID
* Date:-
* Concept: - To hold the details of the Person's ID and way to access them
*/
public class ID extends Person {
private String id;
private boolean isStaff; // true if is staff, false if student
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public boolean isStaff() {
return isStaff;
}
public void setStaff(boolean isStaff) {
this.isStaff = isStaff;
}
public String toString() {
if (this.isStaff) {
return "Name : " + super.getName() + " Campus : " + super.getCampus() + " Age : " + super.getAge()
+ " ID number: " + this.id + " Is Staff or Student: Staff";
} else {
return "Name : " + super.getName() + " Campus : " + super.getCampus() + " Age : " + super.getAge()
+ " ID number: " + this.id + " Is Staff or Student: Student";
}
}
}
Test.java
import java.util.Scanner;
/*Name: -
* Class Name: - Test
* Date:-
* Concept: - To Test the classes
*/
public class Test {
public static void main(String[] args) {
ID id = new ID();
ID id1 = new ID();
Scanner sc = new Scanner(System.in);
System.out.println("Please enter name of First Person:");
id.setName(sc.nextLine());
System.out.println("Please enter from which campus the Person is:");
id.setCampus(sc.nextLine());
System.out.println("Please enter the birth year of First Person:");
id.setYearOfBirth(sc.nextInt());
sc.nextLine();
System.out.println("Please enter the ID number of First Person:");
id.setId(sc.nextLine());
System.out.println("Please Enter if the person is Student/Staff - (St - Student,Sf - Staff)");
String s = sc.nextLine();
while(!s.equals("St") && !s.equals("Sf")) {
System.out.println("Please Enter a valid option (St - Student,Sf - Staff)");
s = sc.nextLine();
}
if(s.equals("St")) {
id.setStaff(false);
} else {
id.setStaff(true);
}
//For second person
System.out.println("Please enter name of Second Person:");
id1.setName(sc.nextLine());
System.out.println("Please enter from which campus the Person is:");
id1.setCampus(sc.nextLine());
System.out.println("Please enter the birth year of Second Person:");
id1.setYearOfBirth(sc.nextInt());
sc.nextLine();
System.out.println("Please enter the ID number of Second Person:");
id1.setId(sc.nextLine());
System.out.println("Please Enter if the person is Student/Staff - (St - Student,Sf - Staff)");
s = sc.nextLine();
while(!s.equals("St") && !s.equals("Sf")) {
System.out.println("Please Enter a valid option (St - Student,Sf - Staff)");
s = sc.nextLine();
}
if(s.equals("St")) {
id1.setStaff(false);
} else {
id1.setStaff(true);
}
sc.close();
System.out.println("Details of First Person:");
System.out.println(id.toString());
System.out.println();
System.out.println("Details of Second Person:");
System.out.println(id1.toString());
}
}
Sample Run: -
Please enter name of First Person:
Jim Morrison
Please enter from which campus the Person is:
Hyderabad
Please enter the birth year of First Person:
2001
Please enter the ID number of First Person:
2017HT12012
Please Enter if the person is Student/Staff - (St - Student,Sf - Staff)
St
Please enter name of Second Person:
Dean Criss
Please enter from which campus the Person is:
Chennai
Please enter the birth year of Second Person:
1992
Please enter the ID number of Second Person:
2012HT12192
Please Enter if the person is Student/Staff - (St - Student,Sf - Staff)
Sf
Details of First Person:
Name : Jim Morrison
Campus : Hyderabad
Age : 16
ID number: 2017HT12012
Is Staff or Student: Student
Details of Second Person:
Name : Dean Criss
Campus : Chennai
Age : 25
ID number: 2012HT12192
Is Staff or Student: Staff
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.