Extra credit for my class. Have written the classes out, not sure if they are co
ID: 3679577 • Letter: E
Question
Extra credit for my class. Have written the classes out, not sure if they are correct. I also dont know how to do the test class and instantiate/arraylist/polymorphism part. Help appreciated!
Problem:
(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.
Code so far, may or may not be right, sorry for formatting:
public class Person {
public String name;
public String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name + " " + address;
}
}
public class Student extends Person {
public final String CLASS_STATUS;
public Student(String name, String address, String status) {
super(name, address);
CLASS_STATUS = status;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name + " " + address + " "
+ CLASS_STATUS;
}
}
public class Employee extends Person{
public String office;
public double salary;
public Employee(String name, String address, String eOffice, double eSalary){
super(name, address);
office = eOffice;
salary = eSalary;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name + " " + address + " "
+ "Office Address: " + office + " Salary: " + salary;
}
}
public class Staff extends Employee{
public String title;
public Staff(String name, String address, String eOffice, double eSalary,
String sTitle){
super(name, address, eOffice, eSalary);
title = sTitle;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name + " " + address + " "
+ "Office Address: " + office + " Salary: " + salary + " "
+ title;
}
}
public class Faculty extends Employee{
public String hours;
public Faculty(String name, String address, String eOffice, double eSalary,
String oHours){
super(name, address, eOffice, eSalary);
hours = oHours;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name + " " + address + " "
+ "Office Address: " + office + " Salary: " + salary + " "
+ "Hours: " + hours;
}
}
Explanation / Answer
Have six class files (five classes and a class containing test program).
Notice that override the toString() method only in the Person class since all the other classes inherit from it so the same overridden toString() method will be available to those child classes as well.
Class #1 - Person.java:
public 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;
}
@Override
public String toString() {
return this.getClass().getName() + " " + name;
}
}
Class #2 - Student.java:
public class Student extends Person {
// capitalize CLASS_STATUS variable is a typical Java convention
// for variables that have been declared as final (aka: constant)
public final String CLASS_STATUS;
public Student(String name, String address, String phone, String email, String classStatus) {
super(name, address, phone, email);
CLASS_STATUS = classStatus;
}
}
Class #3 - Employee.java:
public class Employee extends Person {
public String office;
public double salary;
public Employee(String name, String address, String phone, String email) {
super(name, address, phone, email);
}
}
Class #4 - Faculty.java:
public class Faculty extends Employee {
public String officeHours;
public int rank;
public Faculty(String name, String address, String phone, String email) {
super(name, address, phone, email);
}
}
Class #5 - Staff.java:
public class Staff extends Employee {
public String title;
public Staff(String name, String address, String phone, String email) {
super(name, address, phone, email);
}
}
Class #6 - TestPerson (test program):
public class TestPerson {
public static void main(String[] args) {
Person person = new Person("John Doe", "123 Somewhere", "415-555-1212", "johndoe@somewhere.com");
Person student = new Student("Mary Jane", "555 School Street", "650-555-1212", "mj@abc.com", "junior");
Person employee = new Employee("Tom Jones", "777 B Street", "408-888-9999", "tj@xyz.com");
Person faculty = new Faculty("Jill Johnson", "999 Park Ave", "925-222-3333", "jj@abcxyz.com");
Person staff = new Staff("Jack I. Box", "21 Jump Street", "707-212-1112", "jib@jack.com");
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 from above TestPerson test class:
Person
John Doe
Student
Mary Jane
Employee
Tom Jones
Faculty
Jill Johnson
Staff
Jack I. Box
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.