Question

In: Computer Science

Create a new Class Payroll that contains our ClassEmployee.

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

Solutions

Expert Solution

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


Related Solutions

Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
where can you create a new payroll schedule?
where can you create a new payroll schedule?
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
JAVA Program: You are a developer for a company that wants to create a new payroll...
JAVA Program: You are a developer for a company that wants to create a new payroll system. Write a program that can be used to calculate an employee’s take home pay. Example - rmckanryProgram1. Creating a flow chart or using stump code to outline your program first may help. Include the following features in the program: Enter employee’s name Enter employee’s base hourly pay Enter the number of hours worked for the week Error if the hours worked exceeds 168...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type...
Week 3 In-Class Exercise C++ Payroll Design a PayRoll class that is an abstract data type for payroll. It has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Your class must include the following member functions: a constructor to set the hours and pay rate as arguments, a default constructor to set data members to 0, member functions to set each of the member variables to values given as an...
You may create Payroll Schedules in Payroll Settings in QBO.
You may create Payroll Schedules in Payroll Settings in QBO.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT