Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Payroll Design a PayRoll class that is an abstract data type for payroll. It has

ID: 3732920 • Letter: P

Question

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 argument(s) to the function,

member functions to retrieve the data from each of the member variables,

an input function that reads the values of number of hours and hourly pay rate,

an output function that outputs the value of all data members for an employee,

void function to calculate the total pay for a week.

The input and output functions will each have one formal parameter for the stream.

Write a program with an array of seven PayRoll objects. The program should ask the user for the number of hours each employee has worked and will then display the amount of gross pay each has earned.

Input Validation: Do not accept values greater than 60 for the number of hours

worked.


Sample Output:

Enter the hours worked and pay rate for 7 employees:

Employee #1 pay rate: 15

Employee #1 hours worked: 40

Employee #2 pay rate: 20

Employee #2 hours worked: 35

Employee #3 pay rate: 18

Employee #3 hours worked: 40

Employee #4 pay rate: 22

Employee #4 hours worked: 38

Employee #5 pay rate: 15

Employee #5 hours worked: 40

Employee #6 pay rate: 20

Employee #6 hours worked: 32

Employee #7 pay rate: 22

Employee #7 hours worked: 40

Total pay:

Employee #1: 600.00

Employee #2: 700.00

Employee #3: 720.00

Employee #4: 836.00

Employee #5: 600.00

Employee #6: 640.00

Employee #7: 880.00

Press any key to continue . . .


Explanation / Answer

Answer.)

Code :

package com.example.myExample;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

//Payroll Class
public class Payroll {
private int hourlyPayRate;
private int numberOfHoursWorked;
private int totalPayPerWeek;
//Default constructor setting the parameters to 0.
public Payroll() {
super();
this.hourlyPayRate=0;
this.numberOfHoursWorked=0;
this.totalPayPerWeek=0;
}
//Parameterized Constructor setting the parameters based on arguments
public Payroll(int hourlyPayRate, int numberOfHoursWorked,
int totalPayPerWeek) {
super();
this.hourlyPayRate = hourlyPayRate;
this.numberOfHoursWorked = numberOfHoursWorked;
this.totalPayPerWeek = totalPayPerWeek;
}
//Getter method for hourly rate
public int getHourlyPayRate() {
return hourlyPayRate;
}
//Setter method for hourly rate
public void setHourlyPayRate(int hourlyPayRate) {
this.hourlyPayRate = hourlyPayRate;
}
//Getter method for number of hours
public int getNumberOfHoursWorked() {
return numberOfHoursWorked;
}
//Setter method for number of hours
public void setNumberOfHoursWorked(int numberOfHoursWorked) {
this.numberOfHoursWorked = numberOfHoursWorked;
}
//Getter method for total pay per week
public int getTotalPayPerWeek() {
return totalPayPerWeek;
}
//Setter method for total pay per week
public void setTotalPayPerWeek(int totalPayPerWeek) {
this.totalPayPerWeek = totalPayPerWeek;
}
//Input employee details using Scanner class
public void inputDetails(int i){

Scanner sc=new Scanner(System.in);
System.out.println("Employee #"+i+" pay rate: ");
int rate=sc.nextInt();
System.out.println("Employee #"+i+" hours worked: ");
int hours=sc.nextInt();
while(hours>60){
System.out.println("Employee #"+i+" hours worked: ");
hours=sc.nextInt();
}
//Setting the input values to the class instance variables
this.hourlyPayRate=rate;
this.numberOfHoursWorked=hours;

}
//Print the employee detais
public void outputDetails(int i){
System.out.println("Employee #"+i+": "+calculateTotalPayForWeek());
}
//Calculating total pay for week
private double calculateTotalPayForWeek() {
return this.hourlyPayRate*this.numberOfHoursWorked;
}
//Driver method to instantiate Payroll objects, input, calculate and display output
public static void main(String[] args) {
List<Payroll> employeeList=new ArrayList<Payroll>();
for(int i=1;i<=7;i++){
Payroll payroll=new Payroll();
payroll.inputDetails(i);
employeeList.add(payroll);
//New lines created as per Sample output
System.out.println();
System.out.println();
}
int i=1;
//Iterating the list to display for each Employee
for(Payroll payroll : employeeList){
payroll.outputDetails(i);
i++;
}
}
}

Output :

Employee #1 pay rate:
15
Employee #1 hours worked:
40


Employee #2 pay rate:
20
Employee #2 hours worked:
35


Employee #3 pay rate:
18
Employee #3 hours worked:
40


Employee #4 pay rate:
22
Employee #4 hours worked:
38


Employee #5 pay rate:
15
Employee #5 hours worked:
40


Employee #6 pay rate:
20
Employee #6 hours worked:
32


Employee #7 pay rate:
22
Employee #7 hours worked:
40


Employee #1: 600.0
Employee #2: 700.0
Employee #3: 720.0
Employee #4: 836.0
Employee #5: 600.0
Employee #6: 640.0
Employee #7: 880.0

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote