Question

In: Computer Science

Modify the program below so the driver class (Employee10A) uses a polymorphic approach, meaning it should...

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());
}
}

Solutions

Expert Solution

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);
  }
}

Related Solutions

Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks:...
Modify the GreenvilleRevenue program so that it uses the Contestant class and performs the following tasks: The program prompts the user for the number of contestants in this year’s competition; the number must be between 0 and 30. The program continues to prompt the user until a valid value is entered. The expected revenue is calculated and displayed. The revenue is $25 per contestant. For example if there were 3 contestants, the expected revenue would be displayed as: Revenue expected...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use deep copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working...
**** IN C++ **** 1) Modify the class pointerDataClass so the main function below is working properly. Use shallow copy. int main() { pointerDataClass list1(10); list1.insertAt(0, 50); list1.insertAt(4, 30); list1.insertAt(8, 60); cout<<"List1: " < list1.displayData(); cout<<"List 2: "< pointerDataClass list2(list1); list2.displayData(); list1.insertAt(4,100); cout<<"List1: (after insert 100 at indext 4) " < list1.displayData(); cout<<"List 2: "< list2.displayData(); return 0; } Code: #include using namespace std; class pointerDataClass { int maxSize; int length; int *p; public: pointerDataClass(int size); ~pointerDataClass(); void insertAt(int index,...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using...
Using C#, modify the codes below to do the following: Develop a polymorphic banking application using the Account hierarchy created in the codes below. Create an array of Account references to SavingsAccount and CheckingAccount objects. For each Account in the array, allow the user to specify an amount of money to withdraw from the Account using method Debit and an amount of money to deposit into the Account using method Credit. As you process each Account, determine its type. If...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right)...
Modify the partition.java program (Listing 7.2) so that the partitionIt() method always uses the highest-index (right) element as the pivot, rather than an arbitrary number. (This is similar to what happens in the quickSort1.java program in Listing 7.3.) Make sure your routine will work for arrays of three or fewer elements. To do so, you may need a few extra statements. // partition.java // demonstrates partitioning an array // to run this program: C>java PartitionApp //////////////////////////////////////////////////////////////// class ArrayPar { private...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the...
Modify the FeetInches class so that it overloads the following operators: <= >= != Demonstrate the class's capabilities in a simple program. this is what needs to be modified // Specification file for the FeetInches class #ifndef FEETINCHES_H #define FEETINCHES_H #include <iostream> using namespace std; class FeetInches; // Forward Declaration // Function Prototypes for Overloaded Stream Operators ostream &operator << (ostream &, const FeetInches &); istream &operator >> (istream &, FeetInches &); // The FeetInches class holds distances or measurements...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT