Question

In: Computer Science

Another Type of Employee The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and Hourly.java are from...


Another Type of Employee

The files Firm.java, Staff.java, StaffMember.java, Volunteer.java, Employee.java, Executive.java, and

Hourly.java are from Listings 10.1 - 10.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 $12.00 per hour and 20% commission and the other one earn $14.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 Author: Lewis/Loftus

//

// Demonstrates polymorphism via inheritance.

// ****************************************************************

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 Author: Lewis/Loftus

//

// Represents the personnel staff of a particular business.

//********************************************************************

public class Staff

{

StaffMember[] staffList;

//-----------------------------------------------------------------

// Sets up the list of staff members.

//-----------------------------------------------------------------

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);

}


//-----------------------------------------------------------------

// Pays all staff members.

//-----------------------------------------------------------------

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 Author: Lewis/Loftus

//

// Represents a generic staff member.

//******************************************************************

abstract public class StaffMember

{

protected String name;

protected String address;

protected String phone;

//---------------------------------------------------------------

// Sets up a staff member using the specified information.

//---------------------------------------------------------------

public StaffMember (String eName, String eAddress, String ePhone)

{

name = eName;

address = eAddress;

phone = ePhone;

}


//---------------------------------------------------------------

// Returns a string including the basic employee information.

//---------------------------------------------------------------

public String toString()

{

String result = "Name: " + name + "\n";

result += "Address: " + address + "\n";

result += "Phone: " + phone;

return result;

}


//---------------------------------------------------------------

// Derived classes must define the pay method for each type of

// employee.

//---------------------------------------------------------------

public abstract double pay();

}


//******************************************************************

// Volunteer.java Author: Lewis/Loftus

//

// Represents a staff member that works as a volunteer.

//******************************************************************

public class Volunteer extends StaffMember

{

//---------------------------------------------------------------

// Sets up a volunteer using the specified information.

//---------------------------------------------------------------

public Volunteer (String eName, String eAddress, String ePhone)

{

super (eName, eAddress, ePhone);

}


//---------------------------------------------------------------

// Returns a zero pay value for this volunteer.

//---------------------------------------------------------------

public double pay()

{

return 0.0;

}

}

//******************************************************************

// Employee.java Author: Lewis/Loftus

//

// Represents a general paid employee.

//******************************************************************

public class Employee extends StaffMember

{

protected String socialSecurityNumber;

protected double payRate;


//---------------------------------------------------------------

// Sets up an employee with the specified information.

//---------------------------------------------------------------

public Employee (String eName, String eAddress, String ePhone,

String socSecNumber, double rate)

{

super (eName, eAddress, ePhone);

socialSecurityNumber = socSecNumber;

payRate = rate;

}


//---------------------------------------------------------------

// Returns information about an employee as a string.

//---------------------------------------------------------------

public String toString()

{

String result = super.toString ();

result += "\nSocial Security Number: " + socialSecurityNumber;

return result;

}


//---------------------------------------------------------------

// Returns the pay rate for this employee.

//---------------------------------------------------------------

public double pay()

{

return payRate;

}

}

//******************************************************************

// Executive.java Author: Lewis/Loftus

//

// Represents an executive staff member, who can earn a bonus.

//******************************************************************

public class Executive extends Employee

{

private double bonus;


//-----------------------------------------------------------------

// Sets up an executive with the specified information.

//-----------------------------------------------------------------

public Executive (String eName, String eAddress, String ePhone,

String socSecNumber, double rate)

{

super (eName, eAddress, ePhone, socSecNumber, rate);

bonus = 0; // bonus has yet to be awarded

}


//-----------------------------------------------------------------

// Awards the specified bonus to this executive.

//-----------------------------------------------------------------

public void awardBonus (double execBonus)

{

bonus = execBonus;

}


//-----------------------------------------------------------------

// Computes and returns the pay for an executive, which is the

// regular employee payment plus a one-time bonus.

//-----------------------------------------------------------------

public double pay()

{

double payment = super.pay() + bonus;

bonus = 0;

return payment;

}

}

//******************************************************************

// Hourly.java Author: Lewis/Loftus

//

// Represents an employee that gets paid by the hour.

//*******************************************************************

public class Hourly extends Employee

{

private int hoursWorked;


//-----------------------------------------------------------------

// Sets up this hourly employee using the specified information.

//-----------------------------------------------------------------

public Hourly (String eName, String eAddress, String ePhone,

String socSecNumber, double rate)

{

super (eName, eAddress, ePhone, socSecNumber, rate);

hoursWorked = 0;

}


//-----------------------------------------------------------------

// Adds the specified number of hours to this employee's

// accumulated hours.

//-----------------------------------------------------------------

public void addHours (int moreHours)

{

hoursWorked += moreHours;

}


//-----------------------------------------------------------------

// Computes and returns the pay for this hourly employee.

//-----------------------------------------------------------------

public double pay()

{

double payment = payRate * hoursWorked;

hoursWorked = 0;

return payment;

}


//-----------------------------------------------------------------

// Returns information about this hourly employee as a string.

//-----------------------------------------------------------------

public String toString()

{

String result = super.toString();

result += "\nCurrent hours: " + hoursWorked;

return result;

}

}

MY PAYDAY CLASS IS ERRORING LISTED BELOW

//-----------------------------------------------------------------

// Pays all staff members.

//-----------------------------------------------------------------

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 ("-----------------------------------");

}

}

PLEASE LIST ALL STEPS/CODE

Solutions

Expert Solution

/***********************************Firm.java****************************/

//*****************************************************************

// Firm.java Author: Lewis/Loftus

//

// Demonstrates polymorphism via inheritance.

// ****************************************************************

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***************************/

//********************************************************************

// Staff.java Author: Lewis/Loftus

//

// Represents the personnel staff of a particular business.

//********************************************************************

public class Staff

{

   StaffMember[] staffList;

//-----------------------------------------------------------------

// Sets up the list of staff members.

//-----------------------------------------------------------------

   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");

       ((Executive) staffList[0]).awardBonus(500.00);

       ((Hourly) staffList[3]).addHours(40);
       staffList[6] = new Commission("Virat", "680 Fifth Ave.", "555-0691", "987-65-4322", 12.00, 20);
       staffList[7] = new Commission("MS", "234 Main Street", "456-3234", "445-34-4344", 14.75, 15);
       ((Hourly) staffList[6]).addHours(35);
       ((Commission) staffList[6]).addSales(400);
       ((Hourly) staffList[7]).addHours(40);
       ((Commission) staffList[7]).addSales(950);

   }

//-----------------------------------------------------------------

// Pays all staff members.

//-----------------------------------------------------------------

   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**************************************/

//******************************************************************

// StaffMember.java Author: Lewis/Loftus

//

// Represents a generic staff member.

//******************************************************************

abstract public class StaffMember

{

   protected String name;

   protected String address;

   protected String phone;

//---------------------------------------------------------------

// Sets up a staff member using the specified information.

//---------------------------------------------------------------

   public StaffMember(String eName, String eAddress, String ePhone)

   {

       name = eName;

       address = eAddress;

       phone = ePhone;

   }

//---------------------------------------------------------------

// Returns a string including the basic employee information.

//---------------------------------------------------------------

   public String toString()

   {

       String result = "Name: " + name + "\n";

       result += "Address: " + address + "\n";

       result += "Phone: " + phone;

       return result;

   }

//---------------------------------------------------------------

// Derived classes must define the pay method for each type of

// employee.

//---------------------------------------------------------------

   public abstract double pay();

}
/*****************************************************Volunteer.java*******************************/

//******************************************************************

// Volunteer.java Author: Lewis/Loftus

//

// Represents a staff member that works as a volunteer.

//******************************************************************

public class Volunteer extends StaffMember

{

//---------------------------------------------------------------

// Sets up a volunteer using the specified information.

//---------------------------------------------------------------

   public Volunteer(String eName, String eAddress, String ePhone)

   {

       super(eName, eAddress, ePhone);

   }

//---------------------------------------------------------------

// Returns a zero pay value for this volunteer.

//---------------------------------------------------------------

   public double pay()

   {

       return 0.0;

   }

}

/***********************************************Employee.java*****************************/

//******************************************************************

// Employee.java Author: Lewis/Loftus

//

// Represents a general paid employee.

//******************************************************************

public class Employee extends StaffMember

{

   protected String socialSecurityNumber;

   protected double payRate;

//---------------------------------------------------------------

// Sets up an employee with the specified information.

//---------------------------------------------------------------

   public Employee(String eName, String eAddress, String ePhone,

           String socSecNumber, double rate)

   {

       super(eName, eAddress, ePhone);

       socialSecurityNumber = socSecNumber;

       payRate = rate;

   }

//---------------------------------------------------------------

// Returns information about an employee as a string.

//---------------------------------------------------------------

   public String toString()

   {

       String result = super.toString();

       result += "\nSocial Security Number: " + socialSecurityNumber;

       return result;

   }

//---------------------------------------------------------------

// Returns the pay rate for this employee.

//---------------------------------------------------------------

   public double pay()

   {

       return payRate;

   }

}

/*************************************Executive.java***********************************/

//******************************************************************

// Executive.java Author: Lewis/Loftus

//

// Represents an executive staff member, who can earn a bonus.

//******************************************************************

public class Executive extends Employee

{

   private double bonus;

//-----------------------------------------------------------------

// Sets up an executive with the specified information.

//-----------------------------------------------------------------

   public Executive(String eName, String eAddress, String ePhone,

           String socSecNumber, double rate)

   {

       super(eName, eAddress, ePhone, socSecNumber, rate);

       bonus = 0; // bonus has yet to be awarded

   }

//-----------------------------------------------------------------

// Awards the specified bonus to this executive.

//-----------------------------------------------------------------

   public void awardBonus(double execBonus)

   {

       bonus = execBonus;

   }

//-----------------------------------------------------------------

// Computes and returns the pay for an executive, which is the

// regular employee payment plus a one-time bonus.

//-----------------------------------------------------------------

   public double pay()

   {

       double payment = super.pay() + bonus;

       bonus = 0;

       return payment;

   }

}

/*******************************************Hourly.java************************************/

//******************************************************************

// Hourly.java Author: Lewis/Loftus

//

// Represents an employee that gets paid by the hour.

//*******************************************************************

public class Hourly extends Employee

{

   private int hoursWorked;

//-----------------------------------------------------------------

// Sets up this hourly employee using the specified information.

//-----------------------------------------------------------------

   public Hourly(String eName, String eAddress, String ePhone,

           String socSecNumber, double rate)

   {

       super(eName, eAddress, ePhone, socSecNumber, rate);

       hoursWorked = 0;

   }

//-----------------------------------------------------------------

// Adds the specified number of hours to this employee's

// accumulated hours.

//-----------------------------------------------------------------

   public void addHours(int moreHours)

   {

       hoursWorked += moreHours;

   }

//-----------------------------------------------------------------

// Computes and returns the pay for this hourly employee.

//-----------------------------------------------------------------

   public double pay()

   {

       double payment = payRate * hoursWorked;

       hoursWorked = 0;

       return payment;

   }

//-----------------------------------------------------------------

// Returns information about this hourly employee as a string.

//-----------------------------------------------------------------

   public String toString()

   {

       String result = super.toString();

       result += "\nCurrent hours: " + hoursWorked;

       return result;

   }

}
/************************************Commission.java****************************/


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 getTotalSales() {
       return totalSales;
   }

   @Override
   public double pay() {

       double payment = super.pay() + totalSales * (commissionRate / 100);
       return payment;
   }

}
/****************************************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: Virat
Address: 680 Fifth Ave.
Phone: 555-0691
Social Security Number: 987-65-4322
Current hours: 35
Paid: 500.0
------------------------------------
Name: MS
Address: 234 Main Street
Phone: 456-3234
Social Security Number: 445-34-4344
Current hours: 40
Paid: 732.5
------------------------------------

Please let me know if you have any problem or modify the answer, Thanks and do not forget to thumbs up :)


Related Solutions

If an employee makes $1,400 per month and files as single with no withholding allowances, what...
If an employee makes $1,400 per month and files as single with no withholding allowances, what would be his monthly income tax withholding? A. What would it be if an employee makes $2,500 per month and files as single with two withholding allowance?
An employee recently transferred to your Department from another area within the organization. While you strongly...
An employee recently transferred to your Department from another area within the organization. While you strongly encourage your staff to reach out to you with any questions at any time, this particular employee never contacts you. As a matter of fact, if you did not take affirmative steps to check in on the employee, you are not sure you would ever see him. The employee is a strong performer but it has become apparent that he routinely makes mistakes and...
The "F" ears represent another type of locus interaction in a dihybrid cross (different from that...
The "F" ears represent another type of locus interaction in a dihybrid cross (different from that in #5). At one locus the dominant allele (D) would normally produce a purple seed. However, the dominant allele (I) at a second, unlinked locus inhibits formation of purple pigment, and a yellow seed is the result just as though the seed were homozygous recessive at the first locus. Allele "i" does not block pigment formation, and the recessive "d" allele produces a yellow...
In 2018, Ava, an employee, who files single, has AGI of $29,900 and incurred the following...
In 2018, Ava, an employee, who files single, has AGI of $29,900 and incurred the following miscellaneous itemized deductions this year: Union dues and work uniforms: $990 Home office expenses: $2,970 Unreimbursed employee expenses: $1,386 Gambling losses to the extent of gambling winnings: $1,150. What is Ava’s total itemized deduction related to these items?
Please do the following in JAVA. Deposit and Withdrawal Files Use Notepad or another text editor...
Please do the following in JAVA. Deposit and Withdrawal Files Use Notepad or another text editor to create a text file named Deposits.txt. The file should contain the following numbers, one per line: 100.00 124.00 78.92 37.55 Next, create a text file named Withdrawals.txt. The file should contain the following numbers, one per line: 29.88 110.00 27.52 50.00 12.90 The numbers in the Deposits.txt file are the amounts of deposits that were made to a savings account during the month,...
Explain what differentiates one type of cell from another (morphology aside) at the molecular level? What...
Explain what differentiates one type of cell from another (morphology aside) at the molecular level? What role does DNA play in this differentiation process?
_________ occurs if a disgruntled employee convinces another to steal from the company. A) The control environment B) Collusion C) A control activity D) Monitoring
_________ occurs if a disgruntled employee convinces another to steal from the company.A) The control environmentB) CollusionC) A control activityD) Monitoring
Given the same simple Employee-Workson-Project database schema , which contains three files described as follows:
Given the same simple Employee-Workson-Project database schema , which contains three files described as follows:Emp (eid : integer, ename : string, age : integer, salary: real)Workson (eid : integer, pid : integer, hours : integer)Project (pid : integer, pname : string, budget : real, managerid : integer)Note : eid, ename, age and salary are the employee id, name, age and salary respectively. Also, hours is the number of hours worked by employee on a project. The rest of the attributes...
This lab will explore the password files. a) Open a terminal window and type less /etc/passwd....
This lab will explore the password files. a) Open a terminal window and type less /etc/passwd. Which line describes the root account? b) Type ls –l /etc/passwd. Who is the owner of the passwd file? What are the permissions? c) Type less /etc/shadow. What is in the password filed for most of these accounts? d) Type ls –l /etc/shadow. Who owns this file and how do the permissions differ from the /etc/passwd file? e) Type cat /etc/default/useradd. What is the...
1.A fellow servant is an employee who has the same status as another worker and works...
1.A fellow servant is an employee who has the same status as another worker and works with that employee. True False 2.Under Social Security, the family of a worker who dies while fully insured at the time of death has a right to survivors’ benefits. True False 3.The theory of respondeat superior states that an employer is liable for personal injury committed outside the course of employment. Group of answer choices True False 4.The common law provides that an employer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT