In: Computer Science
JAVA
Design a class named Person and its two derived classes named Student and Employee. Make Faculty and Staff derived classes of Employee. A person has a name, address, phone number, and e-mail address. A student has a class status (freshman, sophomore, junior, or senior). An employee has an office, salary, and datehired. Define a class named MyDate that contains the fields year, month, and day. A faculty member has office hours and a rank. A staff member has a title. Define an abstract toString function in the Person class and override it in each class to display the class name and the person’s name. Implement the classes. Write a test program that creates a Person, Student, Employee, Faculty, and Staff, and invokes their toString() functions.
package inheritance;
// Defines a class MyDate to store date information
class MyDate
{
// Instance variables to store date information
int year, month, day;
// Default constructor to assign default values
// to instance variable
MyDate()
{
year = month = day = 0;
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
MyDate(int y, int m, int d)
{
year = y;
month = m;
day = d;
}// End of parameterized constructor
// Overrides toString() method to return date
// in the form of year, month and day
public String toString()
{
return "\n Year: " + year + "\n Month: " + month +
"\n Day: " + day;
}// End of method
}// End of class MyDate
// Defines class to store Person information
class Person
{
// Instance variables to store person information
String name;
String address;
String phoneNumber;
String eMail;
// Default constructor to assign default values
// to instance variable
Person()
{
name = address = phoneNumber = eMail = "";
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
Person(String na, String add, String ph, String em)
{
name = na;
address = add;
phoneNumber = ph;
eMail = em;
}// End of parameterized constructor
// Overrides toString() method to return person information
public String toString()
{
return "\n\n Name: " + name + "\n Address: " + address +
"\n Phone Number: " + phoneNumber +
"\n E-Mail Address: " + eMail;
}// End of method
}// End of class Person
// Defines a class Student extended from Person super class
class Student extends Person
{
// Instance variable to store student information
String classStatus;
// Default constructor to assign default values
// to instance variable
Student()
{
super();
classStatus = "";
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
Student(String na, String add, String ph, String em, String cs)
{
super(na, add, ph, em);
classStatus = cs;
}// End of parameterized constructor
// Overrides toString() method to return student information
public String toString()
{
return super.toString() +
"\n\n Class Status: " + classStatus;
}// End of method
}// End of class Student
// Defines a class Employee extended from Person super class
class Employee extends Person
{
// Instance variables to store employee information
String office;
double salary;
MyDate dateHired;
// Default constructor to assign default values
// to instance variable
Employee()
{
super();
office = "";
salary = 0.0;
dateHired = null;
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
Employee(String na, String add, String ph, String em,
String of, double sa, MyDate dh)
{
super(na, add, ph, em);
office = of;
salary = sa;
dateHired = dh;
eMail = em;
}// End of parameterized constructor
// Overrides toString() method to return employee information
public String toString()
{
return super.toString() +
"\n\n Office: " + office + "\n Salary: " + salary +
"\n Date Hired: " + dateHired;
}// End of method
}// End of class Employee
// Defines a class Faculty extended from Employee super class
class Faculty extends Employee
{
// Instance variables to store faculty information
int officeHours;
int rank;
// Default constructor to assign default values
// to instance variable
Faculty()
{
super();
officeHours = rank = 0;
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
Faculty(String na, String add, String ph, String em,
String of, double sal, MyDate dh,
int oh, int ra)
{
super(na, add, ph, em, of, sal, dh);
officeHours = oh;
rank = ra;
}// End of parameterized constructor
// Overrides toString() method to return faculty information
public String toString()
{
return super.toString() +
"\n\n Office Hours: " + officeHours +
"\n Rank: " + rank;
}// End of method
}// End of class Faculty
// Defines class Staff extended from Employee super class
class Staff extends Employee
{
// Instance variable to store staff information
String title;
// Default constructor to assign default values
// to instance variable
Staff()
{
super();
title = "";
}// End of default constructor
// Parameterized constructor to assign parameter values
// to instance variable
Staff(String na, String add, String ph, String em,
String of, double sal, MyDate dh,
String ti)
{
super(na, add, ph, em, of, sal, dh);
title = ti;
}// End of parameterized constructor
// Overrides toString() method to return staff information
public String toString()
{
return super.toString() +
"\n\n Title: " + title;
}// End of method
}// End of class Staff
// Driver class PersonDemo definition
public class PersonDemo
{
// main method definition
public static void main(String ss[])
{
// Creates an object of class Person using
// parameterized constructor
Person per = new Person("Mohan", "Berhampur", "9040998887",
// Creates an object of class Student using
// parameterized constructor
Student stu = new Student("Ram", "Bhubaneswar",
"9948998334", "[email protected]", "Junior");
// Creates an object of class Employee using
// parameterized constructor
Employee emp = new Employee("Pyari", "Rourkela",
"9998778337", "[email protected]", "Development",
56000.78, new MyDate(2000, 1, 29));
// Creates an object of class Faculty using
// parameterized constructor
Faculty fac = new Faculty("Anil", "Rayagada",
"9001118322", "[email protected]", "Teaching",
22000, new MyDate(2008, 5, 12), 8, 2);
// Creates an object of class Staff using
// parameterized constructor
Staff sta = new Staff("Suresh", "Nayagada",
"9891358119", "[email protected]", "Account",
12000, new MyDate(2003, 2, 18), "Acc");
// Displays each object
System.out.print("\n\n*********** Person ***********");
System.out.println(per);
System.out.print("\n\n*********** Student ***********");
System.out.println(stu);
System.out.print("\n\n*********** Employee ***********");
System.out.println(emp);
System.out.print("\n\n*********** Faculty ***********");
System.out.println(fac);
System.out.print("\n\n*********** Staff ***********");
System.out.println(sta);
}// End of main method
}// End of driver class PersonDemo
Sample Output:
*********** Person ***********
Name: Mohan
Address: Berhampur
Phone Number: 9040998887
E-Mail Address: [email protected]
*********** Student ***********
Name: Ram
Address: Bhubaneswar
Phone Number: 9948998334
E-Mail Address: [email protected]
Class Status: Junior
*********** Employee ***********
Name: Pyari
Address: Rourkela
Phone Number: 9998778337
E-Mail Address: [email protected]
Office: Development
Salary: 56000.78
Date Hired:
Year: 2000
Month: 1
Day: 29
*********** Faculty ***********
Name: Anil
Address: Rayagada
Phone Number: 9001118322
E-Mail Address: [email protected]
Office: Teaching
Salary: 22000.0
Date Hired:
Year: 2008
Month: 5
Day: 12
Office Hours: 8
Rank: 2
*********** Staff ***********
Name: Suresh
Address: Nayagada
Phone Number: 9891358119
E-Mail Address: [email protected]
Office: Account
Salary: 12000.0
Date Hired:
Year: 2003
Month: 2
Day: 18
Title: Acc