Question

In: Computer Science

Assignment 2 - Employee Hierarchy In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model...

Assignment 2 - Employee Hierarchy

In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object.

A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation:

  • CommissionCompensationModel - For Employees who are paid by commission, the CommissionCompensationModel class would contain grossSales and commissionRate instance variables, and would define an earnings method to return grossSales * commissionRate.
  • BasePlusCommissionCompensationModel - For Employees who are paid a base salary and commission, this subclass of CommissionCompensationModel would contain an instance variable of baseSalary and would define the earnings method to return super.earnings() + baseSalary.

Each of these classes would contain a toString() method that displays the Compensation Model information as illustrated in the sample output.

This approach is more flexible than our original hierarchy. For example, consider an Employee who gets promoted. With the approach described here, you can simply change that Employee's CompensationModel by assigning the composed CompensationModel reference an appropriate subclass object. With the CommissionEmployee - BasePlusCommissionEmployee hierarchy, you'd need to change the Employee's type by creating a new object of the appropriate class and moving data from the old object to the new one.

Implement the Employee class and CompensationModel hierarchy discussed in this exercise. In addition to the firstName, lastName, socialSecurityNumber and CommisionCompensationModel instance variables, class Employee should provide:

  • A constructor that receives three Strings and a CommissionCompensationModel to initialize the instance variables.
  • A set method that allows the client code to change an Employee's CompensationModel.
  • An earnings method that calls the CompensationModel's earning method and returns the result.
  • A toString() method that displays all the information about the Employee as illustrated in the sample output.

Your code in the subclasses should call methods in the super classes whenever possible to reduce the amount of code in the subclasses and utilize the code already developed in the super classes as in the code demonstrated in Figures 9.10 and 9.11 in the book.

Use the following code in your main function to test your classes, just copy and paste it into your main method:

        // Create the two employees with their compensation models.
       
        CommissionCompensationModel commissionModel = new CommissionCompensationModel(2000.00, 0.04);
        BasePlusCommissionCompensationModel basePlusCommissionModel = new BasePlusCommissionCompensationModel(2000.00, 0.05, 600.00);
       
        Employee employee1 = new Employee("John", "Smith", "111-11-1111", commissionModel);
        Employee employee2 = new Employee("Sue", "Jones", "222-22-2222", basePlusCommissionModel);
       
        System.out.printf("%s%n%s%n", employee1, employee2);
        System.out.printf("%s%s%s%s%s%8.2f%n%n", "Earnings for ", employee1.getFirstName(), " ", employee1.getLastName(), ": ", employee1.earnings());
       
        // Change the compensation model for the two employees.
       
        CommissionCompensationModel commissionModelNew = new CommissionCompensationModel(5000.00, 0.04);
        BasePlusCommissionCompensationModel basePlusCommissionModelNew = new BasePlusCommissionCompensationModel(4000.00, 0.05, 800.00);
       
        // Set the new compensation models for the employees.
        employee1.setCompensation(basePlusCommissionModelNew);
        employee2.setCompensation(commissionModelNew);
       
        // Print out the new information for the two employees.
        System.out.printf("%s%n%s%n", employee1, employee2);

The output from your program should look like the following:

run:
John Smith
Social Security Number: 111-11-1111
Commission Compensation with:
Gross Sales of: 2000.00
Commission Rate of: 0.04
Earnings:    80.00

Sue Jones

Social Security Number: 222-22-2222
Base Plus Commission Compensation with:
Gross Sales of: 2000.00
Commission Rate of: 0.05
Base Salary of:   600.00
Earnings:   700.00

Earnings for John Smith:    80.00

John Smith
Social Security Number: 111-11-1111
Base Plus Commission Compensation with:
Gross Sales of: 4000.00
Commission Rate of: 0.05
Base Salary of:   800.00
Earnings: 1000.00

Sue Jones

Social Security Number: 222-22-2222
Commission Compensation with:
Gross Sales of: 5000.00
Commission Rate of: 0.04
Earnings:   200.00

Solutions

Expert Solution

//TestFile.java


public class TestFile {
   public static void main(String[] args){
     
   CommissionCompensationModel commissionModel = new CommissionCompensationModel(2000.00, 0.04);
   BasePlusCommissionCompensationModel basePlusCommissionModel = new BasePlusCommissionCompensationModel(2000.00, 0.05, 600.00);

   Employee employee1 = new Employee("John", "Smith", "111-11-1111", commissionModel);
   Employee employee2 = new Employee("Sue", "Jones", "222-22-2222", basePlusCommissionModel);

   System.out.printf("%s%n%s%n", employee1, employee2);
   System.out.printf("%s%s%s%s%s%8.2f%n%n", "Earnings for ", employee1.getFirstName(), " ", employee1.getLastName(), ": ", employee1.earnings());

   // Change the compensation model for the two employees.

   CommissionCompensationModel commissionModelNew = new CommissionCompensationModel(5000.00, 0.04);
   BasePlusCommissionCompensationModel basePlusCommissionModelNew = new BasePlusCommissionCompensationModel(4000.00, 0.05, 800.00);

   // Set the new compensation models for the employees.
   employee1.setCompensation(basePlusCommissionModelNew);
   employee2.setCompensation(commissionModelNew);

   // Print out the new information for the two employees.
   System.out.printf("%s%n%s%n", employee1, employee2);
   }
}

//Employee.java


public class Employee {
   private String firstName;
   private String lastName;
   private String socialSecurityNumber;
   private CommissionCompensationModel compensation;
  
   //Parametried constructor
   public Employee(String firstName, String lastName, String socialSecurityNumber,
   CommissionCompensationModel commissionModel) {
   this.firstName = firstName;
   this.lastName = lastName;
   this.socialSecurityNumber = socialSecurityNumber;
   this.compensation = commissionModel;
   }
  
   //setter for Compensation
   public void setCompensation(CommissionCompensationModel compensation) {
   this.compensation = compensation;
   }
  
  
   //the getters
   public String getFirstName() {
   return firstName;
   }

   public String getLastName() {
   return lastName;
   }

   public String getSocialSecurityNumber() {
   return socialSecurityNumber;
   }

   public CommissionCompensationModel getCompensation() {
   return compensation;
   }
  
   //method: earnings()
   public double earnings(){
   return this.compensation.earnings();
   }
  
   //toString method
   @Override
   public String toString() {
   String str = "";
   str = str+firstName+" "+lastName;
   str = str+"\n"+"Social Security Number:"+socialSecurityNumber;
   str = str+ compensation.toString();
  
   return str;
   }
}

//BasePlusCommissionCompensationModel.java


public class BasePlusCommissionCompensationModel extends CommissionCompensationModel{
   private double baseSalary;
  
   //parametrized constructor
   public BasePlusCommissionCompensationModel(double grossSales,double commissionRate,
   double baseSalary){
   super(grossSales,commissionRate);
   this.baseSalary = baseSalary;
   }
  
   //earnings() method
   public double earnings(){
   return super.earnings() + baseSalary;
   }
  
   @Override
   public String toString() {
   String str = "\nBase Plus Commission Compensation with:";
   str = str + "\nGross Sales of: "+getGrossSales(); //inherited method
   str = str + "\nCommission Rate of: "+getCommissionRate();//inherited method
   str = str + "\nBase salary of: "+baseSalary;
   str = str + "\nEarnings: "+earnings();
  
   return str;
   }
}
//CommissionCompensationModel.java


public class CommissionCompensationModel {
   private double grossSales;
   private double commissionRate;
  
   //parametrized constructor
   public CommissionCompensationModel(double grossSales,double commissionRate){
   this.grossSales = grossSales;
   this.commissionRate = commissionRate;
   }
   //method: earnings
   public double earnings(){
   return grossSales*commissionRate;
   }
  
  
   //getters
   public double getGrossSales() {
   return grossSales;
   }
   public double getCommissionRate() {
   return commissionRate;
   }
   //toString() method
   @Override
   public String toString() {
   String str = "\nCommission Compensation with:";
   str = str + "\nGross Sales of: "+grossSales;
   str = str + "\nCommission Rate of: "+commissionRate;
   str = str + "\nEarnings: "+earnings();
  
   return str;
   }
}


Related Solutions

Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship...
Code in Java In Chapter 9, we created the CommissionEmployee-BasePlusCommissionEmployee inheritance hierarchy to model the relationship between two types of employees and how to calculate the earnings for each. Another way to look at the problem is that CommissionEmployees and BasePlusCommissionEmployees are each Employees and that each has a different CompensationModel object. A CompensationModel would provide an earnings method. Classes or subclasses of CompensationModel would contain the details of a particular Employee's compensation: CommissionCompensationModel - For Employees who are paid...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee Hierarchy that you created in Java in Programming Assignment 2 by adding an interface called Compensation with the following two methods: earnings() - receives no parameters and returns a double. raise(double percent) - receives one parameter which is the percentage of the raise and returns a void. Create the abstract class CompensationModel which implements the Compensation interface. Create the following classes as subclasses of...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee...
Assignment 3 - Enhanced Employee Hierarchy For this assignment, you are going to enhance the Employee Hierarchy that you created in Programming Assignment 2 by adding an interface called Compensation with the following two methods: earnings() - receives no parameters and returns a double. raise(double percent) - receives one parameter which is the percentage of the raise and returns a void. Create the abstract class CompensationModel which implements the Compensation interface. Create the following classes as subclasses of CompensationModel: SalariedCompensationModel...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker Classes). Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: ● The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. ● The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. ● The ProductionWorker class should throw an...
Chapter 9: Patterns of Inheritance 1. Describe the theory of blending hypothesis. Explain why it is...
Chapter 9: Patterns of Inheritance 1. Describe the theory of blending hypothesis. Explain why it is rejected. 2. Define and distinguish between true-breeding organisms, hybrids, the P generation, the F 1 generation, and the F 2 generation. 3. Define and distinguish between the following pairs of terms: • homozygous and heterozygous; • dominant allele and recessive allele; • genotype and phenotype. • Monohybrid cross, dihybrid cross and test cross 4. Understand law of segregation and law of independent assortment 5....
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive...
Objective: Create an Inheritance Hierarchy to Demonstrate Polymorphic Behavior 1. Create a Rectangle 2. Class Derive a Square Class from the Rectangle Class 3. Derive a Right Triangle Class from the Rectangle Class 4. Create a Circle Class 5. Create a Sphere Class 6. Create a Prism Class 7. Define any other classes necessary to implement a solution 8. Define a Program Driver Class for Demonstration (Create a Container to hold objects of the types described above Create and Add...
In Chapter employee benefit we explored employee benefits. If you were starting a small business of...
In Chapter employee benefit we explored employee benefits. If you were starting a small business of say 250 employees what type of benefits would you have to provide? Which would you offer as supplemental? Explain why you included each benefit.
TOPIC 2: Use Figure 2.6 Maslow's Hierarchy of Needs and the accompanying text in Chapter 2...
TOPIC 2: Use Figure 2.6 Maslow's Hierarchy of Needs and the accompanying text in Chapter 2 to discuss three products you use and what need they fill in your life. Please use specific examples and explain how they relate to the hierarchy. (5 points per product)
Question 2 chapter 15 Handout Assignment
Millet Sales Corp., a public company, is planning to acquire new computers with a total value of $ 60,000 on January 1, 2021. They have a choice of leasing the computers for a three-year period, or purchasing them and financing the purchase by issuing a note payable. Details of the two alternative arrangements are as follows: 1. Lease option: Three annual lease payments of $ 22,446 due on December 31 of each year. Millet would purchase the computers at the end...
a) In Chapter 9 (Week 9 Tutorial) we discussed the integration of the Human Resource Management...
a) In Chapter 9 (Week 9 Tutorial) we discussed the integration of the Human Resource Management and Payroll databases. In particular, we discussed different benefits of this integration in the Week 9 Tutorials. Some companies believe in the integration of HRM and Payroll systems. However, many firms do not agree with this integration. These companies maintain separate HRM information systems and Payroll. Required: Discuss both arguments. For this discussion, think in terms of (1) difference in employee background, and (2)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT