2. Payroll Classes Write a Payroll class that uses the following arrays as field
ID: 3807486 • Letter: 2
Question
2. Payroll Classes 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 double s to hold each employee’s hourly pay rate wages . An array of seven double s 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.
Explanation / Answer
Payroll.java
import java.util.Scanner;
public class Payroll {
//Declaring arrays
private int ids[];
private int hours[];
private double hourlyPayRate[];
//Zero argumented constructor
public Payroll() {
super();
this.hours =new int[7];
this.hourlyPayRate = new double[7];
}
//Getters and setters
public int[] getHours() {
return hours;
}
public void setHours(int[] hours) {
this.hours = hours;
}
public double[] getHourlyPayRate() {
return hourlyPayRate;
}
public void setHourlyPayRate(double[] hourlyPayRate) {
this.hourlyPayRate = hourlyPayRate;
}
//This method will calculate the gross pay of each employee
public double[] calGrossPay(int ids[])
{
Scanner sc=new Scanner(System.in);
int i=0;
int hour;
double pay;
double wages[]=new double[7];
while(i<ids.length)
{
//Getting the no of hours worked by each employee
System.out.print("Enter no of hours worked for Employee "+ids[i]+":");
hour=sc.nextInt();
if(hour<0)
{
System.out.println("** Invalid.Must be Positive number **");
continue;
}
else
{
while(true)
{
//Getting the pay rate
System.out.print("Enter payrate of Employee "+ids[i]+":");
pay=sc.nextDouble();
if(pay<6.0)
{
System.out.println("** Invalid.Must be greater than or equal to 6.0 **");
continue;
}
else
{
hours[i]=hour;
hourlyPayRate[i]=pay;
break;
}
}
wages[i]=hours[i]*hourlyPayRate[i];
System.out.println();
i++;
}
}
return wages;
}
}
____________________
Test.java
public class Test {
public static void main(String[] args) {
double wages[];
//Creating and initializing an integer array of employee ids
int enos[]={5658845,4520125,7895122,8777541,8451277,1302850,7580489};
//Creating an Payroll object
Payroll pay=new Payroll();
//calling the method on the payroll class object
wages=pay.calGrossPay(enos);
//Displaying the Id and Gross wages of each employee
System.out.println("Id Gross Wages");
for(int i=0;i<wages.length;i++)
{
System.out.println(enos[i]+" "+wages[i]);
}
}
}
______________________
Output:
Enter no of hours worked for Employee 5658845:-40
** Invalid.Must be Positive number **
Enter no of hours worked for Employee 5658845:40
Enter payrate of Employee 5658845:5.0
** Invalid.Must be greater than or equal to 6.0 **
Enter payrate of Employee 5658845:11.0
Enter no of hours worked for Employee 4520125:38
Enter payrate of Employee 4520125:9.0
Enter no of hours worked for Employee 7895122:32
Enter payrate of Employee 7895122:7.5
Enter no of hours worked for Employee 8777541:22
Enter payrate of Employee 8777541:8.0
Enter no of hours worked for Employee 8451277:43
Enter payrate of Employee 8451277:8.5
Enter no of hours worked for Employee 1302850:34
Enter payrate of Employee 1302850:9.5
Enter no of hours worked for Employee 7580489:33
Enter payrate of Employee 7580489:6.5
Id Gross Wages
5658845 440.0
4520125 342.0
7895122 240.0
8777541 176.0
8451277 365.5
1302850 323.0
7580489 214.5
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.