PLEASE WRITE IN JAVA Problem 2 Modify the Firm example from this chapter such th
ID: 3706020 • Letter: P
Question
PLEASE WRITE IN JAVA
Problem 2
Modify the Firm example from this chapter such that all employees can be given different vacation options depending on their classification. Provide a method called vacation that returns the number of vacation days a person has. Give all employees a standard number of vacation days (14), then override the method in the various employee classes as appropriate. Modify the driver program to demonstrate this new functionality.
The Firm example and its components can be found at the link below. Thank you
http://www.chegg.com/homework-help/modify-firm-example-chapter-accomplishes-polymorphism-using-chapter-10-problem-1pp-solution-9780133594959-exc
Explanation / Answer
//Code to run
//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();
//call method to print the vacaation days
personnel.printVacationDays();
}
}
----------------------------------------------------------------------------
//Staff.java
public class Staff
{
private StaffMember[] staffList;
public Staff ()
{
staffList = new StaffMember[4];
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);
}
//-----------------------------------------------------------------
// Print all staff members including vacation days
//-----------------------------------------------------------------
public void printVacationDays ()
{
System.out.println("Get vacation Days : ");
for (int count=0; count < staffList.length; count++)
{
System.out.println(staffList[count]);
System.out.println ("Vacation Days : "+staffList[count].getVacationDays());
System.out.println ("-----------------------------------");
}
}
}
----------------------------------------------------------------------------
//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;
}
/*Override getVacationDays method*/
public double getVacationDays() {
return VACATION_DAYS;
}
}
----------------------------------------------------------------------------
//StaffMember.java
abstract public class StaffMember
{
protected int VACATION_DAYS=12;
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();
//create an abstract method
public abstract double getVacationDays();
}
----------------------------------------------------------------------------
//Executive.java
public class Executive extends Employee
{
private double bonus;
private int EXTRA_VAC_DAYS=5;
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
}
public void awardBonus (double execBonus)
{
bonus = execBonus;
}
public double pay()
{
double payment = super.pay() + bonus;
bonus = 0;
return payment;
}
/*Override getVacationDays method*/
public double getVacationDays() {
return super.getVacationDays()+EXTRA_VAC_DAYS;
}
}
----------------------------------------------------------------------------
//Hourly.java
public class Hourly extends Employee
{
private int hoursWorked;
private int EXTRA_VAC_DAYS=2;
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;
}
/*Override getVacationDays method*/
public double getVacationDays() {
return super.getVacationDays()+EXTRA_VAC_DAYS;
}
}
----------------------------------------------------------------------------
Sample Output:
Get vacation Days :
Name: Sam
Address: 123 Main Line
Phone: 555-0469
Social Security Number: 123-45-6789
Vacation Days : 17.0
-----------------------------------
Name: Carla
Address: 456 Off Line
Phone: 555-0101
Social Security Number: 987-65-4321
Vacation Days : 12.0
-----------------------------------
Name: Woody
Address: 789 Off Rocker
Phone: 555-0000
Social Security Number: 010-20-3040
Vacation Days : 12.0
-----------------------------------
Name: Diane
Address: 678 Fifth Ave.
Phone: 555-0690
Social Security Number: 958-47-3625
Current hours: 0
Vacation Days : 14.0
-----------------------------------
Note :some of classes are taken for references only
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.