The objective of this programming assignment is to experience the use of inherit
ID: 3655571 • 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. The details of the three classes to be implemented are as follows: 1. An Employee contains a name, a department where the employee works, and an hourly rate of pay. An explicit value constructor should be provided to set all three values when an Employee object is created. There should be mutator methods to set the values of the department and the pay rate. There should be one accessor method which returns a string containing the name and the department in which the employee works. Finally, there should be a weekly pay method that takes an integer parameter for the number of hours worked and returns the weekly pay. If the number of hours worked is less than 40 hours, the pay is the number of hours times the rate. If the number of hours worked is 40 or more, the pay is 40 times the rate. 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. 3. The 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. 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
As we discussed ..I am giving the answer to your question..it is as per your requirment...Now check it out ..and please rate it..i have do quite a lot of work for this ....Hope i helped you...Let me know if you have any other doubts..
Employee Class
public class Employee {
private String name;
private String deptartment;
private double hourRatePay;
public Employee() {
super();
}
public Employee(String name, String deptartment, double hourRatePay) {
super();
this.name = name;
this.deptartment = deptartment;
this.hourRatePay = hourRatePay;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDeptartment() {
return deptartment;
}
public void setDeptartment(String deptartment) {
this.deptartment = deptartment;
}
public double getHourRatePay() {
return hourRatePay;
}
public void setHourRatePay(double hourRatePay) {
this.hourRatePay = hourRatePay;
}
public double weeklyPay(double noOfHoursWorked)
{
double payAmount=0;
if(noOfHoursWorked<40)
{
payAmount=noOfHoursWorked*hourRatePay;
}
else if(noOfHoursWorked>=40)
{
payAmount=40*hourRatePay;
}
return payAmount;
}
}
CommissionEmployee Class
public class CommissionEmployee extends Employee{
private double commissionRate;
private double salesAmount;
public CommissionEmployee(String name,String dept,double hourPayRate,double commissionRate) {
super();
this.commissionRate = commissionRate;
this.setName(name);
this.setDeptartment(dept);
this.setHourRatePay(hourPayRate);
}
public CommissionEmployee() {
super();
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
public double getSalesAmount() {
return salesAmount;
}
public void setSalesAmount(double salesAmount) {
this.salesAmount = salesAmount;
}
public double weeklyPay(double noOfHoursWorked)
{
System.out.println("super class hour method");
double payAmount=0;
if(noOfHoursWorked<40)
{
payAmount=super.weeklyPay(noOfHoursWorked)+ (commissionRate*salesAmount);
}
else if(noOfHoursWorked>=40)
{
payAmount=super.weeklyPay(noOfHoursWorked)+ (commissionRate*salesAmount);
}
return payAmount;
}
}
UNION Employee Class
public class UnionEmployee extends Employee {
private double dueAmount;
public UnionEmployee(String name,String dept,double hourPayRate,double dueAmount) {
super();
this.dueAmount = dueAmount;
this.setDeptartment(dept);
this.setName(name);
this.setHourRatePay(hourPayRate );
}
public UnionEmployee() {
super();
}
public double getDueAmount() {
return dueAmount;
}
public void setDueAmount(double dueAmount) {
this.dueAmount = dueAmount;
}
public double weeklyPay(double noOfHoursWorked)
{
double payAmount=0;
if(noOfHoursWorked<40)
{
payAmount=super.weeklyPay(noOfHoursWorked);
}
else if(noOfHoursWorked>=40)
{
double extrahour=noOfHoursWorked-40;
payAmount=super.weeklyPay(40)+(1.5*getHourRatePay()*extrahour)-dueAmount;
}
return payAmount;
}
}
TestController Class
import java.io.CharConversionException;
public class TestController {
/**
* @param args
*/
public static void display(Employee e,double noOfHoursWorked)
{
System.out.println("The Name Of Employee is :"+e.getName());
System.out.println("The Department Of Employee is :"+e.getDeptartment());
System.out.println("The weekly pay for the employee is :"+e.weeklyPay(noOfHoursWorked));
}
public static void display(UnionEmployee e,double noOfHoursWorked)
{
System.out.println("The Name Of Employee is :"+e.getName());
System.out.println("The Department Of Employee is :"+e.getDeptartment());
System.out.println("The weekly pay for the employee is :"+e.weeklyPay(noOfHoursWorked));
}
public static void display(CommissionEmployee e,double noOfHoursWorked)
{
System.out.println("The Name Of Employee is :"+e.getName());
System.out.println("The Department Of Employee is :"+e.getDeptartment());
System.out.println("The weekly pay for the employee is :"+e.weeklyPay(noOfHoursWorked));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee e=new Employee("Sumit","CSE",500);
Employee e1=new UnionEmployee("Sumit1","ECE",400,2000);
Employee e2=new CommissionEmployee("Sumit2","IT",200,40);
//display using base class
display(e,41.5);
//display using union Employee
display(e1,47);
//display using commission employee
display(e2,36);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.