Write a Payroll class that uses the following arrays as fields: * employeeId. An
ID: 3873368 • 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.
In addition to the appropriate accessor and mutator methods, 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.(I NEED JAVA CODE)
output should be like this
test 2
Explanation / Answer
import java.io.*;
import java.util.*;
class Payroll{
private int[] id;
private int[] hr;
private double[] payrate;
private double[] wages;
public Payroll(){
id = new int[7];
id[0] = 5658845;
id[1] = 4520125;
id[2] = 7895122;
id[3] = 8777541;
id[4] = 8451277;
id[5] = 1302850;
id[6] = 7580489;
hr = new int[7];
payrate = new double[7];
wages = new double[7];
}
public int[] getId(){
return id;
}
public double[] getWages(){
return wages;
}
public void setHours(int[] a){
for (int i = 0; i<7; i++)
hr[i] = a[i];
}
public void setPayrate(double[] a){
for (int i = 0; i<7; i++)
payrate[i] = a[i];
}
public void cal(){
for (int i = 0; i<7; i++)
wages[i] = hr[i] * payrate[i];
}
public double gross(int id1){
for (int i = 0 ; i<7; i++){
if (id1 == id[i]){
return wages[i];
}
}
System.out.println("Employee id not found ");
return -1;
}
}
public class DemoPayroll{
public static void main(String[] args){
Payroll p = new Payroll();
Scanner sc = new Scanner(System.in);
int[] id = new int[7];
id = p.getId();
int[] hr = new int[7];
double[] payrate = new double[7];
double[] wages = new double[7];
for (int i = 0; i<7; i++){
System.out.print("Please enter hours for employee ID " + id[i] + ":");
hr[i] = sc.nextInt();
if (hr[i] < 0){
do {
System.out.print("Number of hours should not be -ve.Please enter hours for employee ID " + id[i] + ":");
hr[i] = sc.nextInt();
} while (hr[i] < 0);
}
System.out.print("Please enter pay rate for employee ID " + id[i] + ":");
payrate[i] = sc.nextDouble();
if (payrate[i] < 6){
do {
System.out.print("Incorrect value (payrate should be more than> 6). Please pay rate for employee ID " + id[i] + ":");
payrate[i] = sc.nextDouble();
} while (payrate[i] < 6);
}
}
p.setPayrate(payrate);
p.setHours(hr);
p.cal();
System.out.println("====================================== ");
System.out.println("Employee ID Gross Wages ");
System.out.println("====================================== ");
wages = p.getWages();
for (int i = 0; i<7; i++){
System.out.println(id[i] + " $" + String.format("%.2f",wages[i]));
}
System.out.println("Gross of id 5658845 is " + String.format("%.2f",p.gross(5658845)));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.