Program 1 (15 points): Work programming exercise 11.2, page 445. Please follow t
ID: 3885924 • Letter: P
Question
Program 1 (15 points): Work programming exercise 11.2, page 445. Please follow the specifications given in the problem statement. Write a separate test program to create and test the objects specified in the problem statement.
11.2 (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. Use the MyDate class defined in Programming Exercise 10.14 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. Draw the UML diagram for the classes and implement them. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.
Explanation / Answer
public class Person
{
protected String name;
protected String address;
protected String MobileNumber;
protected String email;
public Person(String name)
{
this.name = name;
}
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 getMobileNumber()
{
return MobileNumber;
}
public void setMobileNumber(String MobileNumber)
{
this.MobileNumber = MobileNumber;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
@Override
public String toString()
{
return "Name: " + getName() + " Class: " + this.getClass().getName();
}
}
public class Student extends Person
{
public static final String FRESHMAN = "Freshman";
public static final String SOPHOMORE = "Sophomore";
public static final String JUNIOR = "Junior";
public static final String SENIOR = "Senior";
protected String status;
public Student(String name)
{
super(name);
}
public Student(String name, String status)
{
super(name);
this.status = status;
}
@Override
public String toString()
{
return "Name: " + getName() + " Class: " + this.getClass().getName();
}
}
public class Employee extends Person
{
protected double salary;
protected String office;
protected MyDate dateHired;
public Employee(String name)
{
this(name, 0, "none", new MyDate());
}
public Employee(String name, double salary, String office, MyDate dateHired) {
super(name);
this.salary = salary;
this.office = office;
this.dateHired = dateHired;
}
public double getSalary()
{
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getOffice()
{
return office;
}
public void setOffice(String office)
{
this.office = office;
}
public MyDate getDateHired()
{
return dateHired;
}
public void setDateHired(MyDate dateHired)
{
this.dateHired = dateHired;
}
@Override
public String toString()
{
return "Name: " + getName() + " Class: " + this.getClass().getName();
}
}
public class Faculty extends Employee
{
public static String LECTURER = "Lecturer";
public static String ASSISTANT_PROFESSOR = "Assistant Professor";
public static String ASSOCIATE_PROFESSOR = "Associate Professor";
public static String PROFESSOR = "Professor";
protected String officeHours;
protected String rank;
public Faculty(String name)
{
this(name, "9-5PM", "Employee");
}
public Faculty(String name, String officeHours, String rank)
{
super(name);
this.officeHours = officeHours;
this.rank = rank;
}
public String getOfficeHours()
{
return officeHours;
}
public void setOfficeHours(String officeHours)
{
this.officeHours = officeHours;
}
public String getRank()
{
return rank;
}
public void setRank(String rank) {
this.rank = rank;
}
@Override
public String toString()
{
return "Name: " + getName() + " Class: " + this.getClass().getName();
}
}
public class Staff extends Employee {
protected String title;
public Staff(String name) {
this(name, "none");
}
public Staff(String name, String title) {
super(name);
this.title = title;
}
public String getTitle()
{
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString()
{
return "Name: " + getName() + " Class: " + this.getClass().getName();
}
}
public class Exercise_02
{
public static void main(String[] args) {
Person person = new Person("person");
Student student = new Student("student");
Employee employee = new Employee("employee");
Faculty faculty = new Faculty("faculty");
Staff staff = new Staff("staff");
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());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.