Assignment 2 In chapter 9 of your Deitel & Deitel text, you studied an inheritan
ID: 3878660 • Letter: A
Question
Assignment 2
In chapter 9 of your Deitel & Deitel text, 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 BasePlusCommissionEmployeeTest app using these new classes to ensure that the app still displays the same results for a BasePlusCommissionEmployee object.
-----------------------------------------------------------------------------------------------------------------
Explanation / Answer
Employee.java
public class Employee {
private String firstName;
private String lastName;
private String socialSecurityNumber;
public Employee(String firstName, String lastName,
String socialSecurityNumber) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
}
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;
}
@Override
public String toString() {
return String.format("%s,%s,%s", getFirstName(), getLastName(), getSocialSecurityNumber());
}
}
______________________
CommissionEmployee.java
public class CommissionEmployee extends Employee {
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage
public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate) {
super(firstName, lastName, socialSecurityNumber);
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public double getGrossSales() {
return grossSales;
}
public void setGrossSales(double grossSales) {
this.grossSales = grossSales;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
@Override
public String toString() {
return String.format("%s: %s%n%s: %.2f%n%s: %.2f",
super.toString(),
"commission employee",
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
}
____________________
BasePlusCommissionEmployee.java
public class BasePlusCommissionEmployee extends CommissionEmployee {
private double baseSalary; // base salary per week
public BasePlusCommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary) {
super(firstName, lastName, socialSecurityNumber, grossSales,
commissionRate);
this.baseSalary = baseSalary;
}
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
@Override
public String toString() {
return String.format("%s %s%n%s: %.2f", "base-salaried",
super.toString(), "base salary", getBaseSalary());
}
}
_______________________
BasePlusCommissionEmployeeTest.java
public class BasePlusCommissionEmployeeTest {
public static void main(String[] args) {
// instantiate CommissionEmployee object
CommissionEmployee employee1 = 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("%n%s %s%n", "First name is",
employee1.getFirstName());
System.out.printf("%s %s%n", "Last name is",
employee1.getLastName());
System.out.printf("%s %s%n", "Social security number is",
employee1.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is",
employee1.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is",
employee1.getCommissionRate());
employee1.setGrossSales(5000);
employee1.setCommissionRate(.1);
System.out.printf("%n%s:%n%n%s%n",
"Updated employee information obtained by toString", employee1);
// instantiate BasePlusCommissionEmployee object
BasePlusCommissionEmployee employee2 =
new BasePlusCommissionEmployee(
"Bob", "Lewis", "333-33-3333", 5000, .04, 300);
// get base-salaried commission employee data
System.out.println(
"Employee information obtained by get methods:");
System.out.printf("%n%s %s%n", "First name is",
employee2.getFirstName());
System.out.printf("%s %s%n", "Last name is",
employee2.getLastName());
System.out.printf("%s %s%n", "Social security number is",
employee2.getSocialSecurityNumber());
System.out.printf("%s %.2f%n", "Gross sales is",
employee2.getGrossSales());
System.out.printf("%s %.2f%n", "Commission rate is",
employee2.getCommissionRate());
System.out.printf("%s %.2f%n", "Base salary is",
employee2.getBaseSalary());
employee2.setBaseSalary(1000);
System.out.printf("%n%s:%n%n%s%n",
"Updated employee information obtained by toString",
employee2.toString());
}
}
_____________________
Output:
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:
Sue,Jones,222-22-2222: commission employee
gross sales: 5000.00
commission rate: 0.10
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 Bob,Lewis,333-33-3333: commission employee
gross sales: 5000.00
commission rate: 0.04
base salary: 1000.00
_________________Thank You