The objective of this programming assignment is to experience the use of inherit
ID: 1812962 • Letter: T
Question
The objective of this programming assignment is to experience the use of inheritance in Java and to see how polymorphism works with inheritance in Java.
The assignment involves writing three classes, plus a test class. The base class is an employee class which contains a couple of attributes common to all employees and a fundamental method to calculate pay. The two derived classes are a commissioned employee that adds payment of a sales commission as part of the pay calculation, and a union employee which adds overtime payment and union dues as part of the pay calculation. The test program will be structured to include a method which accepts a base class reference and demonstrates polymorphic behavior.
1. 2. The Union Employee inherits from the Employee class. A Union Employee contains a dues variable which holds the amount of dues a Union Employee has withheld from their paycheck each week. An explicit value constructor should be provided to set all three values from the base class along with the dues value. There should be a mutator method provided to set the dues variable. The base class weekly pay method should be overridden because a Union Employee gets 1.5 times the rate for any hours over 40 per week. This method should use the base class method to calculate pay for first 40 hours and then add the overtime amount. Also the weekly pay is reduced by the amount of dues withheld.
The3. Commission Employee inherits from the Employee class. A Commission Employee contains a commission rate and a sales amount variable which are used as part of the pay calculation. An explicit value constructor should be provided to set all 3 values of the base class along with the commission rate variable. There should be mutator methods for the commission rate and the sales amount. The base class weekly pay method should be overridden because the Commission Employee gets the base employee pay plus the commission rate times the sales amount. This method should use the base class weekly pay method to calculate the hourly part of the pay
1. 4. The test program needs to create a Union Employee object and a Commission Employee object. The test program must contain a display method which takes a base class Employee object reference along with the number of hours worked by the employee. The display method should use the base class method to get the employee name and department info and output that information. The display method should also use the base class method to get the weekly pay info for the employee object and display that information. The test program should pass the Union Employee object and the Commission Employee object to the display method along with the number of hours each employee has worked. It should test the payroll calculation for the number of hours worked to be less than 40, 40, and greater than 40 hours for each employee type. The output seen should demonstrate polymorphic behavior, that is the base class Employee reference to a Union Employee object elicits Union Employee pay calculations, and the base class Employee reference to a Commission Employee elicits Commission Employee payroll calculations.
Explanation / Answer
package pkg; public class Employee { public static String name1; public static String department; public static double hoursWorked; public static double payRate; public Employee(String name1, double hoursWorked, double payRate, String department) { super(); Employee.name1 = name1; Employee.department = department; Employee.hoursWorked = hoursWorked; Employee.payRate = payRate; // Calculations Begin Object product = payRate * hoursWorked; //Display Results System.out.println(name1);//print first name System.out.printf("total income this week is $%.2f ", product); } public Employee(String name12, double payRate2, double hoursWorked2, String department2, double uniondues) { } public String getName1() { return name1; } public void setName1(String name1) { Employee.name1 = name1; } public String getDepartment(){ return department; } public void setDepartment(String department){ Employee.department =department; } public double getHoursWorked() { return hoursWorked; } public void setHoursWorked(double hoursWorked) { Employee.hoursWorked = hoursWorked; } public double getPayRate() { return payRate; } public void setPayRate(double payRate) { Employee.payRate = payRate; } public void disp() { System.out.printf("Name=%d Payrate= %7.2d Department=%d", name1, payRate, department);} } ]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.