JAVA The Person Student Employee Faculty and Staff classes D
*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, and address. A student has a class status(freshman, sophomore, junior, or senior). Define the status as a constant. An employee has an office, and salary. A faculty member has office hours. A staff member has a title.
Override the toString method in each class to display the class name and available information of the person.
•Draw the UML diagram for the classes.
• Implement the classes.
•Write a test program, in which instantiate a research team consisting of two students, one faculty, and one staff. Declare the team as an ArrayList, and use polymorphism concept to print out the information on the whole team.
Solution
//Person.java
public class Person
{
//instance variables of class Person
private String name;
private String address;
public Person(String name, String address)
{
this.name=name;
this.address=address;
}
//Returns strng representation of Person object
@Override
public String toString() {
return String.format(\"%-15s%-15s\", name, address);
}
}
--------------------------------------------
//Student.java
public class Student extends Person
{
private Enum status;
public Student(String name, String address, Enum status) {
//calling Person class constructor
super(name, address);
this.status=status;
}
//Returns strng representation of Student object
@Override
public String toString() {
return String.format(\"%s%s\", super.toString(),status);
}
}
--------------------------------------------
//Employee.java
public class Employee extends Person
{
private String office;
private double salary;
public Employee(String name, String address,
String office, double salary) {
//calling Person class constructor
super(name, address);
this.office=office;
this.salary=salary;
}
//Returns strng representation of Employee object
@Override
public String toString() {
return String.format(\"%s%-10s%5.2f\",super.toString(),office,salary );
}
}
--------------------------------------------
//Faculty.java
public class Faculty extends Employee
{
private int officeHours;
public Faculty(String name, String address,
String office, double salary, int officeHours) {
//calling Employee class constructor
super(name, address, office, salary);
this.officeHours=officeHours;
}
//Returns strng representation of Faculty object
@Override
public String toString() {
return String.format(\"%s%-10d\",super.toString(),officeHours);
}
}
--------------------------------------------
//Staff.java
public class Staff extends Employee
{
private String title;
public Staff(String name, String address,
String office, double salary, String title) {
//calling Employee class constructor
super(name, address, office, salary);
this.title=title;
}
//Returns strng representation of Staff object
@Override
public String toString() {
return String.format(\"%s%-10s\", super.toString(),title);
}
}
--------------------------------------------
//Test program of polymorphism
//Driver.java
import java.util.ArrayList;
public class Driver
{
public static void main(String[] args)
{
//Create an array list of the Person type
ArrayList<Person>team=new ArrayList<Person>();
//Add a student to team
team.add(new Student(\"Johnson\", \"Downtown, NY\", Status.junior));
//Add a employee to team
team.add(new Employee(\"Mark\", \"Chicago\", \"IT\", 40000));
//Add a faculty to team
team.add(new Faculty(\"Zing\", \"Chinatown\", \"Huwawi\", 150000, 8));
//Add a staff to team
team.add(new Staff(\"Charan\", \"New Delhi\", \"LAW-DEPT\", 20000, \"Major\"));
//print team persons
for (Person person : team) {
System.out.println(person);
}
}
}//end of the class
--------------------------------------------
Sample ouput:
Johnson Downtown, NY junior
Mark Chicago IT 40000.00
Zing Chinatown Huwawi 150000.008
Charan New Delhi LAW-DEPT 20000.00Major


