CSIS 312 Assignment 2 Employee Class

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.

Make sure that your screen shots show your program running and that your runtime display shows that your program does all that is required of it. You only get credit for what you demonstrate.

Submit this assignment by 11:59 p.m. (ET) on Monday of Module/Week 2.


// Fig. 9.10: CommissionEmployee.java
// CommissionEmployee class uses methods to manipulate its
// private instance variables.
public class CommissionEmployee
{
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales; // gross weekly sales
private double commissionRate; // commission percentage

// five-argument constructor
public CommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate)
{
// implicit call to Object constructor occurs here

// if grossSales is invalid throw exception
if (grossSales < 0.0)
throw new IllegalArgumentException(
"Gross sales must be = 0.0");

// if commissionRate is invalid throw exception
if (commissionRate <= 0.0 || commissionRate = 1.0)
throw new IllegalArgumentException(
"Commission rate must be 0.0 and < 1.0");

this.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.grossSales = grossSales;
this.commissionRate = commissionRate;
} // end constructor

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

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

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

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

this.grossSales = grossSales;
}

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

// set commission rate
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;
}

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

// calculate earnings
public double earnings()
{
return getCommissionRate() * getGrossSales();
}

// return String representation of CommissionEmployee object
@Override
public String toString()
{
return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
"commission employee", getFirstName(), getLastName(),
"social security number", getSocialSecurityNumber(),
"gross sales", getGrossSales(),
"commission rate", getCommissionRate());
}
} // end class CommissionEmployee

// Fig. 9.11: BasePlusCommissionEmployee.java
// BasePlusCommissionEmployee class inherits from CommissionEmployee
// and accesses the superclass’s private data via inherited
// public methods.

public class BasePlusCommissionEmployee extends CommissionEmployee
{
private double baseSalary; // base salary per week

// six-argument constructor
public BasePlusCommissionEmployee(String firstName, String lastName,
String socialSecurityNumber, double grossSales,
double commissionRate, double baseSalary)
{
super(firstName, lastName, socialSecurityNumber,
grossSales, commissionRate);

// if baseSalary is invalid throw exception
if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be = 0.0");

this.baseSalary = baseSalary;
}

// set base salary
public void setBaseSalary(double baseSalary)
{
if (baseSalary < 0.0)
throw new IllegalArgumentException(
"Base salary must be = 0.0");

this.baseSalary = baseSalary;
}

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

// calculate earnings
@Override
public double earnings()
{
return getBaseSalary() + super.earnings();
}

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

// BasePlusCommissionEmployeeTest.java
// Testing class BasePlusCommissionEmployee.

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());
} // end main
} // end class BasePlusCommissionEmployeeTest
Powered by