In: Computer Science
in java
Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to display the class name and the person’s name.Design and implement all 5classes. Write a test program that creates a Person,Student,Employee,Faculty, and Staff, and iinvokes their toString()methods
Answer:
Driver class has all the objects from the mentioned classes, all you need is to execute this class.
import java.time.LocalDate;
public class TestManyClasses {
public static void main(String[] args){
// Person object
Person person = new Person();
person.setName("Adams");
person.setAddress("Eire Island");
person.setEmailId("[email protected]");
person.setPhoneNumber("9809999999");
System.out.println("The person object: \n" + person);
// student object
Student student = new Student();
student.setName("Adams Paul");
student.setAddress("University Of Texas");
student.setEmailId("[email protected]");
student.setPhoneNumber("9809999999");
student.setClass(Student.Status.year_1);
System.out.println("Student object : \n" + student);
// employee object
Employee employee = new Employee();
employee.setName("Christina Kerr");
employee.setAddress("St Peter's Street, California");
employee.setOffice("Google Inc");
employee.setSalary(100000);
employee.setEmailId("[email protected]");
employee.setPhoneNumber("9991919191");
employee.setDateHired(LocalDate.of(2010, 2, 14));
System.out.println("The employee object : \n" + employee);
// The faculty object
Faculty faculty = new Faculty();
faculty.setName("Albert");
faculty.setOfficeHours(10);
faculty.setOffice("Dallas");
faculty.setRank(1);
faculty.setDateHired(LocalDate.of(2009, 5, 10));
faculty.setAddress("Peters Burg");
faculty.setPhoneNumber("9080898984");
faculty.setSalary(1006033);
System.out.println("The faculty object: \n" +faculty);
// The staff object
Staff staff = new Staff();
staff.setName("Christopher");
staff.setTitle("Asst Prof");
staff.setAddress("Washington DC");
staff.setDateHired(LocalDate.of(2012, 5, 19));
staff.setEmailId("[email protected]");
staff.setOffice("Main branch");
staff.setPhoneNumber("9898925923");
staff.setSalary(15000);
System.out.println("The staff object : \n" + staff);
}
}
----------------------------------------------------------------
public class Person {
// attributes
private String name;
private String address;
private String phoneNumber;
private String emailId;
// getters & setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Person{" +
" name='" + name + '\'' +
", address='" + address + '\'' +
", phoneNumber=" + phoneNumber +
", emailId='" + emailId + '\'' +
'}';
}
}
------------------------------------------------------------------
import java.time.LocalDate;
public class Employee extends Person{
// office, salary, and date hired
private String office;
private float salary;
private LocalDate dateHired;
// getter & setters
public String getOffice() {
// returns the office value
return office;
}
public void setOffice(String office) {
// setter for office
this.office = office;
}
public float getSalary() {
// getter for salary
return salary;
}
public void setSalary(float salary) {
// setter for salary
this.salary = salary;
}
public LocalDate getDateHired() {
// getter for date hired
return dateHired;
}
public void setDateHired(LocalDate dateHired) {
// setter for date hired
this.dateHired = dateHired;
}
@Override
public String toString() {
// Overridden method for toString, change accordingly
return "Employee{" +
" name='" + super.getName() + '\'' +
", address='" + super.getAddress() + '\'' +
", phoneNumber=" + super.getPhoneNumber() +
", emailId='" + super.getEmailId() + '\'' +
" office='" + office + '\'' +
", salary=" + salary +
", dateHired=" + dateHired +
'}';
}
}
---------------------------------------------------------------------------------
public class Faculty extends Employee{ // office hours and a rank private float officeHours; private int rank; public float getOffice_hours() { // getter for office hours return officeHours; } public void setOfficeHours(float officeHours) { // setter for office hours this.officeHours = officeHours; } public int getRank() { // getter for rank return rank; } public void setRank(int rank) { // setter for rank this.rank = rank; } @Override public String toString() { // change the overridden method accordingly return "Faculty{" + " name='" + super.getName() + '\'' + ", address='" + super.getAddress() + '\'' + ", phoneNumber=" + super.getPhoneNumber() + ", emailId='" + super.getEmailId() + '\'' + " office='" + super.getOffice() + '\'' + ", salary=" + super.getSalary() + ", dateHired=" + super.getDateHired() + " officeHours=" + officeHours + ", rank=" + rank + '}'; } }
---------------------------------------------------------------------------------
public class Staff extends
Employee{
private String title;
public String getTitle() {
// getter for title
return title;
}
public void setTitle(String title) {
// setter for title
this.title = title;
}
@Override
public String toString() {
// The toString() method overridden, change accordingly
return "Staff{" +
" name='" + super.getName() + '\'' +
", address='" + super.getAddress() + '\'' +
", phoneNumber=" + super.getPhoneNumber() +
", emailId='" + super.getEmailId() + '\'' +
" office='" + super.getOffice() + '\'' +
", salary=" + super.getSalary() +
", dateHired=" + super.getDateHired() +
" title='" + title + '\'' +
'}';
}
}
-------------------------------------------------------------------------------
public class Student extends
Person{
private Status studentClass;
enum Status { // decides on the class/status of the student
year_1,
year_2,
year_3,
year_4
}
public void setClass(Status status){
this.studentClass = status;
}
@Override
public String toString() {
//return "Student class : " + studentClass;
return "Student {" +
" name='" + super.getName() + '\'' +
", address='" + super.getAddress() + '\'' +
", phoneNumber=" + super.getPhoneNumber() +
", emailId='" + super.getEmailId() + '\'' +
" class/status : " + studentClass +
'}';
}
}
--------------------------------------------------------------
Output for your reference