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

9.14 ( Employee Hierarchy) In this chapter, you studied an inheritance hierarchy

ID: 3694485 • Letter: 9

Question

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 Employee s are firstName , lastName , socialSe- curityNumber , 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 theCommissionEmployeeTest 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.

Explanation / Answer

Employee.java

package org.students;


public class Employee {
//Declaring instance variables
private String firstName;
private String lastName;
private String socialSecurityNumber;

//Default Constructor
public Employee() {
   super();
}

//Parameterized constructor.
public Employee(String firstName, String lastName, String socialSecurityNumber) {
   super();
   this.firstName = firstName;
   this.lastName = lastName;
   this.socialSecurityNumber = socialSecurityNumber;
}

//Setters and getters
public String getFirstName() {
   return firstName;
}

public void setFirstName(String firstName) {
   this.firstName = firstName;
}
public String getLastName() {
   return lastName;
}
public void setLastName(String lastName) {
   this.lastName = lastName;
}
public String getSocialSecurityNumber() {
   return socialSecurityNumber;
}
public void setSocialSecurityNumber(String socialSecurityNumber) {
   this.socialSecurityNumber = socialSecurityNumber;
}
//toString() method which displays the contents of the object.
@Override
public String toString() {

   System.out.println("======= Employee class =====");
   System.out.println("firstName="+ firstName);
   System.out.println("lastName=" + lastName);
   System.out.println("socialSecurityNumber="+socialSecurityNumber);
   return "";
}

}

_______________________________________________________________________________________

CommissionEmployee.java

package org.students;

public class CommissionEmployee extends Employee
{
   //Declaring instance variables
   private double grossSales;
   private double commissionRate;
  
   //Default Constructor
   public CommissionEmployee() {
       super();
   }
  
   //Parameterized Constructor.
   public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber,double grossSales, double commissionRate) {
       super(firstName,lastName,socialSecurityNumber);
       if (grossSales < 0.0) throw new IllegalArgumentException( "Gross sales must be >= 0.0");
       if (commissionRate <= 0.0 || commissionRate >= 1.0) throw new IllegalArgumentException( "Commission rate must be > 0.0 and < 1.0");
       this.grossSales = grossSales;
       this.commissionRate = commissionRate;
   }

   //Getters and Setters
   public double getGrossSales() {
       return grossSales;
   }

   public void setGrossSales(double grossSales) {
       if (grossSales < 0.0) throw new IllegalArgumentException( "Gross sales must be >= 0.0");
       this.grossSales = grossSales;
   }

   public double getCommissionRate() {
           return commissionRate;
   }

   public void setCommissionRate(double commissionRate) {
   if (commissionRate <= 0.0 || commissionRate >= 1.0) throw new IllegalArgumentException( "Commission rate must be > 0.0 and < 1.0");
       this.commissionRate = commissionRate;
   }

   @Override
   public String toString() {
       super.toString();
       System.out.println("======= CommissionEmployee class =====");
       System.out.println("GrossSales="+ grossSales);
       System.out.println("CommissionRate="+commissionRate);
       return "";
   }
  
}

________________________________________________________________________________________

CommissionEmployeeTest.java

package org.students;

import java.util.Scanner;

public class CommissionEmployeeTest {


   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
   System.out.print("First Name::");
   String fname=sc.next();
   System.out.print("Last Name::");
   String lname=sc.next();
   System.out.print("Social security number::");
   String ssn=sc.next();
   System.out.print("Gross sales::");
   double gs=sc.nextDouble();
   System.out.print("commission rate::");
   double crate=sc.nextDouble();
  
   //Creating CommissionEmployee Object
   CommissionEmployee co=new CommissionEmployee(fname, lname, ssn, gs, crate);
   System.out.println(co.toString());
  
   }

}

___________________________________________________________________________________________

output:

First Name::Tony
Last Name::Greg
Social security number::112-23-334
Gross sales::45000
commission rate::0.9
======= Employee class =====
firstName=Tony
lastName=Greg
socialSecurityNumber=112-23-334
======= CommissionEmployee class =====
GrossSales=45000.0
CommissionRate=0.9

_________________________________________________________________________________________

package org.students;

public class BasePlusCommissionEmployee extends CommissionEmployee {
   public BasePlusCommissionEmployee(String firstName, String lastName, String socialSecurityNumber,double grossSales, double commissionRate) {
       super(firstName,lastName,socialSecurityNumber,grossSales,commissionRate);
   }

   @Override
   public String toString() {
       System.out.println("========== BasePlusCommissionEmployee class ==========");
       super.toString();
       return "";
   }

}

_________________________________________________________________________________________

package org.students;

import java.util.Scanner;

public class BasePlusCommissionEmployeeTest {

   public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       System.out.print("First Name::");
       String fname=sc.next();
       System.out.print("Last Name::");
       String lname=sc.next();
       System.out.print("Social security number::");
       String ssn=sc.next();
       System.out.print("Gross sales::");
       double gs=sc.nextDouble();
       System.out.print("commission rate::");
       double crate=sc.nextDouble();
      
       BasePlusCommissionEmployee bpce=new BasePlusCommissionEmployee(fname, lname, ssn, gs, crate);
       System.out.println(bpce.toString());
  

   }

}

_______________________________________________________________________________________

output2:

First Name::Tony
Last Name::Greg
Social security number::112-23-334
Gross sales::45000
commission rate::0.9
========== BasePlusCommissionEmployee class ==========
======= Employee class ===========
firstName=Tony
lastName=Greg
socialSecurityNumber=112-23-334
======= CommissionEmployee class =====
GrossSales=45000.0
CommissionRate0.9

_________________________________________________________________________________________

we will get the same outout in both the cases.