Question

In: Computer Science

JAVA Design a class named Person and its two derived classes named Student and Employee. Make...

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.

Solutions

Expert Solution

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",

"[email protected]");

// 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


Related Solutions

Java Question Design a class named Person and its two subclasses named Student and Employee. Make...
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...
Design a class named Person and its two subclasses named Student and Employee. Make Faculty and...
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 e-mail 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. A faculty member has office hours and a rank. A staff member has a title. Override the toString method in each class to...
in java Design and implement a class named Person and its two subclasses named Student and...
in java Design and implement 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 (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI 8 to create an object for date hired. A faculty member has office hours and a rank. A staff...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for...
USE JAVA Develop the classes for the following requirements: 1. A class named Employee (general, for college) 2. A class named Instructor (more specific, for college) 3. A class named Staff (more specific, for college, HR officer, Marking staff) Tasks: 1. Figure out the relationships among the classes; 2. Determine the abstract class, and the child classes; 3. For the abstract class, determine at least one abstract method; 4. Each class should at least two data members and one extra...
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person...
Implement a superclass Person. Make two classes, Student and Instructor, that inherit from Person. A person has a name and a year of birth. A student has a major, and an instructor has a salary. Write the class declarations, the constructors, and the methods toString for all classes. Supply a test program that tests these classes and methods. Add a method Public static Measurable max(Measurable[ ] objects) to the Data class that returns the object with the largest measure. Implement...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone...
(Java) Design a class named Person with fields for holding a person’s name, address, and telephone number. Write one or more constructors and the appropriate mutator and accessor methods for the class’s fields. Next, design a class named Customer, which extends the Person class. The Customer class should have a field for a customer number and a boolean field indicating whether the customer wishes to be on a mailing list. Write one or more constructors and the appropriate mutator and...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can...
Base class: Polygon Derived classes: Rectangle, Triangle Make Square a derived class of Rectangle. There can be several ways to represent the shapes. For example, a shape can be represented by an array of side lengths counted in the clock-wise order. For example, {2, 4, 2, 4} for a rectangle of width 2 and height 4, and {4, 4, 4, 4} for a square. You need to design your representation. Each polygon should have a function area() that returns its...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
Design a class named Employee. The class should keep the following information in fields: ·         Employee...
Design a class named Employee. The class should keep the following information in fields: ·         Employee name ·         Employee number in the format XXX–L, where each X is a digit within the range 0–9 and the L is a letter within the range A–M. ·         Hire date Write one or more constructors and the appropriate accessor and mutator methods for the class. Next, write a class named ProductionWorker that inherits from the Employee class. The ProductionWorker class should have fields...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes...
JAVA A simple Class in a file called Account.java is given below. Create two Derived Classes Savings and Checking within their respective .java files. (modify display() as needed ) 1. Add New private String name (customer name) for both, add a New int taxID for Savings only. 2. Add equals() METHOD TO CHECK any 2 accounts Demonstrate with AccountDemo.java in which you do the following: 3. Create 1 Savings Account and 3 Checking Accounts, where 2 checkings are the same....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT