Jave Program Pratice: (The Person, Student, Employee, Faculty, and Staff classes
ID: 3671752 • Letter: J
Question
Jave Program Pratice:
(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
/**
* @author Srinivas Palli
*
*/
public class Person {
public String name;
public String address;
public String phone;
public String email;
/**
* @param name
* @param address
* @param phone
* @param email
*/
public Person(String name, String address, String phone, String email) {
this.name = name;
this.address = address;
this.phone = phone;
this.email = email;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return this.getClass().getName() + " " + name;
}
}
/**
* @author Srinivas Palli
*
*/
public class Student extends Person {
public final String CLASS_STATUS;
/**
* @param name
* @param address
* @param phone
* @param email
* @param classStatus
*/
public Student(String name, String address, String phone, String email,
String classStatus) {
super(name, address, phone, email);
CLASS_STATUS = classStatus;
}
}
/**
* @author Srinivas Palli
*
*/
public class Employee extends Person {
public String office;
public double salary;
public String dateHired;
/**
* @param name
* @param address
* @param phone
* @param email
* @param dateHired
*/
public Employee(String name, String address, String phone, String email,
String dateHired) {
super(name, address, phone, email);
}
}
/**
* @author Srinivas Palli
*
*/
public class Faculty extends Employee {
public String officeHours;
public int rank;
/**
* @param name
* @param address
* @param phone
* @param email
* @param dateHired
*/
public Faculty(String name, String address, String phone, String email,
String dateHired) {
super(name, address, phone, email, dateHired);
}
}
/**
* @author Srinivas Palli
*
*/
public class Staff extends Employee {
public String title;
/**
* @param name
* @param address
* @param phone
* @param email
* @param dateHired
*/
public Staff(String name, String address, String phone, String email,
String dateHired) {
super(name, address, phone, email, dateHired);
}
}
/**
* @author Srinivas Palli
*
*/
public class TestPerson {
/**
* @param args
*/
public static void main(String[] args) {
Person person = new Person("Srinivas", "Location 1", "9088776221",
"srinivas.der@gmail.com");
Person student = new Student("Rajesh", "Location 2", "9088776221",
"srinivas.der@gmail.com", "junior");
Person employee = new Employee("Pavan", "Location 3", "9088776221",
"srinivas.der@gmail.com", "12/23/2015");
Person faculty = new Faculty("Kishore", "Location 4", "9088776221",
"srinivas.der@gmail.com", "11/12/2015");
Person staff = new Staff("Akshaya", "Location 4", "9088776221",
"srinivas.der@gmail.com", "10/10/2015");
// print using to string
System.out.println(person.toString() + " ");
System.out.println(student.toString() + " ");
System.out.println(employee.toString() + " ");
System.out.println(faculty.toString() + " ");
System.out.println(staff.toString() + " ");
}
}
OUTPUT:
Person
Srinivas
Student
Rajesh
Employee
Pavan
Faculty
Kishore
Staff
Akshaya
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.