Write a payroll class that uses the following fields as arrays empid int[] which
ID: 3825526 • Letter: W
Question
Write a payroll class that uses the following fields as arrays empid int[] which contains the following 6 id numbers: [1234, 2345, 3456, 4567, 5678, 6789] hours int[] rate double[] wages double[] REMEMBER that a subscript of [0] in each array points to the first employee, etc. Setup a field: final int EMPLOYEES = 6; Required methods for the Payroll class setHoursAt (i, hrs)void setRateAt (i, rt)void setWagesAt (i, wg)void getEmpidAt (i) returns int getWagesAt (i) returns double [optional method-not required] *getGrossPay(i) return double *You may prefer to use the above getGrossPayAt method that accepts a subscript and then returns the result of rate * hours Finally, write a PayrollDemo 'demotest' program that: a. Creates an instance of the Payroll Class b. Displays each empid number and prompts the user to enter the hours and rate for that employee c. Sends employee's hours and rate and wages (rate * hours) to their arrays in the Payroll Class d. Prints to screen each empid number along with amount that employee earned.Explanation / Answer
Here is the code for Payroll.java:
class Payroll
{
int[] empid = {1234, 2345, 3456, 4567, 6789};
int[] hours;
double[] rate;
double[] wages;
public Payroll()
{
hours = new int[6];
rate = new double[6];
wages = new double[6];
}
public void setHoursAt(int i, int hrs) { hours[i] = hrs; }
public void setRateAt(int i, double rt) { rate[i] = rt; }
public void setWagesAt(int i, double wg) { wages[i] = wg; }
public int getEmpidAt(int i) { return empid[i]; }
public double getWagesAt(int i) { return wages[i]; }
}
Here is the code for PayrollDemo.java:
import java.util.*;
class PayrollDemo
{
public static void main(String[] args)
{
Payroll payrolls = new Payroll();
Scanner sc = new Scanner(System.in);
int workHours;
double payRate;
for(int i = 0; i < 6; i++)
{
System.out.print("Enter the work hours of employee with id #" + payrolls.getEmpidAt(i) + ": ");
workHours = sc.nextInt();
payrolls.setHoursAt(i, workHours);
System.out.print("Enter the pay rate of employee with id #" + payrolls.getEmpidAt(i) + ": ");
payRate = sc.nextDouble();
payrolls.setRateAt(i, payRate);
payrolls.setWagesAt(i, workHours * payRate);
}
for(int i = 0; i < 6; i++)
System.out.println("Employee with id #" + payrolls.getEmpidAt(i) + " earned $" + payrolls.getWagesAt(i));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.