Q:

(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

5

(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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (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.                                                      *
*********************************************************************************/
public class Exercise_11_02 {
	// Main method
	public static void main(String[] args) {
		// Create a Person, Student, Employee, Faculty, and Staff objects
		Person person = new Person("John", "12 Bell street", 
			"3473339999", "john12@aol.com");

		Student student = new Student("Mary", "100 Town Ave", "5149993333", 
			"mary100@aol.com", Student.FRESHMAN);

		Employee employee = new Employee("Mike", "34 West street", "6189999999",
			"mike80@aol.com", 910, 60000);

		Faculty faculty = new Faculty("Sue", "28 Well street", "4133333333",
			"sue28@aol.com", 101, 50000, "4pm to 6pm", "Professor");

		Staff staff = new Staff("Tom", "90 Country road", "2030000000",
			"tomcat@aol.com", 12, 65000, "Executive Assistant");

		// Invoke toString of Person, Student, Employee, Faculty and 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());
	}
}

Employee.java

/*******************************************************************
*                        Employee                                  *
*------------------------------------------------------------------*
* -office: int                                                     *
* -salary: double                                                  *
* -dateHired: MyDate                                               *
* +Employee(name: String, address: String, phone: String,          *
*  email: String, office: int, salary: double, dateHired: MyDate); *
* +getOffice(): int                                                *
* +getSalary(): double                                             *
* +getDateHired(): MyDate                                          *
* +setOffice(office: int): void                                    *
* +setSalary(salary: double): void                                 *
* +setDateHired(dateHired: MyDate): void                           *
* +toString():String                                               *
*******************************************************************/
// Implement Employee class
public class Employee
		extends Person {
	private int office;
	private double salary;
	private MyDate dateHired;

	/** Construct Employee object */
	public Employee(String name, String address, String phone, 
		String email, int office, double salary) {
		super(name, address, phone, email);
		this.office = office;
		this.salary = salary;
		this.dateHired = new MyDate();
	}

	/** Return office */
	public int getOffice() {
		return office;
	}

	/** Return salaray */
	public String getSalary() {
		return String.format("%.2f", salary);
	}

	/** Return date hired */
	public String getDateHired() {
		return dateHired.getMonth() + "/" + dateHired.getDay() 
				 + "/" + dateHired.getYear();
	}

	/** Set new office */
	public void setOffice(int office) {
		this.office = office;
	}

	/** Set new salary */
	public void setSalary(double salary) {
		this.salary = salary;
	}

	/** Set new dateHired */
	public void setDateHired() {
		dateHired = new MyDate();
	}

	/** Return a string discription of the class */
	public String toString() {
		return super.toString() + "\nOffice: " + office + 
				 "\nSalary: $" + getSalary() + "\nDate hired: " + getDateHired();
	}
}

Faculty.java 

/**********************************************************
*                      Faculty                            *
*---------------------------------------------------------*
* -officeHours: int                                       *
* -rank: String                                           *
* +Faculty(name: String, address: String, phone: String,  *
*  email: String, office: int, salary: double,            *
*  officeHours: int, rank: String)     *
* +getOfficeHours(): int                                  *
* +setOfficeHours(officeHours: int): void                 *
* +getRank(): String                                      *
* +setRank(rank: String): void                            *
* +toString(): String                                     *
**********************************************************/

// Implement Faculty class
public class Faculty 
		extends Employee {
	// Data fields
	private String officeHours;
	private String rank;

	// Constructors
	/** Construct a Faculty object with specified name, address, phone number,
	  * email address, office, salary, office hours and rank */
	public Faculty(String name, String address, String phone, String email, 
		int office, double salary, String officeHours, String rank) {
		super(name, address, phone, email, office, salary);
		this.officeHours = officeHours;
		this.rank = rank;
	}

	/** Return officeHours */
	public String getOfficeHours() {
		return officeHours;
	}

	/** Set new officeHours */
	public void setOfficeHours(String officeHours) {
		this.officeHours = officeHours;
	}

	/** Return rank */
	public String getRank() {
		return rank;
	}

	/** Set new rank */
	public void setRank(String rank) {
		this.rank = rank;
	}

	/** Return a string discription of the class */
	public String toString() {
		return super.toString() + "\nOffice hours: " + officeHours +
		"\nRank: " + rank;
	}
}

MyDate.java 

/*******************************************
*                MyDate                    *
*------------------------------------------*
* -year: int                               *
* -month: int                              *
* -day: int                                *
* +MyDate()                                *
* +MyDate(elapsedTime: long)               *
* +MyDate(year: int, month: int, day: int) *
* +getYear(): int                          *
* +getMonth(): int                         *
* +getDay(): int                           *
* +setDate(elapsedTime: long)              *
*******************************************/

import java.util.GregorianCalendar;

// Implement MyDate class
public class MyDate {
	// Data Fields
	private int year;
	private int month;
	private int day;

	/** Creates a MyDate object for the current date */
	MyDate() {
		GregorianCalendar calander = new GregorianCalendar();
		year = calander.get(GregorianCalendar.YEAR);
		month = calander.get(GregorianCalendar.MONTH);
		day = calander.get(GregorianCalendar.DAY_OF_MONTH);
	}

	/** Creates a MyDate object with a specified elapsed time
	*	 since midnight, January 1, 1970, in milliseconds */
	MyDate(long elapsedTime) {
		setDate(elapsedTime);
	}

	/** Creates a MyDate object with the 
	*   specified year, month, and day */
	MyDate(int year, int month, int day) {
		this.year = year;
		this.month = month;
		this.day = day;
	}

	/** Return year */
	public int getYear() {
		return year;
	}

	/** Return month */
	public String getMonth() {
		String m = String.valueOf(month + 1);
		return (month < 10 ? "0" + m : m);
	}

	/** Return day */
	public String getDay() {
		String d = String.valueOf(day);
		return (day < 10 ? "0" + d : d);
	}

	/** Sets a new date for the object using the elapsed time */
	public void setDate(long elapsedTime) {
		GregorianCalendar calander = new GregorianCalendar();
		calander.setTimeInMillis(elapsedTime);
		year = calander.get(GregorianCalendar.YEAR);
		month = calander.get(GregorianCalendar.MONTH);
		day = calander.get(GregorianCalendar.DAY_OF_MONTH);
	} 
}

Person.java 

/*****************************************
*                Person                  *
*----------------------------------------*
* -name: String                          *
* -address: String                       *
* -phone: String                         *
* -email: String                         *
* +Person()                              *
* +Person(name: String, address: String, *
*  phone: String, email: String)         *
* +getName(): String                     *
* +getAddress(): String                  *
* +getPhone(): String                    *
* +getEmail(): String                    *
* +setName(name: String ): void          *
* +setAddress(address: String): void     *
* +setPhone(phone: String): void         *
* +setEmail(email: String): void         *
* +toString(): String                    *
*****************************************/
// Implement Person class
public class Person {
	private String name;
	private String address;
	private String phone;
	private String email;

	/** Construct default Person object */
	public Person() {
		this("Unknown","Unknown","Unknown","Unknown");
	}

	/** Construct Person object with specified name, address, phone and email */
	public Person(String name, String address, String phone, String email) {
		this.name = name;
		this.address = address;
		this.phone = phone;
		this.email = email;
	} 

	/** Return name */
	public String getName() {
		return name;
	}

	/** Return address */
	public String getAddress() {
		return address;
	}

	/** Return phone */
	public String getPhone() {
		return phone;
	}

	/** Return email */
	public String getEmail() {
		return email;
	}

	/** Set new name */
	public void setName(String name) {
		this.name = name;
	}

	/** Set new address */
	public void setAddress(String address) {
		this.address = address;
	}

	/** Set new phone number */
	public void setPhone(String phone) {
		this.phone = phone;
	}

	/** Set new email */
	public void setEmail(String email) {
		this.email = email;
	}

	/** Return a string discription of the class */
	public String toString() {
		return "\nName: " + name + "\nAddress: " + address + 
				 "\nPhone number: " + phone + "\nEmail address: " + email;
	}
}

Staff.java 

/**********************************************************************
*                                Staff                                *
*---------------------------------------------------------------------*
* -title: String                                                      *
* +Staff(name: String, address: String, phone: String, email: String, *
*  office: int, salary: double, dateHired: MyDate, title: String)     *
* +getTitle(): String                                                 *
* +setTitle(title: String): void                                      *
* +toString(): String                                                 *
**********************************************************************/
// Implement Staff class
public class Staff
		extends Employee {
	// Data Fields
	private String title;

	// Constructors
	/** Construct a Staff object */
	public Staff(String name, String address, String phone,  
		String email, int office, double salary, String title) {
		super(name, address, phone, email, office, salary);
		this.title = title;
	}

	/** Return title */
	public String getTitle() {
		return title;
	}

	/** Set new title */
	public void setTitle(String title) {
		this.title = title;
	}

	/** Return a string discription of the class */
	public String toString() {
		return super.toString() + "\nTitle: " + title;
	}
}

Student.java

/*************************************************
*                   Student                      *
*------------------------------------------------*
* -status: String                                *
* ---------------                                *
* +Student(name: String, address: String,        *
*  phone: String, email: String, status: String) *
* +getStatus(): String                           *
* +setStatus(status: String): void               *
* +toString(): String                            *
*************************************************/
// Implement Student class
public class Student
		extends Person {
	private int status;
	public final static int FRESHMAN = 1;
	public final static int SOPHOMORE = 3;
	public final static int JUNIOR = 2;
	public final static int SENIOR = 4;

	public Student(String name, String address,  
		String phone, String email, int status) {
		super(name, address, phone, email);
		this.status = status;
	}

	/** Set new status */
	public void setStatus(int status) {
		this.status = status; 
	}

	/** Return status */
	public String getStatus() {
		switch (status) {
			case 1 : return "freshman"; 
			case 2 : return "sophomore"; 
			case 3 : return "junior"; 
			case 4 : return "senior";
			default : return "Unknown"; 
		}
	}

	/** Return a string discription of the class */
	public String toString() {
		return super.toString() + "\nStatus: " + getStatus();
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now