In: Computer Science
Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should create an array of the superclass (Employee10A) to hold the subclass (HourlyEmployee10A, SalariedEmployee10A, & CommissionEmployee10A) objects, then load the array with the objects you create. Create one object of each subclass. The three subclasses inherited from the abstract superclass print the results using the overridden abstract method.
Below is the source code for the driver class:
public class EmployeeTest10A {
public static void main(String[] args) {
//creating scanner object to read input
Scanner sc = new Scanner(System.in);
//declaring and initializing variables
String firstName, lastName;
int answer, hrsWorked, otHrs, soldItems;
double otPay = 0;
double hrlyRate, regularPay, wklyPaycheck, bonus, itemCost;
//user input to begin or exit the program
System.out.println("Enter any number to continue or enter a -1 to
exit "
+ "the program.");
answer = sc.nextInt();
//while loop
while(answer != -1) {
//Hourly Employee details
System.out.println("Please provide information for Hourly
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's hourly rate of
pay:");
hrlyRate = sc.nextDouble();
System.out.println("Enter the number of hours worked by the
employee:");
hrsWorked = sc.nextInt();
if (hrsWorked > 40) {
System.out.println("Enter the number of overtime hours worked
"
+ "by the employee:");
otHrs = sc.nextInt();
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
}
else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
//creating object
HourlyEmployee10A output = new HourlyEmployee10A(firstName,
lastName,
hrsWorked, hrlyRate, regularPay, otPay);
//calling methods from "HourlyEmployee9C" to calculate weekly
paycheck
output.WklyPaycheck();
wklyPaycheck = output.getWklyPaycheck();
//printing results
System.out.println(output);
//Salary Employee details
System.out.println("\nPlease provide information for Salaried
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's yearly salary:");
regularPay = sc.nextDouble();
System.out.println("Enter the employee's bonus percent:");
bonus = sc.nextDouble();
//creating object
SalariedEmployee10A salary = new SalariedEmployee10A(firstName,
lastName,
regularPay, bonus);
//calling methods from "SalariedEmployee9C" to calculate weekly
paycheck
salary.WeeklyPay();
//printing results
System.out.println(salary);
//Commission Employee details
System.out.println("\nPlease provide information for Commission
Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the number of items sold by the
employee:");
soldItems = sc.nextInt();
System.out.println("Enter the price of each item:");
itemCost = sc.nextDouble();
//creating object
CommissionEmployee10A commission = new
CommissionEmployee10A(firstName,
lastName, soldItems, itemCost);
//calling methods from "CommissionEmployee9C" to calculate weekly
paycheck
commission.WeeklyPay();
//printing results
System.out.println(commission);
//user input to continue or exit the program
System.out.println("\nEnter any number to continue or enter a -1 to
"
+ "exit the program and calculate the total number of
paychecks.");
answer = sc.nextInt();
}
//printing total number of paychecks calculated
System.out.println("\nTotal number of paychecks calculated: " +
Employee10A.counter);
}
}
Source code for the abstract superclass:
abstract public class Employee10A {
//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 Employee10A(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();
}
Source code for abstract subclass HourlyEmployee10A:
abstract public class HourlyEmployee10A extends Employee10A
{
//declaring instance variables
private int hrsWorked;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
//constructor to set instance variables
public HourlyEmployee10A(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
"Employee10A" toString method
@Override
public String toString() {
return (super.toString() + " The employee is a Hourly Employee and
its "
+ "weekly paycheck amount is $" + wklyPaycheck + ".");
}
@Override
public void payPrint() {
WklyPaycheck();
System.out.println(toString());
}
}
Source code for abstract subclass SalariedEmployee10A:
abstract public class SalariedEmployee10A extends Employee10A {
//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 SalariedEmployee10A(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
"Employee10A" toString method
@Override
public String toString() {
return super.toString() + " The employee is a Salaried Employee and
its "
+ "weekly paycheck amount is $" + Math.round(wklyPay) + ".";
}
@Override
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
Source code for abstract subclass CommissionEmployee10A:
abstract public class CommissionEmployee10A extends Employee10A {
//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 CommissionEmployee10A(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
"Employee10A" toString method
@Override
public String toString() {
return super.toString() + " The employee is a Commission Employee
and its "
+ "weekly paycheck amount is $" + wklyPay + ".";
}
@Override
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
Your code is almost correct. Only Employee10A class should be abstract. All other class shouldn't be as we can't create objects of abstract classes. Once we make these changes, we can store newly created objects of subclasses into array of type Employee10A (superclass). I have made these changes in your code. If you need any other help, add a comment. If you like this answer consider pressing like button.
Employee10A.java
public abstract class Employee10A {
// declaring & initializing static int variable to keep running total of the number of paychecks
// calculated
static int counter = 0;
// declaring instance variables
private String firstName;
private String lastName;
// constructor to set instance variables
public Employee10A(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();
public abstract void WeeklyPay();
}
HourlyEmployee10A.java
public class HourlyEmployee10A extends Employee10A {
// declaring instance variables
private int hrsWorked;
private double hrlyRate, regularPay, otPay, wklyPaycheck;
// constructor to set instance variables
public HourlyEmployee10A(
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++;
}
@Override
public void WeeklyPay() {
wklyPaycheck = regularPay + otPay;
}
// toString method that prints out the results and overrides the "Employee10A" toString method
@Override
public String toString() {
return (super.toString()
+ " The employee is a Hourly Employee and its "
+ "weekly paycheck amount is $"
+ wklyPaycheck
+ ".");
}
@Override
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
SalariedEmployee10A.java
public class SalariedEmployee10A extends Employee10A {
//constant variable representing the number of weeks in a year
private final static double weeks = 52;
//declaring instance variables
private double yrlySalary;
private double bonusPercent;
private double wklyPay;
//constructor to set instance variables
public SalariedEmployee10A(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 "Employee10A" toString method
@Override
public String toString() {
return super.toString() + " The employee is a Salaried Employee and its "
+ "weekly paycheck amount is $" + Math.round(wklyPay) + ".";
}
@Override
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
CommissionEmployee10A.java
public class CommissionEmployee10A extends Employee10A {
// constant variable representing the base pay for the commission employee
private static final double baseComm = 200;
// declaring instance variables
private int soldItems;
private double itemCost, wklyPay;
// constructor to set instance variables
public CommissionEmployee10A(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 "Employee10A" toString method
@Override
public String toString() {
return super.toString()
+ " The employee is a Commission Employee and its "
+ "weekly paycheck amount is $"
+ wklyPay
+ ".";
}
@Override
public void payPrint() {
WeeklyPay();
System.out.println(toString());
}
}
EmployeeTest10A.java
import java.util.Scanner;
public class EmployeeTest10A {
public static void main(String[] args) {
// creating scanner object to read input
Scanner sc = new Scanner(System.in);
// declaring and initializing variables
String firstName, lastName;
int answer, hrsWorked, otHrs, soldItems;
double otPay = 0;
double hrlyRate, regularPay, wklyPaycheck, bonus, itemCost;
// create array of type Employee10A, but store objects of its subclasses
Employee10A[] employees = new Employee10A[3];
// user input to begin or exit the program
System.out.println("Enter any number to continue or enter a -1 to exit " + "the program.");
answer = sc.nextInt();
// while loop
while (answer != -1) {
// Hourly Employee details
System.out.println("Please provide information for Hourly Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's hourly rate of pay:");
hrlyRate = sc.nextDouble();
System.out.println("Enter the number of hours worked by the employee:");
hrsWorked = sc.nextInt();
if (hrsWorked > 40) {
System.out.println("Enter the number of overtime hours worked " + "by the employee:");
otHrs = sc.nextInt();
otPay = ((hrlyRate * 1.5) * otHrs);
regularPay = hrlyRate * 40;
} else {
otPay = 0;
regularPay = hrlyRate * hrsWorked;
}
// creating object and storing in Employee10A type array
employees[0] =
new HourlyEmployee10A(firstName, lastName, hrsWorked, hrlyRate, regularPay, otPay);
// calling methods from "HourlyEmployee9C" to calculate weekly paycheck
employees[0].WeeklyPay();
// printing results
System.out.println(employees[0]);
// Salary Employee details
System.out.println("\nPlease provide information for Salaried Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the employee's yearly salary:");
regularPay = sc.nextDouble();
System.out.println("Enter the employee's bonus percent:");
bonus = sc.nextDouble();
// creating object
employees[1] = new SalariedEmployee10A(firstName, lastName, regularPay, bonus);
// calling methods from "SalariedEmployee9C" to calculate weekly paycheck
employees[1].WeeklyPay();
// printing results
System.out.println(employees[1]);
// Commission Employee details
System.out.println("\nPlease provide information for Commission Employee.");
System.out.println("Enter the employee's first name:");
firstName = sc.next();
System.out.println("Enter the employee's last name:");
lastName = sc.next();
System.out.println("Enter the number of items sold by the employee:");
soldItems = sc.nextInt();
System.out.println("Enter the price of each item:");
itemCost = sc.nextDouble();
// creating object
employees[2] = new CommissionEmployee10A(firstName, lastName, soldItems, itemCost);
// calling methods from "CommissionEmployee9C" to calculate weekly paycheck
employees[2].WeeklyPay();
// printing results
System.out.println(employees[2]);
// user input to continue or exit the program
System.out.println(
"\nEnter any number to continue or enter a -1 to "
+ "exit the program and calculate the total number of paychecks.");
answer = sc.nextInt();
}
// printing total number of paychecks calculated
System.out.println("\nTotal number of paychecks calculated: " + Employee10A.counter);
}
}