Write a (JAVA) Payroll class that uses the following arrays as fields: * employe
ID: 3737539 • Letter: W
Question
Write a (JAVA) 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.
Explanation / Answer
Payroll.java
import java.text.DecimalFormat;
public class Payroll {
//Declaring constant
public final int SIZE=7;
//Declaring arrays
private int employeeID[]={5658845, 4520125, 7895122, 8777541, 8451277, 1302850,7580489};
private int hours[];
private double payRates[];
//Zero argumented cosntructor
public Payroll() {
//employeeID=
hours=new int[SIZE];
payRates=new double[SIZE];
}
//Setters and getters
public int[] getEmployeeID() {
return employeeID;
}
public int[] getHours() {
return hours;
}
public void setHours(int[] hours) {
this.hours = hours;
}
public double[] getPayRates() {
return payRates;
}
public void setPayRates(double[] payRates) {
this.payRates =payRates;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat(".00");
System.out.println("PAYROLL DATA");
System.out.println("-----");
for(int i=0;i<employeeID.length;i++)
{
System.out.println("Employee ID:"+employeeID[i]);
System.out.println("Gross pay: $"+df.format(hours[i]*payRates[i]));
System.out.println();
}
return "";
}
double calGrossPayOfAnEmployee(int id)
{
double sal=-1;
for(int i=0;i<employeeID.length;i++)
{
if(employeeID[i]==id)
{
sal=hours[i]*payRates[i];
}
}
return sal;
}
}
__________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int hours[]=new int[7];
double payrate[]=new double[7];
int hr;
double payR;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
Payroll p=new Payroll();
for(int i=0;i<hours.length;)
{
while(true)
{
System.out.print("Enter no of hours worked by Employee#"+p.getEmployeeID()[i]+":");
hr=sc.nextInt();
if(hr<0)
{
System.out.println("Invalid.Must be Positive:");
continue;
}
else
{
hours[i]=hr;
break;
}
}
while(true)
{
System.out.print("Enter hourly payrate for Employee#"+p.getEmployeeID()[i]+":$");
payR=sc.nextDouble();
if(payR<6)
{
System.out.println("Invalid.Must be greater than 6.0:");
continue;
}
else
{
payrate[i]=payR;
System.out.println();
break;
}
}
i++;
}
p.setHours(hours);
p.setPayRates(payrate);
System.out.println(p);
}
}
______________________
Output:
Enter no of hours worked by Employee#5658845:39
Enter hourly payrate for Employee#5658845:-10
Invalid.Must be greater than 6.0:
Enter hourly payrate for Employee#5658845:12
Enter no of hours worked by Employee#4520125:22
Enter hourly payrate for Employee#4520125:14
Enter no of hours worked by Employee#7895122:34
Enter hourly payrate for Employee#7895122:11
Enter no of hours worked by Employee#8777541:34
Enter hourly payrate for Employee#8777541:12
Enter no of hours worked by Employee#8451277:37
Enter hourly payrate for Employee#8451277:8
Enter no of hours worked by Employee#1302850:32
Enter hourly payrate for Employee#1302850:14
Enter no of hours worked by Employee#7580489:23
Enter hourly payrate for Employee#7580489:11
PAYROLL DATA
-----
Employee ID:5658845
Gross pay: $468
Employee ID:4520125
Gross pay: $308
Employee ID:7895122
Gross pay: $374
Employee ID:8777541
Gross pay: $408
Employee ID:8451277
Gross pay: $296
Employee ID:1302850
Gross pay: $448
Employee ID:7580489
Gross pay: $253
______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.