The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java
ID: 3704479 • Letter: T
Question
The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and Hourly.java are from Listings 9.1 - 9.7 in the text. The program illustrates inheritance and polymorphism. In this exercise you will add one more employee type to the class hierarchy (see Figure 9.1 in the text). The employee will be one that is an hourly employee but also earns a commission on sales. Hence the class, which we’ll name Commission, will be derived from the Hourly class. Write a class named Commission with the following features: It extends the Hourly class. It has two instance variables (in addition to those inherited): one is the total sales the employee has made (type double) and the second is the commission rate for the employee (the commission rate will be type double and will represent the percent (in decimal form) commission the employee earns on sales (so .2 would mean the employee earns 20% commission on sales)). The constructor takes 6 parameters: the first 5 are the same as for Hourly (name, address, phone number, social security number, hourly pay rate) and the 6th is the commission rate for the employee. The constructor should call the constructor of the parent class with the first 5 parameters then use the 6th to set the commission rate. One additional method is needed: public void addSales (double totalSales) that adds the parameter to the instance variable representing total sales. The pay method must call the pay method of the parent class to compute the pay for hours worked then add to that the pay from commission on sales. (See the pay method in the Executive class.) The total sales should be set back to 0 (note: you don’t need to set the hours Worked back to 0—why not?). The toString method needs to call the toString method of the parent class then add the total sales to that. To test your class, update Staff.java as follows: Increase the size of the array to 8. Add two commissioned employees to the staffList—make up your own names, addresses, phone numbers and social security numbers. Have one of the employees earn $6.25 per hour and 20% commission and the other one earn $9.75 per hour and 15% commission. For the first additional employee you added, put the hours worked at 35 and the total sales $400; for the second, put the hours at 40 and the sales at $950. Compile and run the program. Make sure it is working properly.
firm.java
public class Firm { Creates a staff of employees for a firm and pays them.
public static void main (String[] args) { Staff personnel = new Staff(); personnel.payday(); } }'
---------------------------------------------------------------------------------------------
staff.java
public class Staff { StaffMember[] staffList;
public Staff () { staffList = new StaffMember[6]; staffList[0] = new Executive ("Sam", "123 Main Line", "555-0469", "123-45-6789", 2423.07);
staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 1246.15);
staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23);
staffList[3] = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55);
staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374") ;
staffList[5] = new Volunteer ("Cliff", "321 Duds Lane", "555-7282");
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40); }
public void payday () { double amount; for (int count=0; count < staffList.length; count++)
{ System.out.println (staffList[count]);
amount = staffList[count].pay(); // polymorphic if (amount == 0.0) System.out.println ("Thanks!");
else System.out.println ("Paid: " + amount); System.out.println ("------------------------------------"); } } }
----------------------------------------------------------------------------------------------------------------------------------------
Staffmember.java
abstract public class StaffMember { protected String name; protected String address; protected String phone;
public StaffMember (String eName, String eAddress, String ePhone) { name = eName; address = eAddress; phone = ePhone; }
public String toString() { String result = "Name: " + name + " "; result += "Address: " + address + " "; result += "Phone: " + phone; return result; }
public abstract double pay(); }
---------------------------------------------------------------------------------
volunteer.java
public class Volunteer extends StaffMember {
public Volunteer (String eName, String eAddress, String ePhone)
{ super (eName, eAddress, ePhone); }
public double pay() { return 0.0; } }
--------------------------------------------------------------------------
Employee.java
public class Employee extends StaffMember
{ protected String socialSecurityNumber;
protected double payRate;
public Employee (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone); socialSecurityNumber = socSecNumber; payRate = rate; }
public String toString() { String result = super.toString (); result += " Social Security Number: " + socialSecurityNumber; return result; }
public double pay() { return payRate; } }
------------------------------------------------------------------
Executive.java
public class Executive extends Employee
{ private double bonus;
public Executive (String eName, String eAddress, String ePhone, String socSecNumber, double rate)
{ super (eName, eAddress, ePhone, socSecNumber, rate); bonus = 0;
public void awardBonus (double execBonus) { bonus = execBonus; }
public double pay() { double payment = super.pay() + bonus; bonus = 0; return payment; } }
----------------------------------------------------------------------
Hourly.java
public class Hourly extends Employee { private int hoursWorked;
public Hourly (String eName, String eAddress, String ePhone, String socSecNumber, double rate)
{ super (eName, eAddress, ePhone, socSecNumber, rate); hoursWorked = 0; }
public void addHours (int moreHours) { hoursWorked += moreHours; }
public double pay() { double payment = payRate * hoursWorked; hoursWorked = 0; return payment; }
public String toString() { String result = super.toString(); result += " Current hours: " + hoursWorked; return result; } }
Explanation / Answer
package com.employeproblem;
public class Commission extends Hourly{
private double totalSales;
private double commissionRate;
public Commission(String eName, String eAddress, String ePhone, String socSecNumber, double rate,double commissionRate) {
super(eName, eAddress, ePhone, socSecNumber, rate);
this.commissionRate = commissionRate;
}
public void addSales (double totalSales) {
this.totalSales = totalSales;
}
public double pay(){
return super.pay();
}
}
package com.employeproblem;
public class Employee extends StaffMember
{ protected String socialSecurityNumber;
protected double payRate;
public Employee (String eName, String eAddress, String ePhone, String socSecNumber, double rate) { super (eName, eAddress, ePhone); socialSecurityNumber = socSecNumber; payRate = rate; }
public String toString() { String result = super.toString (); result += " Social Security Number: " + socialSecurityNumber; return result; }
public double pay() { return payRate; } }
package com.employeproblem;
public class Executive extends Employee
{
private double bonus;
public Executive (String eName, String eAddress, String ePhone, String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
bonus = 0;
}
public void awardBonus (double execBonus) {
bonus = execBonus;
}
public double pay() {
double payment = super.pay() + bonus; bonus = 0;
return payment;
}
}
package com.employeproblem;
public class Hourly extends Employee{
private int hoursWorked;
public Hourly (String eName, String eAddress, String ePhone, String socSecNumber, double rate)
{ super (eName, eAddress, ePhone, socSecNumber, rate); hoursWorked = 0; }
public void addHours (int moreHours) { hoursWorked += moreHours; }
public double pay() { double payment = payRate * hoursWorked; hoursWorked = 0; return payment; }
public String toString() { String result = super.toString(); result += " Current hours: " + hoursWorked; return result; }
}
package com.employeproblem;
public class Staff { StaffMember[] staffList;
public Staff () {
staffList = new StaffMember[8];
staffList[0] = new Executive ("Sam", "123 Main Line", "555-0469", "123-45-6789", 2423.07);
staffList[1] = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 1246.15);
staffList[2] = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23);
staffList[3] = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55);
staffList[4] = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374") ;
staffList[5] = new Volunteer ("Cliff", "321 Duds Lane", "555-7282");
staffList[6] = new Commission("Nag", "321 Duds Lane", "555-7298", "923-34-2748", 6.25,0.2);
staffList[7] = new Commission("Pawan", "321 Duds Lane", "555-6345", "945-47-4853", 9.75,0.15);
((Executive)staffList[0]).awardBonus (500.00);
((Hourly)staffList[3]).addHours (40);
((Hourly)staffList[6]).addHours(35);
((Commission)staffList[6]).addSales(400);
((Hourly)staffList[7]).addHours(35);
((Commission)staffList[7]).addSales(400);
}
public void payday () {
double amount = 0.0;
for (int count=0; count < staffList.length; count++)
{
System.out.println (staffList[count]);
amount = staffList[count].pay();
if (amount == 0.0){
System.out.println ("Thanks!");
}else {
System.out.println ("Paid: " + amount);
System.out.println ("------------------------------------");
}
}
}
}
package com.employeproblem;
abstract public class StaffMember { protected String name; protected String address; protected String phone;
public StaffMember (String eName, String eAddress, String ePhone) { name = eName; address = eAddress; phone = ePhone; }
public String toString() { String result = "Name: " + name + " "; result += "Address: " + address + " "; result += "Phone: " + phone; return result; }
public abstract double pay(); }
package com.employeproblem;
public class Volunteer extends StaffMember {
public Volunteer (String eName, String eAddress, String ePhone)
{ super (eName, eAddress, ePhone); }
public double pay() { return 0.0; } }
package com.employeproblem;
public class Firm { //Creates a staff of employees for a firm and pays them.
public static void main (String[] args) {
Staff personnel = new Staff();
personnel.payday();
}
}
// you can execute program from Firm Class since we have main method there the below output will be come
// output
Name: Sam
Address: 123 Main Line
Phone: 555-0469
Social Security Number: 123-45-6789
Paid: 2923.07
------------------------------------
Name: Carla
Address: 456 Off Line
Phone: 555-0101
Social Security Number: 987-65-4321
Paid: 1246.15
------------------------------------
Name: Woody
Address: 789 Off Rocker
Phone: 555-0000
Social Security Number: 010-20-3040
Paid: 1169.23
------------------------------------
Name: Diane
Address: 678 Fifth Ave.
Phone: 555-0690
Social Security Number: 958-47-3625
Current hours: 40
Paid: 422.0
------------------------------------
Name: Norm
Address: 987 Suds Blvd.
Phone: 555-8374
Thanks!
Name: Cliff
Address: 321 Duds Lane
Phone: 555-7282
Thanks!
Name: Nag
Address: 321 Duds Lane
Phone: 555-7298
Social Security Number: 923-34-2748
Current hours: 35
Paid: 218.75
------------------------------------
Name: Pawan
Address: 321 Duds Lane
Phone: 555-6345
Social Security Number: 945-47-4853
Current hours: 35
Paid: 341.25
------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.