NEED HELP WRITING A JAVA PROGRAM In Chapter 9 we studied an
NEED HELP WRITING A JAVA PROGRAM
In Chapter 9, we studied an inheritance hierarchy in which class BasePlusCommissionEmployee inherited from CommissionEmployee. However, not all employees are CommissionedEmployees. For this assignment, create a more general Employee class that factors out the attributes and behavior that are common to all employees. Also, you will add two new types of employees, a SalariedEmployee, who gets paid a fixed salary, and an HourlyEmployee who gets paid an hourly wage plus time-and-a-half (1.5 times their hourly wage for hours worked over 40 hours). Create the proper Employee class hierarchy to reflect good object-oriented design.
The SalariedEmployee will need the additional instance variable of salary, and the HourlyEmployee will need the additional instance variables of hourlyWage and hoursWorked. Add setters and getters for these classes as appropriate. These classes will also need the earnings method and an override for the toString() method. The SalariedEmployee class will need a setSalary method to set the current salary for the employee. The HourlyEmployee class will need set methods for the hourlyWage and hoursWorked variables also. Salary and hourlyWage should be checked to make sure they are greater than zero and hoursWorked should be checked to ensure that it is between 1 and 168 (the number of hours in a week).
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:
CommissionEmployee employee1 = new CommissionEmployee(\"Fred\", \"Jones\", \"111-11-1111\", 2000.0, .05);
BasePlusCommissionEmployee employee2 = new BasePlusCommissionEmployee(\"Sue\", \"Smith\", \"222-22-2222\", 3000.0, .05, 300);
SalariedEmployee employee3 = new SalariedEmployee(\"Sha\", \"Yang\", \"333-33-3333\", 1150.0);
HourlyEmployee employee4 = new HourlyEmployee(\"Ian\", \"Tanning\", \"444-44-4444\", 15.0, 50);
HourlyEmployee employee5 = new HourlyEmployee(\"Angela\", \"Domchek\", \"555-55-5555\", 20.0, 40);
System.out.printf(\"%s%s%s%s%s\", employee1, employee2, employee3, employee4, employee5);
The output from your program should look like the following:
run:
Commissioned Employee: Fred Jones with ssn: 111-11-1111
Gross Sales: 2000.00
Commission Rate: 0.05
Earnings: $100.00
Base Salary Plus Commissioned Employee: Sue Smith with ssn: 222-22-2222
Gross Sales: 3000.00
Commission Rate: 0.05
with Base Salary of: $300.00
Earnings: $450.00
Salaried Employee: Sha Yang with ssn: 333-33-3333
Salary: 1150.00
Earnings: $1150.00
Hourly Employee: Ian Tanning with ssn: 444-44-4444
Hourly Wage: 15.00
Hours Worked: 50.00
Earnings: $825.00
Hourly Employee: Angela Domchek with ssn: 555-55-5555
Hourly Wage: 20.00
Hours Worked: 40.00
Earnings: $800.00
public class Employees {
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales;
private double commissionRate;
//five-argument constructor
public Employees(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 commssionRate 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 Constructors
//return first name
public String getFirstName()
{
return firstName;
}
//return last name
public String getLastName()
{
return lastName;
}
//return Social Secrutiy 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 represntation of Commission Employee 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 Comission Employee
//BaseplusCommissionEmployee
public class BasePlusCommissionEmployee extends Employees
{
private double baseSalary; // base salary oer 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;
}
//calcualte earnings
@Override
public double earnings()
{
return getBaseSalary()+super.earnings();
}
//return String representation of Base Plus Commission Employee
@Override
public String toString()
{
return String.format(\"%s %s%n%s %.2f\",\"base-salaried\",
super.toString(),\"base salary\", getBaseSalary());
}
}//end class BasePlusCommissionEmployee
Solution
Employee.java
public class Employee {
//Declaring instance variables
private String firstname;
private String lastname;
private String socialSecurityNumber;
public Employee() {
}
//Parameterised 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;
}
@Override
public String toString() {
System.out.println(getFirstname()+\" \"+getLastname()+\" with ssn: \"+getSocialSecurityNumber());
return \" \";
}
}
___________________
CommissionEmployee.java
public class CommissionEmployee extends Employee
{
//Declaring instance variables
private double grossSlaes;
private double commissionRate;
//Parameterised constructor
public CommissionEmployee(String firstname, String lastname, String ssn,
double grossSales, double commissionrate) {
super(firstname,lastname,ssn);
this.grossSlaes=grossSales;
this.commissionRate=commissionrate;
}
//Setters and Getters.
public double getGrossSlaes() {
return grossSlaes;
}
public void setGrossSlaes(double grossSlaes) {
this.grossSlaes = grossSlaes;
}
public double getCommissionRate() {
return commissionRate;
}
public void setCommissionRate(double commissionRate) {
this.commissionRate = commissionRate;
}
/*earnings() will return the money earned by that person
* Params: void
* Return : double
*/
public double earnings()
{
return getCommissionRate()*getGrossSlaes();
}
//toString() method will display the contents of the Object.
@Override
public String toString() {
System.out.print(\"Commissioned Employee :\");
super.toString();
System.out.println(\"Gross Sales: \"+getGrossSlaes());
System.out.println(\"Commission Rate: \"+getCommissionRate());
System.out.println(\"Earnings: \"+earnings());
return \"\";
}
}
_____________________________
BasePlusCommissionEmployee.java
public class BasePlusCommissionEmployee extends CommissionEmployee{
//Declaring instance variables
private double baseSalary;
//Parameterised constructor
public BasePlusCommissionEmployee(String firstname, String lastname, String ssn,
double grossSales, double commissionrate,double basesalary)
{
super(firstname,lastname,ssn,grossSales,commissionrate);
this.baseSalary=basesalary;
}
//Setters and Getters.
public double getBaseSalary() {
return baseSalary;
}
public void setBaseSalary(double baseSalary) {
this.baseSalary = baseSalary;
}
/*earnings() will return the money earned by that person
* Params: void
* Return : double
*/
public double earnings()
{
return getBaseSalary()+super.earnings();
}
//toString() method will display the contents of the Object.
@Override
public String toString() {
System.out.print(\"Base Salary Plus \");
super.toString();
System.out.println(\" with Base Salary of: \"+getBaseSalary());
System.out.println(\"Earnings: \"+earnings());
return \" \";
}
}
_______________________
HourlyEmployee.java
public class HourlyEmployee extends Employee {
//Declaring instance variables
private double hourlyWage;
private int hoursworked;
//Parameterised constructor
public HourlyEmployee(String firstname, String lastname, String ssn,
double hourlyWage, int hoursworked) {
super(firstname,lastname,ssn);
this.hourlyWage=hourlyWage;
this.hoursworked=hoursworked;
}
//Setters and Getters.
public double getHourlyWage() {
return hourlyWage;
}
public void setHourlyWage(double hourlyWage) {
if(hourlyWage>0)
{
this.hourlyWage = hourlyWage;
}
else
{
throw new IllegalArgumentException(\"HourlyWage must be greater than 0.0\");
}
}
public int getHoursworked() {
return hoursworked;
}
public void setHoursworked(int hoursworked) {
if(hoursworked>0 && hoursworked<=168)
{
this.hoursworked = hoursworked;
}
else
{
throw new IllegalArgumentException(\"Hours Worked must be greater than 0 and less than 168\");
}
}
/*earnings() will return the money earned by that person
* Params: void
* Return : double
*/
public double earnings()
{
double sal=0;
if(hoursworked<=40)
sal= hourlyWage*hoursworked;
else if(hoursworked>40)
{
sal= (40*hourlyWage)+((hoursworked-40)*(hourlyWage*1.5));
}
return sal;
}
//toString() method will display the contents of the Object.
@Override
public String toString() {
System.out.print(\"Hourly Employee:\");
super.toString();
System.out.println(\"Hourly Wage :\"+getHourlyWage());
System.out.println(\"Hours Worked :\"+getHoursworked());
System.out.println(\"Earnings :\"+earnings());
return \" \";
}
}
__________________________
SalariedEmployee.java
public class SalariedEmployee extends Employee {
//Declaring instance variables
private double salary;
//Parameterised constructor
public SalariedEmployee(String firstname, String lastname, String ssn,double salary)
{
super(firstname,lastname,ssn);
this.salary=salary;
}
//Setters and Getters.
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary>0)
{
this.salary = salary;
}
else
{
throw new IllegalArgumentException(\"Salary must be greater than 0\");
}
}
/*earnings() will return the money earned by that person
* Params: void
* Return : double
*/
public double earnings()
{
return getSalary();
}
//toString() method will display the contents of the Object.
@Override
public String toString() {
System.out.print(\"Salaried Employee:\");
super.toString();
System.out.println(\"Salary: : \"+getSalary());
System.out.println(\"Earnings: \"+earnings());
return \" \";
}
}
_________________________
Test.java
public class Test {
public static void main(String[] args) {
CommissionEmployee employee1 = new CommissionEmployee(\"Fred\", \"Jones\", \"111-11-1111\", 2000.0, .05);
BasePlusCommissionEmployee employee2 = new BasePlusCommissionEmployee(\"Sue\", \"Smith\", \"222-22-2222\", 3000.0, .05, 300);
SalariedEmployee employee3 = new SalariedEmployee(\"Sha\", \"Yang\", \"333-33-3333\", 1150.0);
HourlyEmployee employee4 = new HourlyEmployee(\"Ian\", \"Tanning\", \"444-44-4444\", 15.0, 50);
HourlyEmployee employee5 = new HourlyEmployee(\"Angela\", \"Domchek\", \"555-55-5555\", 20.0, 40);
System.out.printf(\"%s%s%s%s%s\", employee1, employee2, employee3, employee4, employee5);
}
}
______________________
Output:
Commissioned Employee :Fred Jones with ssn: 111-11-1111
Gross Sales: 2000.0
Commission Rate: 0.05
Earnings: 100.0
Base Salary Plus Commissioned Employee :Sue Smith with ssn: 222-22-2222
Gross Sales: 3000.0
Commission Rate: 0.05
Earnings: 450.0
with Base Salary of: 300.0
Earnings: 450.0
Salaried Employee:Sha Yang with ssn: 333-33-3333
Salary: : 1150.0
Earnings: 1150.0
Hourly Employee:Ian Tanning with ssn: 444-44-4444
Hourly Wage :15.0
Hours Worked :50
Earnings :825.0
Hourly Employee:Angela Domchek with ssn: 555-55-5555
Hourly Wage :20.0
Hours Worked :40
Earnings :800.0
___________Thank You








