In: Computer Science
Java Question
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 Date class 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. Write a test class that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() methods.
Note: Could you plz go through this code and let me know
if u need any changes in this.Thank You
=================================
// Person.java
public class Person {
// Declaring variables
private String name, address;
private String phoneNo;
private String email;
// Zero Argument Constructor
public Person() {
super();
}
public Person(String name, String address,String phoneNo,String email) {
super();
this.name = name;
this.address = address;
this.phoneNo=phoneNo;
this.email=email;
}
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 getPhoneNo() {
return phoneNo;
}
public void setPhoneNo(String phoneNo) {
this.phoneNo = phoneNo;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Name=" + name + ", Address=" + address + ", Phone No="
+ phoneNo + ", email=" + email;
}
}
=====================================
// Student.java
public class Student extends Person {
private final String classStatus;
public Student(String classStatus) {
this.classStatus = classStatus;
}
public Student(String name, String address,String phoneNo,String
email, String classStatus) {
super(name, address,phoneNo,email);
this.classStatus = classStatus;
}
public String toString() {
return super.toString() + " Satus= " + this.classStatus;
}
}
=====================================
// MyDate.java
public class MyDate {
private int year;
private int month;
private int day;
public MyDate(int year, int month, int day) {
super();
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
@Override
public String toString() {
return month+"/"+day+"/"+year;
}
}
=====================================
// Employee.java
public class Employee extends Person {
// Declaring variables
private String office;
private double salary;
private MyDate dateHired;
// Zero Argument Constructor
public Employee() {
super();
}
// Parameterized constructor.
public Employee(String name, String address, String phoneNo,String
email, String office, double salary,MyDate datehired) {
super(name, address, phoneNo, email);
this.office = office;
this.salary = salary;
this.dateHired=datehired;
}
// Setters and getters
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
// toString() method displays the contents of the object.
@Override
public String toString() {
return super.toString() + " office=" + office + ", salary=" +
salary;
}
}
=====================================
// Faculty.java
public class Faculty extends Employee {
// Declaring variables
private int office_hours;
private int rank;
// Zero Argument
Constructor
public Faculty() {
super();
}
// Parameterized constructor.
public Faculty(String name, String address,
String phoneNo, String email,
String office, double salary, MyDate
datehired,
int office_hours,int rank) {
super(name, address, phoneNo, email, office, salary, datehired);
this.office_hours =
office_hours;
this.rank=rank;
}
// Setters and getters
public int getOffice_hours() {
return office_hours;
}
/**
* @return the rank
*/
public int getRank() {
return rank;
}
/**
* @param rank the rank to
set
*/
public void setRank(int rank) {
this.rank =
rank;
}
public void setOffice_hours(int office_hours) {
this.office_hours = office_hours;
}
// toString() method displays the contents of the object.
@Override
public String toString()
{
return super.toString() + " office_hours=" + office_hours+", Rank="+rank;
}
}
=======================================
// Staff.java
public class Staff extends Employee {
// Declaring variables
private String title;
// Zero Argument Constructor
public Staff() {
super();
}
// Parameterized constructor.
public Staff(String name, String address,String phoneNo,String email, String office, double salary,MyDate datehired,
String title) {
super(name, address,phoneNo,email, office, salary,datehired);
this.title = title;
}
// Setters and getters
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
// toString() method displays the contents of the object.
@Override
public String toString() {
return super.toString() + " Title=" + title;
}
}
=====================================
// Test.java
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
ArrayList<Person> al = new ArrayList<Person>();
al.add(new
Student("James", "101 Church Street", "9848425879",
"[email protected]",
"Sophomore"));
al.add(new
Employee("Pattinson", "44 That Street",
"9848424345",
"[email protected]", "Accenture", 45000, new
MyDate(2014,
04,
23)));
al.add(new
Faculty("Williams", "500 Over There Street",
"9843567845",
"[email protected]", "Accenture", 50000, new
MyDate(2012, 4,
12),
8,3));
al.add(new
Staff("James", "867 Hay Street", "9845677667",
"[email protected]", "Microsoft", 80000, new
MyDate(2014, 05,
21),
"Janitor"));
for (int i = 0; i < al.size(); i++)
{
if (al.get(i) instanceof Faculty)
System.out.println("Faculty " + al.get(i).toString());
else if (al.get(i) instanceof Staff)
System.out.println("Staff " + al.get(i).toString());
else if (al.get(i) instanceof Student)
System.out.println("Student " + al.get(i).toString());
else if (al.get(i) instanceof Employee)
System.out.println("Employee " + al.get(i).toString());
}
}
}
=======================================
Output:
Student Name=James, Address=101 Church Street, Phone
No=9848425879, [email protected] Satus=
Sophomore
Employee Name=Pattinson, Address=44 That Street,
Phone No=9848424345, [email protected] office=Accenture,
salary=45000.0
Faculty Name=Williams, Address=500 Over There Street, Phone
No=9843567845, [email protected] office=Accenture,
salary=50000.0 office_hours=8, Rank=3
Staff Name=James, Address=867 Hay Street, Phone
No=9845677667, [email protected] office=Microsoft,
salary=80000.0 Title=Janitor
=====================Could you plz rate me well.Thank You