Modify the Employee9C superclass so that is an abstract superclass with a constructor to set its variables (firstName and lastName). It should contain an abstract method called payPrint. Below is the source code for the Employee9C superclass:
public class Employee9C {
//declaring instance variables
private String firstName;
private String lastName;
//declaring & initializing static int variable to keep running
total of the number of paychecks calculated
static int counter = 0;
//constructor to set instance variables
public Employee9C(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//toString method that prints out the results
public String toString() {
return ("The employee's full name is " + firstName + " " + lastName
+ ".");
}
}
The three subclasses (hourly, salaried, and commission employee) should inherit the abstract class and use its constructor to instantiate an object and set those instance variables mentioned before (firstName and lastName). To make things easy, the implementation of payPrint can use the toString method. Below are the source code for the subclasses.
public class HourlyEmployee9C extends Employee9C {
//declaring instance variables
private int hrsWorked;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
//constructor to set instance variables
public HourlyEmployee9C(String firstName, String lastName, int
hrsWorked,
double hrlyRate, double regularPay, double otPay) {
//calling super class constructor
super (firstName, lastName);
this.hrsWorked = hrsWorked;
this.hrlyRate = hrlyRate;
this.regularPay = regularPay;
this.otPay = otPay;
counter++;
}
//method that calculates the weekly paycheck
public void WklyPaycheck() {
wklyPaycheck = regularPay + otPay;
}
//getter(method) that retrieves the "wklyPaycheck"
public double getWklyPaycheck() {
return wklyPaycheck;
}
//toString method that prints out the results and overrides the
"Employee9C" toString method
@Override
public String toString() {
return (super.toString() + " The employee is a Hourly Employee and
its "
+ "weekly paycheck amount is $" + wklyPaycheck + ".");
}
}
public class SalariedEmployee9C extends Employee9C {
//declaring instance variables
private double yrlySalary;
private double bonusPercent;
private double wklyPay;
//constant variable representing the number of weeks in a
year
private final static double weeks = 52;
//constructor to set instance variables
public SalariedEmployee9C(String firstName, String lastName, double
yrlySalary,
double bonusPercent) {
//calling super class constructor
super(firstName, lastName);
this.yrlySalary = yrlySalary;
this.bonusPercent = bonusPercent;
counter++;
}
//method that calculates the weekly pay
public void WeeklyPay() {
wklyPay = (yrlySalary * (1.0 + bonusPercent/100.0)) / weeks;
}
//toString method that prints out the results and overrides the
"Employee9C" toString method
public String toString() {
return super.toString() + " The employee is a Salaried Employee and
its "
+ "weekly paycheck amount is $" + Math.round(wklyPay) + ".";
}
}
public class CommissionEmployee9C extends Employee9C {
//declaring instance variables
private int soldItems;
private double itemCost, wklyPay;
//constant variable representing the base pay for the commission
employee
private static final double baseComm = 200;
//constructor to set instance variables
public CommissionEmployee9C(String firstName, String lastName, int
soldItems,
double itemCost) {
//calling super class constructor
super(firstName, lastName);
this.soldItems = soldItems;
this.itemCost = itemCost;
counter++;
}
//method that calculates the weekly pay
public void WeeklyPay() {
wklyPay = baseComm + ((10.0/100.0) * (double)soldItems *
itemCost);
}
//toString method that prints out the results and overrides the
"Employee9C" toString method
public String toString() {
return super.toString() + " The employee is a Commission Employee
and its "
+ "weekly paycheck amount is $" + wklyPay + ".";
}
}
Here is the solution for the above assignment.
Code :
File name : Employee9C.java
abstract public class Employee9C {
//declaring instance variables
private String firstName;
private String lastName;
//declaring & initializing static int variable to keep running total of the number of paychecks calculated
static int counter = 0;
//constructor to set instance variables
public Employee9C(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//toString method that prints out the results
public String toString() {
return ("The employee's full name is " + firstName + " " + lastName + ".");
}
abstract void payPrint();
}
Filename: CommissionEmployee9C.java
Code :
public class CommissionEmployee9C extends Employee9C {
//declaring instance variables
private int soldItems;
private double itemCost, wklyPay;
//constant variable representing the base pay for the commission employee
private static final double baseComm = 200;
//constructor to set instance variables
public CommissionEmployee9C(String firstName, String lastName, int soldItems,
double itemCost) {
//calling super class constructor
super(firstName, lastName);
this.soldItems = soldItems;
this.itemCost = itemCost;
counter++;
}
//method that calculates the weekly pay
public void WeeklyPay() {
wklyPay = baseComm + ((10.0/100.0) * (double)soldItems * itemCost);
}
//toString method that prints out the results and overrides the "Employee9C" toString method
public String toString() {
return super.toString() + " The employee is a Commission Employee and its "
+ "weekly paycheck amount is $" + wklyPay + ".";
}
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
Filename : SalariedEmployee9C.java
Code:
public class SalariedEmployee9C extends Employee9C {
//declaring instance variables
private double yrlySalary;
private double bonusPercent;
private double wklyPay;
//constant variable representing the number of weeks in a year
private final static double weeks = 52;
//constructor to set instance variables
public SalariedEmployee9C(String firstName, String lastName, double yrlySalary,
double bonusPercent) {
//calling super class constructor
super(firstName, lastName);
this.yrlySalary = yrlySalary;
this.bonusPercent = bonusPercent;
counter++;
}
//method that calculates the weekly pay
public void WeeklyPay() {
wklyPay = (yrlySalary * (1.0 + bonusPercent/100.0)) / weeks;
}
//toString method that prints out the results and overrides the "Employee9C" toString method
public String toString() {
return super.toString() + " The employee is a Salaried Employee and its "
+ "weekly paycheck amount is $" + Math.round(wklyPay) + ".";
}
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
Also I created a driver class which tests the functionlaity of the program.
Additionally, I have added comments in the driver class for your better understanding.
Filename: DriverClass.java
Code:
public class DriverClass {
public static void main(String[] args) {
// Here observe that reference type is Employee9C and it is refereing to object of type CommissionEmployee9C
Employee9C commEmp = new CommissionEmployee9C("Mark", "Henry", 10, 30);
commEmp.payPrint();
// Here observe that reference type is Employee9C and it is refereing to object of type SalariedEmployee9C
Employee9C salariedEmp = new SalariedEmployee9C("Issac", "Newton", 500000, 10);
salariedEmp.payPrint();
// Here observe that reference type is Employee9C and it is refereing to object of type HourlyEmployee9C
Employee9C hourlyEmp = new HourlyEmployee9C("Alan", "Turing", 12, 10, 5, 4);
hourlyEmp.payPrint();
}
}
Output Screenshot
Thank You!
Get Answers For Free
Most questions answered within 1 hours.