Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java: 9.14 ( Employee Hierarchy) In this chapter, you studied an inheritan

ID: 3809086 • Letter: U

Question

Using Java:

9.14 (Employee Hierarchy) In this chapter, you studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, you’ll create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, socialSecurityNumber, getFirstName, getLastName, getSocialSecurityNumber and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. Class CommissionEmployee’s constructor should invoke class Employee’s constructor and CommissionEmployee’s toString method should invoke Employee’s toString method. Once you’ve completed these modifications, run the CommissionEmployeeTest and BasePlusCommissionEmployeeTest apps using these new classes to ensure that the apps still display the same results for a CommissionEmployee object and BasePlusCommissionEmployee object, respectively.

public class CommissionEmployee
5   {
6      private final String firstName;                        
7      private final String lastName;                         
8      private final String socialSecurityNumber;             
9      private double grossSales; // gross weekly sales       
10      private double commissionRate; // commission percentage
11
12      // five-argument constructor
13      public CommissionEmployee(String firstName, String lastName,
14         String socialSecurityNumber, double grossSales,
15         double commissionRate)
16      {
17         // implicit call to Object constructor occurs here
18
19         // if grossSales is invalid throw exception
20         if (grossSales < 0.0)
21            throw new IllegalArgumentException(
22               "Gross sales must be >= 0.0");
23
24         // if commissionRate is invalid throw exception
25         if (commissionRate <= 0.0 || commissionRate >= 1.0)
26            throw new IllegalArgumentException(
27               "Commission rate must be > 0.0 and < 1.0");
28
29         this.firstName = firstName;
30         this.lastName = lastName;
31         this.socialSecurityNumber = socialSecurityNumber;
32         this.grossSales = grossSales;
33         this.commissionRate = commissionRate;
34      } // end constructor
35
36      // return first name
37      public String getFirstName()
38      {
39         return firstName;
40      }
41
42      // return last name
43      public String getLastName()
44      {
45         return lastName;
46      }
47
48      // return social security number
49      public String getSocialSecurityNumber()
50      {
51         return socialSecurityNumber;
52      }
53
54      // set gross sales amount
55      public void setGrossSales(double grossSales)
56      {
57         if (grossSales < 0.0)
58            throw new IllegalArgumentException(
59               "Gross sales must be >= 0.0");
60
61         this.grossSales = grossSales;
62      }
63
64      // return gross sales amount
65      public double getGrossSales()
66      {
67         return grossSales;
68      }
69
70      // set commission rate
71      public void setCommissionRate(double commissionRate)
72      {
73         if (commissionRate <= 0.0 || commissionRate >= 1.0)
74            throw new IllegalArgumentException(
75               "Commission rate must be > 0.0 and < 1.0");
76
77         this.commissionRate = commissionRate;
78      }
79
80      // return commission rate
81      public double getCommissionRate()
82      {
83         return commissionRate;
84      }
85
86      // calculate earnings
87      public double earnings()
88      {
89         return getCommissionRate() * getGrossSales();
90      }
91
92      // return String representation of CommissionEmployee object
93      @Override
94      public String toString()
95      {
96         return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
97            "commission employee", getFirstName(), getLastName(),
98            "social security number", getSocialSecurityNumber(),
99            "gross sales", getGrossSales(),
100            "commission rate", getCommissionRate());
101     }
102  } // end class CommissionEmployee

public class BasePlusCommissionEmployee extends CommissionEmployee
7   {
8      private double baseSalary; // base salary per week
9
10      // six-argument constructor
11      public BasePlusCommissionEmployee(String firstName, String lastName,
12         String socialSecurityNumber, double grossSales,
13         double commissionRate, double baseSalary)
14      {
15         super(firstName, lastName, socialSecurityNumber,
16            grossSales, commissionRate);
17
18         // if baseSalary is invalid throw exception
19         if (baseSalary < 0.0)
20            throw new IllegalArgumentException(
21               "Base salary must be >= 0.0");
22
23         this.baseSalary = baseSalary;
24      }
25
26      // set base salary
27      public void setBaseSalary(double baseSalary)
28      {
29         if (baseSalary < 0.0)
30            throw new IllegalArgumentException(
31               "Base salary must be >= 0.0");
32
33         this.baseSalary = baseSalary;
34      }
35
36      // return base salary
37      public double getBaseSalary()
38      {
39         return baseSalary;
40      }
41
42      // calculate earnings
43      @Override
44      public double earnings()
45      {
46         return getBaseSalary() + super.earnings();
47      }
48
49      // return String representation of BasePlusCommissionEmployee
50      @Override
51      public String toString()
52      {
53         return String.format("%s %s%n%s: %.2f", "base-salaried",
54            super.toString(), "base salary", getBaseSalary());   
55      }
56   } // end class BasePlusCommissionEmployee

Explanation / Answer

Employee.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author user
*/
public class Employee {
  
      private String firstName;
      private String lastName;
       private String socialSecurityNumber;

      // three-argument constructor
      public Employee( String first, String last, String ssn )
      {
         firstName = first;
         lastName = last;
         socialSecurityNumber = ssn;
      } // end three-argument Employee constructor

      // set first name
      public void setFirstName( String first )
      {
         firstName = first; // should validate
      } // end method setFirstName

      // return first name
      public String getFirstName()
      {
         return firstName;
     } // end method getFirstName

      // set last name
      public void setLastName( String last )
      {
         lastName = last; // should validate
      } // end method setLastName

      // return last name
      public String getLastName()
      {
         return lastName;
      } // end method getLastName

      // set social security number
      public void setSocialSecurityNumber( String ssn )
      {
         socialSecurityNumber = ssn; // should validate
      } // end method setSocialSecurityNumber

      // return social security number
      public String getSocialSecurityNumber()
      {
         return socialSecurityNumber;
      } // end method getSocialSecurityNumber

      // return String representation of Employee object
      @Override
      public String toString()
      {
         return String.format( "%s %s social security number: %s",
            getFirstName(), getLastName(), getSocialSecurityNumber() );
      } // end method toString

   
   } // end class Employee
  

CommissionEmployee.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author user
*/
public class CommissionEmployee extends Employee
    {
       private double grossSales; // gross weekly sales
       private double commissionRate; // commission percentage

       // five-argument constructor
      public CommissionEmployee( String first, String last, String ssn,
         double sales, double rate )
      {
         super( first, last, ssn );
         setGrossSales( sales );
         setCommissionRate( rate );
      } // end five-argument CommissionEmployee constructor

      // set commission rate
      public void setCommissionRate( double rate )
      {
         if ( rate > 0.0 && rate < 1.0 )
            commissionRate = rate;
         else
            throw new IllegalArgumentException(
               "Commission rate must be > 0.0 and < 1.0" );
      } // end method setCommissionRate

      // return commission rate
      public double getCommissionRate()
      {
         return commissionRate;
      } // end method getCommissionRate

      // set gross sales amount
      public void setGrossSales( double sales )
      {
         if ( sales >= 0.0 )
            grossSales = sales;
         else
            throw new IllegalArgumentException(
               "Gross sales must be >= 0.0" );
      } // end method setGrossSales

      // return gross sales amount
      public double getGrossSales()
      {
         return grossSales;
      } // end method getGrossSales

      // calculate earnings; override abstract method earnings in Employee
                                                        
      public double earnings()                                          
      {                                                                 
      return getCommissionRate() * getGrossSales();                  
      } // end method earnings                                          

      // return String representation of CommissionEmployee object
      @Override                                                 
      public String toString()                                  
      {                                  
      return String.format( "%s: %s %s: $%,.2f; %s: %.2f",
            "commission employee", super.toString(),            
            "gross sales", getGrossSales(),                     
            "commission rate", getCommissionRate() );           
      } // end method toString                                  
   } // end class CommissionEmployee

BasePlusCommisionEmployee.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author user
*/
public class BasePlusCommissionEmployee extends CommissionEmployee
    {
       private double baseSalary; // base salary per week

       // six-argument constructor
       public BasePlusCommissionEmployee( String first, String last,
         String ssn, double sales, double rate, double salary )
      {
         super( first, last, ssn, sales, rate );
         setBaseSalary( salary ); // validate and store base salary
      } // end six-argument BasePlusCommissionEmployee constructor

      // set base salary
      public void setBaseSalary( double salary )
      {
         if ( salary >= 0.0 )
            baseSalary = salary;
         else
            throw new IllegalArgumentException(
               "Base salary must be >= 0.0" );
      } // end method setBaseSalary

      // return base salary
      public double getBaseSalary()
      {
         return baseSalary;
      } // end method getBaseSalary

      // calculate earnings; override method earnings in CommissionEmployee
      @Override                                                          
      public double earnings()                                           
      {                                                                  
      return getBaseSalary() + super.earnings();                      
      }// end method earnings                                            

      // return String representation of BasePlusCommissionEmployee object
      @Override                                                         
      public String toString()                                          
      {                                                                 
      return String.format( "%s %s; %s: $%,.2f",
            "base-salaried", super.toString(),                          
            "base salary", getBaseSalary() );                           
      } // end method toString                                          
   } // end class BasePlusCommissionEmployee

CommissionEmployeeTest.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author user
*/
public class CommissionEmployeeTest
{

public static void main( String args[] )

   {

// instantiate CommissionEmployee object

      CommissionEmployee employee = new CommissionEmployee( "Sue","Jones","222-22-2222",10000,.06 );     


// get commission employee data

      System.out.println( "Employee information obtained by get methods: " );

      System.out.printf( "%s %s ","First name is", employee.getFirstName() );

      System.out.printf("%s %s ","Last name is",   employee.getLastName() );

      System.out.printf( "%s %s ","Social security number is", employee.getSocialSecurityNumber() );

      System.out.printf( "%s %.2f ","Gross sales is", employee.getGrossSales() );

      System.out.printf( "%s %.2f ", "Commission rate is", employee.getCommissionRate() );

      employee.setGrossSales( 500 );
// set gross sales    

      employee.setCommissionRate( .1 );
// set commission rate

      System.out.printf( " %s: %s ", "Updated employee information obtained by toString", employee.toString() );

   }
// end main

}
// end class CommissionEmployeeTest

BasePlusCommissionEmployeeTest.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication2;

/**
*
* @author user
*/
public class BasePlusCommissionEmployeeTest {
  
public static void main( String args[] )
{

      BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee("Bob","Lewis","333-33-3333",5000,.04,300 );
  

System.out.println( "Employee information obtained by get methods: " );
      System.out.printf( "%s %s ", "First name is", employee.getFirstName() );
      System.out.printf( "%s %s ", "Last name is", employee.getLastName() );
      System.out.printf( "%s %s ", "Social security number is", employee.getSocialSecurityNumber() );
      System.out.printf( "%s %.2f ","Gross sales is", employee.getGrossSales() );

      System.out.printf( "%s %.2f ", "Commission rate is", employee.getCommissionRate() );

            System.out.printf( "%s %.2f ","Base salary is", employee.getBaseSalary() );
                  employee.setBaseSalary( 1000 );
                  System.out.printf( " %s: %s ", "Updated employee information obtained by toString", employee.toString() );

   } }

when running baseplusemploeetest.java

output is :

run:
Employee information obtained by get methods:

First name is Bob
Last name is Lewis
Social security number is 333-33-3333
Gross sales is 5000.00
Commission rate is 0.04
Base salary is 300.00

Updated employee information obtained by toString:

base-salaried commission employee: Bob Lewis
social security number: 333-33-3333
gross sales: $5,000.00; commission rate: 0.04; base salary: $1,000.00
BUILD SUCCESSFUL (total time: 0 seconds)

when running CommissionEmployeeTest.java

output is:

run:
Employee information obtained by get methods:

First name is Sue
Last name is Jones
Social security number is 222-22-2222
Gross sales is 10000.00
Commission rate is 0.06

Updated employee information obtained by toString:

commission employee: Sue Jones
social security number: 222-22-2222
gross sales: $500.00; commission rate: 0.10
BUILD SUCCESSFUL (total time: 0 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote