Question: The EmployeeDemo.java class creates instances of the PartTimeEmployee.java and FullTimeEmployee.java classes and prints them. If you run the EmployeeDemo.java code, you will see that the
The EmployeeDemo.java class creates instances of the PartTimeEmployee.java and FullTimeEmployee.java classes and prints them. If you run the EmployeeDemo.java code, you will see that the program runs fine; however, the PartTimeEmployee.java and FullTimeEmployee.java classes have a lot of duplicate code.
A) Use Inheritance to reduce code duplication, as follows:
1- Create a class called Employee.java. This class will contain the code that is in common between PartTimeEmployee.java and FullTimeEmployee.java classes.
2- Add a constructor method to the Employee class, which receives parameters for all instance variables of the Employee class.
3- Add a toString() method to the Empolyee.java class, which prints all the instance variables of the class.
4- Modify PartTimeEmployee.java and FullTimeEmployee.java classes such that they inherit and use the common code from the Employee.java rather than duplicating them.
5- Modify the toString() methods of the new PartTimeEmployee.java and FullTimeEmployee.java classes to use inheritance.
6- getSalary() method is a good candidate to be implemented as an abstract method.
a. Explain why this method should be implemented as an abstract method.
b. Implement this method as an abstract method.
7- Run the EmployeeDemo.java to verify your new code runs correctly. Specifically, make sure that the toString() methods of the new PartTimeEmployee.java and FullTimeEmployee.java classes are working correctly.
(Note: You should not change the EmployeeDemo.java class to answer part A)
B) Create a new class called EmployeeDemo2.java and in its main method implement the following:
1- Create two Employee variables called empPT and empFT.
2- Assign empPT to an instance of the new PartTimeEmployee class that you created in part A and assign empFT to an instance of the new FullTimeEmployee class that you also created in part A.
3- Print both empPT and empFT variables and specify whether the toString() method of the Employee class is called or the toString() methods of the PartTimeEmployee and FullTimeEmployee classes are called.
a. Explain why the above has happened.
b. In the Object Oriented paradigm, what this phenomena is called?
4- Call empPT.getHours() in EmployeeDemo2.java.
a. What is the result? Explain why it is happening.
b. If there is an issue, explain how it can be fixed. Employee demo
public class EmployeeDemo{
public static void main(String[] args){ PartTimeEmployee pe1 = new PartTimeEmployee("John", "Driver", 25.0); FullTimeEmployee fe1 = new FullTimeEmployee("Maggie", "Lab Manager", 8000.0); pe1.setHours(40); System.out.println(pe1); System.out.println(fe1); }
} Full time employee
public class FullTimeEmployee{
private String name; private String title; private double salary; public FullTimeEmployee(String n, String t, double s){ name = n; title = t; salary = s; } public String getName(){ return name; } public String getTitle(){ return title; } public double getSalary(){ return salary; } public String toString(){ return " Full-time Employee: " + "Name: " + name + " Title: " + title + " Salary: " + salary; }
} Part time employee
public class PartTimeEmployee{
private String name; private String title; private double rate; private int hours; public PartTimeEmployee(String n, String t, double r){ name = n; title = t; rate = r; hours = 0; } public String getName(){ return name; } public String getTitle(){ return title; } public void setHours(int h){ hours = h; } public int getHours(){ return hours; } public double getSalary(){ return hours * rate; } public String toString(){ return " Part-time Employee: " + "Name: " + name + " Title: " + title + " Salary: " + getSalary(); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
