Write a Payroll class that uses the following arrays as fields: * employeeId. An
ID: 3737052 • Letter: W
Question
Write a Payroll class that uses the following arrays as fields:
* employeeId. An array of seven integers to hold employee identification numbers. The array should be initialized with the following numbers: 5658845 4520125 7895122 8777541 8451277 1302850 7580489
* hours. An array of seven integers to hold the number of hours worked by each employee
* payRate. An array of seven doubles to hold each employee’s hourly pay rate
* wages. An array of seven doubles to hold each employee’s gross wages
The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the
employee whose identification number is stored in element 0 of the employeeId array. That same employee’s pay rate should be stored in element 0 of the payRate array.
The class should have a method that accepts an employee’s identification number as an argument and returns the
gross pay for that employee. Demonstrate the class in a complete program that displays each employee number and asks the user to enter that employee’s hours and pay rate. It should then display each employee’s identification number and gross wages.
Input Validation: Do not accept negative values for hours or numbers less than 6.00 for pay rate.
____________________________________________________________________________
Should get something like this
Also should be able to do this:
Please help!
Explanation / Answer
Payroll.java
import java.util.*;
public class Payroll {
public int employeeId[];
public double hours[];
public double payRate[];
public double wages[];
Payroll() {
employeeId = new int[] { 5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489 };
hours = new double[7];
payRate = new double[7];
wages = new double[7];
}
double gross_pay(int empId) {
int i;
for (i = 0; i < 7; i++) {
if (employeeId[i] == empId) {
return (double) hours[i] * payRate[i];
}
}
return 0.0;
}
}
PayrollDriver.java
import java.util.*;
public class PayrollDriver {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
Payroll emp = new Payroll();
int i;
for (i = 0; i < 7; i++) {
while (true) {
System.out.println("Please enter hours for employee ID: " + emp.employeeId[i]);
emp.hours[i] = in.nextDouble();
if (emp.hours[i] >= 0)
break;
System.out.println("Hours cannot be negative ");
}
while (true) {
System.out.println("Please enter pay rate for employee ID: " + emp.employeeId[i]);
emp.payRate[i] = in.nextDouble();
if (emp.payRate[i] >= 6)
break;
System.out.println("Employee pay rate cannot be less than $6.00/hour");
}
}
for (i = 0; i < 7; i++)
{
emp.wages[i] = emp.gross_pay(emp.employeeId[i]);
}
System.out.println("=================================");
System.out.println("Employee ID Gross Wages");
System.out.println("=================================");
for (i = 0; i < 7; i++) {
System.out.printf("%12d %15.2f ", emp.employeeId[i], emp.wages[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.