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...
(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...
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....
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/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone...
JAVA/Netbeans •Design a class named Person with fields for holding a person’s name, address and phone number (all Strings) –Write a constructor that takes all the required information. –Write a constructor that only takes the name of the person and uses this to call the first constructor you wrote. –Implement the accessor and mutator methods. Make them final so subclasses cannot override them –Implement the toString() method •Design a class named Customer, which extends the Person class. It should have...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
In C# Create classes: Person, Student, Employee, Professor, Staff and Address ☐ Address class must have...
In C# Create classes: Person, Student, Employee, Professor, Staff and Address ☐ Address class must have suitable auto-implemented properties for Address 1, Address 2 and City. ☐ Person class must have suitable auto-implemented properties for Name, Residence (type Address) and email. ☐ Student and Employee must be subclasses of Person. ☐ Employee must be a super class for Professor and Staff. ☐ Employee class must have suitable auto-implemented properties for salary (between 2000 to 8000), and hire date. ☐ Professor...
Create a class named Employee and its child class named Salesperson. Save each class in its...
Create a class named Employee and its child class named Salesperson. Save each class in its own file. Name your class and source code file containing the main method homework.java. Make sure each class follows these specifications: 1. An employee has a name (String), employee id number (integer), hourly pay rate (double), a timesheet which holds the hours worked for the current week (double array) and email address (String). A salesperson also has a commission rate, which is a percentage...
In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT