Question

In: Computer Science

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 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

JAVA PROGRAM

/////////////////////CommissionCompensationModel.java//////////////////////////////

package employee.commission;

//vbase class: CommissionCompensationModel
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;  
   }
  
  
  
}

////////////////////////////////BasePlusCommissionCompensationModel.java///////////////////////////////

package employee.commission;

//class:BasePlusCommissionCompensationModel
//inhertis : CommissionCompensationModel
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;  
   }

}

///////////////////////Employee.java///////////////////////

package employee.commission;

//class : Employee
public class Employee {
   //private fields
   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;
   }
  
}

/////////////////////////Main.java////////////////////////

package employee.commission;

public class Main {
   public static void main(String[] args){
       // 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);
   }
}

======================================

OUTPUT

======================================

John Smith
Social Security Number:111-11-1111
Commission Compensation with:
Gross Sales of: 2000.0
Commission Rate of: 0.04
Earnings: 80.0
Sue Jones
Social Security Number:222-22-2222
Base Plus Commission Compensation with:
Gross Sales of: 2000.0
Commission Rate of: 0.05
Base salary of: 600.0
Earnings: 700.0
Earnings for John Smith: 80.00

John Smith
Social Security Number:111-11-1111
Base Plus Commission Compensation with:
Gross Sales of: 4000.0
Commission Rate of: 0.05
Base salary of: 800.0
Earnings: 1000.0
Sue Jones
Social Security Number:222-22-2222
Commission Compensation with:
Gross Sales of: 5000.0
Commission Rate of: 0.04
Earnings: 200.0


Related Solutions

Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In...
Java Code: Problem #1: Create an inheritance hierarchy of Rodent: mouse, gerbil, hamster, guinea pig. In the base class, provide methods that are common to all rodents based on behaviours you find with a quick Internet search. Be sure to document the behaviours you implement (e.g., eat, sleep, groom, move, etc.). Each behaviour should print its action to standard output (e.g., rodent eating). Next, refine these behaviours in the child classes to perform different behaviours, depending on the specific type...
Java programming question:Consider the inheritance hierarchy in the figure on the next page.The top...
Java programming question:Consider the inheritance hierarchy in the figure on the next page. The top of the hierarchy begins with class Shape, which is extended by subclasses TwoDShape and ThreeDShape, corresponding to 2D and 3D shapes, respectively. The third level of this hierarchy contains specific types of 2D and 3D shapes such as circles and spheres, respectively. The arrows in the graph represent is a(inheritance) relationships and the boxes represent classes. For example, a Triangle is a TwoDShape, which in...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code...
JAVA programming - please ONLY answer prompts with java code *Inheritance* will be used in code Classwork A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose. Part A Think through common characteristics of animals, and write a class ZooAnimal. ☑ ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for. ☑ It should also have at least two methods...
Question(Design Java Method). There is a java code what created a "Student" class and hold all...
Question(Design Java Method). There is a java code what created a "Student" class and hold all the books owned by the student in the inner class "Book". Please propose a new method for the Student class so that given a Student object "student", a program can find out the title of a book for which student.hasBook(isbn) returns true. Show the method code and the calls to it from a test program. The Student class is following: import java.lang.Integer; import java.util.ArrayList;...
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....
Lesson on Inheritance in Java: Using the payroll weekly paycheck calculator scenario as a model, your...
Lesson on Inheritance in Java: Using the payroll weekly paycheck calculator scenario as a model, your code will consist of a superclass (Employee), three subclasses (SalaryEmployee, HourlyEmployee, CommEmployee) and a test class (PayrollTest). - The superclass should contain fields for common user data for an employee (first name and last name) and a toString method that returns the formatted output of the names to the calling method. - Three subclasses should be created that all inherit from the superclass: one...
Must be written in JAVA Code Modify the program you created in previous project to accomplish...
Must be written in JAVA Code Modify the program you created in previous project to accomplish the following: Support the storing of additional user information: street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' Store the data in an ArrayList object Program to Modify import java.util.ArrayList; import java.util.Scanner; public class Address { String firstname; String lastname; int zipcode;    Address(String firstname,String...
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
Write a short code segment which includes/illustrates the concepts of inheritance, overloading, and overriding in java language.
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)...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am...
JAVA CODE --- from the book, java programming (7th edition) chapter 7 carly's question I am getting a illegal expression error and don't know how to fix it, also may be a few other error please help CODE BELOW import java.util.Scanner; public class Event { public static double pricePerGuestHigh = 35.00; public static double pricePerGuestLow = 32.00; public static final int LARGE_EVENT_MAX = 50; public String phnum=""; public String eventNumber=""; private int guests; private double pricePerEvent; public void setPhoneNumber() {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT