Java Here is one version of the CommissionEmployee class : public class Commissi
ID: 3755347 • Letter: J
Question
Java
Here is one version of the CommissionEmployee class :
public class CommissionEmployee extends Object {
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
private double grossSales;
private double commissionRate;
public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) {
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.firstName = firstName;
this.lastName = lastName;
this.socialSecurityNumber = socialSecurityNumber;
this.grossSales = grossSales;
this.commissionRate = commissionRate;
}
public String getFirstName() {return firstName;}
public String getLastName() {return lastName;}
public String getSocialSecurityNumber() {return socialSecurityNumber;}
public void setGrossSales(double grossSales) {
if (grossSales < 0.0) throw new IllegalArgumentException( "Gross sales must be >= 0.0");
this.grossSales = grossSales;
}
public double getGrossSales() {return grossSales;}
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;
}
public double getCommissionRate() {return commissionRate;}
public double earnings() {return commissionRate * grossSales;}
@Override
public String toString() {
return String .format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f",
"commission employee", firstName, lastName,
"social security number", socialSecurityNumber,
"gross sales", grossSales,
"commission rate", commissionRate);
}
}
However, it's important to note that not all employees are CommissionEmployees. Create a more general Employee superclass that factors out the attributes and behaviors in CommissionEmployees 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 the method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor .
Next, rewrite the CommissionEmployee class as a subclass of Employee. CommissionEmployee should contain only the instance variables and methods that are not declared in superclass Employee. CommissionEmployee's constructor should invoke Employee's constructor and CommissionEmployee's toString method should invoke Employee's toString method .
Create a Driver class to test your new CommissionEmployee class . Prompt the user to input the first name , last name , social security number, gross sales, and commission rate and create a CommissionEmployee object , using the toString method to print its information.
SAMPLE RUN #1: java Driver
Enter·first·name:Gabriel?
Enter·last·name:Acosta?
Enter·social·security·number:512-19-3789?
Enter·gross·sales:15640?
Enter·commission·rate:.15?
commission·employee:·Gabriel·Acosta?
social·security·number:·512-19-3789?
gross·sales:·15640.00?
commission·rate:·0.15?
Explanation / Answer
Abstract Employee class:
//START of Employee class
public abstract class Employee {
//instance variables
private final String firstName;
private final String lastName;
private final String socialSecurityNumber;
//constructor
Employee(String firstName,String lastName,String socialSecurityNumber)
{
this.firstName=firstName;
this.lastName=lastName;
this.socialSecurityNumber=socialSecurityNumber;
}
//getter methods
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSocialSecurityNumber() {
return socialSecurityNumber;
}
//toString() method
@Override
public String toString()
{
return firstName +lastName+" social·security·number:·"+socialSecurityNumber;
}
}
Modified CommissionEmployee class
//START of CommissionEmployee class
public class CommissionEmployee extends Employee
{
//instance variables
private double grossSales;
private double commissionRate;
//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;
}
public void setGrossSales(double grossSales) {
if (grossSales < 0.0) throw new IllegalArgumentException( "Gross sales must be >= 0.0");
this.grossSales = grossSales;
}
public double getGrossSales() {
return grossSales;
}
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;
}
public double getCommissionRate() {
return commissionRate;
}
public double earnings() {
return commissionRate * grossSales;
}
@Override
public String toString() {
return "commission employee:·"+ super.toString()+" "+
"grosssales:·"+grossSales+" "+
"commission rate:·"+commissionRate;
}
}
//END of CommissionEmployee class
Driver class:
import java.util.*;
public class TestEmployeeApp {
//main method to run application
public static void main(String[] args)
{
//Scanner class to read inputs
Scanner scan = new Scanner(System.in);
//asking user to input
System.out.print("Enter first name:");
String fname=scan.nextLine();
System.out.print("Enter last name:");
String lname=scan.nextLine();
System.out.print("Enter social security number:");
String ssn=scan.nextLine();
System.out.print("Enter gross sale:");
double grossSale=scan.nextDouble();
System.out.print("Enter commission rate:");
double commissionRate=scan.nextDouble();
//creating a CommissionEmployee object
Employee employee = new CommissionEmployee(fname,lname,ssn,grossSale,commissionRate);
//displaying the employee inform
System.out.println(employee);
}
}
Output:
Enter first name:Gabriel
Enter last name:Acosta
Enter social security number:512-19-3789
Enter gross sale:15640
Enter commission rate:.15
commission employee:·GabrielAcosta
social·security·number:·512-19-3789
grosssales:·15640.0
commission rate:·0.15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.