In Java: ( The Person, Student, Employee, Faculty, and Staff classes ) Design a
ID: 3723913 • Letter: I
Question
In Java:
(The Person, Student, Employee, Faculty, and Staff classes) Design 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 (freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, salary, and 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. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.
Explanation / Answer
// IMPORTANT NOTE: Please save this program as MyClass.java before runing on your local machine.
class Person
{
public String name;
public String address;
public String phone;
public String email;
public Person(String name, String address, String phone, String email)
{
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
public String toString()
{
return ( "name =" + this.name + " className =" + this.getClass().getSimpleName() ); // getClass().getSimpleName() function gets the name of class of calling object.
}
}
enum Status {FRESHMAN,SOPHOMORE,JUNIOR,SENIOR}
class Student extends Person
{
public Status status;
public Student(String name, String address, String phone, String email, Status status)
{
super(name,address,phone,email);
this.status = status;
}
public String toString()
{
return super.toString();
}
}
class Employee extends Person
{
public String office;
public int salary;
public String hired_date;
public Employee(String name, String address, String phone, String email,String office,int salary, String hired_date)
{
super(name,address,phone,email);
this.office = office;
this.salary = salary;
this.hired_date = hired_date;
}
public String toString()
{
return super.toString();
}
}
class Faculty extends Employee
{
public String office_hours;
public int rank;
public Faculty(String name, String address, String phone, String email,String office,int salary, String hired_date,String office_hours, int rank)
{
super(name,address,phone,email,office,salary,hired_date);
this.office_hours = office_hours;
this.rank = rank;
}
public String toString()
{
return super.toString();
}
}
class Staff extends Employee
{
public String title;
public Staff(String name, String address, String phone, String email,String office,int salary, String hired_date,String title)
{
super(name,address,phone,email,office,salary,hired_date);
this.title = title;
}
public String toString()
{
return super.toString();
}
}
public class MyClass {
public static void main(String args[]) {
Person P = new Person("NIKHIL", "#14135 st5","9712345678","nikhil@abc.com");
Student S = new Student("YASH", "#14135 st5","9712345678","yash@abc.com",Status.SENIOR);
Employee E = new Employee("ADAM", "#14135 st5","9712345678","adam@abc.com","JSK",10000,"13 feb 2018");
Faculty F = new Faculty("NIKOL", "#14135 st5","9712345678","nikol@abc.com","JSK",10000,"13 feb 2018","9 to 5",5);
Staff staff = new Staff("LUCIFER", "#14135 st5","9712345678","lucifer@abc.com","JSK",10000,"13 feb 2018","lab trainer");
System.out.println(P.toString());
System.out.println(S.toString());
System.out.println(E.toString());
System.out.println(F.toString());
System.out.println(staff.toString());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.