In: Computer Science
JAVA
Create a new Class Payroll that contains our ClassEmployee.
we are adding a copy constructor to our Employee Class
we are adding a toString() method to our Employee Class
our company has grown so we can no longer avoid deducting taxes from our workers, so we moved the PrintPayStub() method to our newPayroll Class.
we will need 2 static members to our Payroll Class to help us compute State and Federal tax deductions from our Employee Paychecks
Delving deeper into the concepts of Object Oriented Programming - code re-use and class relationships
1). ANSWER :
GIVENTHAT :
/*********************************Worker.java*******************************/
public class Worker {
// Member variables
private String name;
private String employeeId;
private boolean dayShift;
private double HourlyPay;
// The default constructor
public Worker() {
this.name = "";
this.employeeId = "";
this.dayShift = false;
this.HourlyPay = 0.0;
}
public Worker(String name, String employeeId, boolean dayShift,
double hourlyPay) {
super();
this.name = name;
this.employeeId = employeeId;
this.dayShift = dayShift;
HourlyPay = hourlyPay;
}
public Worker(Worker w) {
this.name = w.name;
this.employeeId = w.employeeId;
this.dayShift = w.dayShift;
this.HourlyPay = w.HourlyPay;
}
// This method sets the name of the worker
public void setName(String n) {
this.name = n;
}
// This method sets the employee Id
public void setemployeeId(String i) {
this.employeeId = i;
}
// This method sets the day shift
public void setdayShift(boolean s) {
this.dayShift = s;
}
// This method sets the hourly pay
public void setHourlyPay(double p) {
this.HourlyPay = p;
}
// This method returns the name of the worker
public String getname() {
return this.name;
}
// This method returns the employee Id
public String getemployeeId() {
return this.employeeId;
}
// This method returns if the worker is in day shift
public boolean getdayShift() {
return this.dayShift;
}
// This method returns the hourly pay
public double getHourlyPay() {
return this.HourlyPay;
}
// This method calculates the overtime pay
public double calculateOvertimePay(int hours) {
return (hours - 40) * 1.5 * HourlyPay;
}
// This method calculates and returns regular pay
public double calculateRegPay(int hours) {
return HourlyPay * hours;
}
@Override
public String toString() {
return "Worker name=" + name + ", employeeId=" + employeeId + ", dayShift=" + dayShift + ", HourlyPay="
+ HourlyPay;
}
}
/**************************************Payroll.java******************************/
public class Payroll {
private Worker w;
private int dependents;
private static final double STATE_TAX = 0.10;
private static final double FED_TAX = 0.05;
public Payroll() {
this.w = new Worker();
this.dependents = 0;
}
public Payroll(String name, String id, double pay, boolean s, int
d) {
this.w = new Worker(name, id, s, pay);
this.dependents = d;
}
public Payroll(Worker w, int dependents) {
super();
this.w = w;
this.dependents = dependents;
}
public Worker getW() {
return w;
}
public void setW(Worker w) {
this.w = w;
}
public int getDependents() {
return dependents;
}
public void setDependents(int dependents) {
this.dependents = dependents;
}
public double calculateStateTax(int hours) {
return (w.calculateRegPay(hours) + w.calculateOvertimePay(hours)) *
STATE_TAX;
}
public double calculateFedTax(int hours) {
return (w.calculateRegPay(hours) + w.calculateOvertimePay(hours)) *
FED_TAX;
}
public void PrintPayStub(int hours) {
double regPay, overPay;
if (hours <= 40) {
// get the regular pay
regPay = w.calculateRegPay(hours) - calculateStateTax(hours) - calculateFedTax(hours)
+ (dependents * .03 * w.calculateRegPay(hours));
overPay = 0;
} else {
// get the overtime pay
regPay = 40 * w.getHourlyPay() - calculateStateTax(hours) - calculateFedTax(hours);
overPay = w.calculateOvertimePay(hours) - calculateStateTax(hours) - calculateFedTax(hours);
}
// display the pay Stub
System.out.println("\nName: \t" + w.getname());
System.out.println("Employee ID: " + w.getemployeeId());
System.out.print("Shift: ");
if (w.getdayShift())
System.out.println("\t\t day");
else
System.out.println("\t\tnight");
System.out.printf("Hourly Pay: $ %7.2f", w.getHourlyPay());
System.out.printf("\nRegular Pay: $ %.2f", regPay);
System.out.printf("\nOvertime Pay: $ %4.2f", overPay);
System.out.printf("\nGross Pay: $ %8.2f\n", (regPay + overPay));
}
}
/**************************************WorkerPayrollTest.java*****************************************/
import java.util.Scanner;
public class WorkerPayrollTest {
public static void main(String[] args) {
Worker[] workers = new Worker[5];
Payroll[] payrolls = new Payroll[5];
Scanner scan = new Scanner(System.in);
for (int i = 0; i < workers.length; i++) {
System.out.print("Enter worker name: ");
String name = scan.nextLine();
System.out.print("Enter id: ");
String id = scan.nextLine();
System.out.print("Hourly Pay: ");
double pay = scan.nextDouble();
scan.nextLine();
System.out.print("Is day shift: ");
boolean shift = scan.nextBoolean();
scan.nextLine();
workers[i] = new Worker(name, id, shift, pay);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString());
System.out.print("Enter dependent: ");
int dependents = scan.nextInt();
scan.nextLine();
payrolls[i] = new Payroll(workers[i], dependents);
}
for (int i = 0; i < payrolls.length; i++) {
System.out.println(workers[i].toString()+" dependent: "+payrolls[i].getDependents());
System.out.print("Enter hours worked for "+workers[i].getname()+": ");
int hours = scan.nextInt();
scan.nextLine();
payrolls[i].PrintPayStub(hours);
}
}
}
/***********************************output******************************/
Enter worker name: Virat
Enter id: 1
Hourly Pay: 100
Is day shift: true
Enter worker name: Jugal
Enter id: 2
Hourly Pay: 200
Is day shift: true
Enter worker name: Ajay
Enter id: 3
Hourly Pay: 120
Is day shift: false
Enter worker name: Vijay
Enter id: 4
Hourly Pay: 130
Is day shift: true
Enter worker name: MS
Enter id: 5
Hourly Pay: 150
Is day shift: false
Worker name=Virat, employeeId=1, dayShift=true, HourlyPay=100.0
Enter dependent: 3
Worker name=Jugal, employeeId=2, dayShift=true, HourlyPay=200.0
Enter dependent: 4
Worker name=Ajay, employeeId=3, dayShift=false, HourlyPay=120.0
Enter dependent: 5
Worker name=Vijay, employeeId=4, dayShift=true, HourlyPay=130.0
Enter dependent: 6
Worker name=MS, employeeId=5, dayShift=false, HourlyPay=150.0
Enter dependent: 7
Worker name=Virat, employeeId=1, dayShift=true, HourlyPay=100.0 dependent: 3
Enter hours worked for Virat: 40
Name: Virat
Employee ID: 1
Shift: day
Hourly Pay: $ 100.00
Regular Pay: $ 3760.00
Overtime Pay: $ 0.00
Gross Pay: $ 3760.00
Worker name=Jugal, employeeId=2, dayShift=true, HourlyPay=200.0 dependent: 4
Enter hours worked for Jugal: 50
Name: Jugal
Employee ID: 2
Shift: day
Hourly Pay: $ 200.00
Regular Pay: $ 6050.00
Overtime Pay: $ 1050.00
Gross Pay: $ 7100.00
Worker name=Ajay, employeeId=3, dayShift=false, HourlyPay=120.0 dependent: 5
Enter hours worked for Ajay: 60
Name: Ajay
Employee ID: 3
Shift: night
Hourly Pay: $ 120.00
Regular Pay: $ 3180.00
Overtime Pay: $ 1980.00
Gross Pay: $ 5160.00
Worker name=Vijay, employeeId=4, dayShift=true, HourlyPay=130.0 dependent: 6
Enter hours worked for Vijay: 40
Name: Vijay
Employee ID: 4
Shift: day
Hourly Pay: $ 130.00
Regular Pay: $ 5356.00
Overtime Pay: $ 0.00
Gross Pay: $ 5356.00
Worker name=MS, employeeId=5, dayShift=false, HourlyPay=150.0 dependent: 7
Enter hours worked for MS: 58
Name: MS
Employee ID: 5
Shift: night
Hourly Pay: $ 150.00
Regular Pay: $ 4087.50
Overtime Pay: $ 2137.50
Gross Pay: $ 6225.00