I need help with this program

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

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.

This is what I've gotten so far

``````import java.util.Scanner; // imports the Scanner class

public class PayrollTracker
{
private final int NUM_EMPLOYEES = 7; // constant for the number of employee ID's
private int[] employeeId;
private int[] hours;
private double[] payRate;
private double[] wages;

public PayrollTracker()
{
final int NUM_EMPLOYEES = 7;
int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489}; // employee id in an array
int[] hours = new int[NUM_EMPLOYEES]; // int array for 7 employees hours
double[] payRate = new double[NUM_EMPLOYEES]; // double array for payrate of 7 employees
double[] wages = new double[NUM_EMPLOYEES]; // double array for the wages of 7 employees
}

public int getEmployeeId(int id)
{
return employeeId[id];
}
public double getWages(int id)
{
return wages[id];
}
public void setPayRate(int id, double pr)
{
payRate[id]=pr;
}
public void setHours(int id, int h)
{
hours[id]=h;
}
public void calcGrossWages()
{
for(int i = 0; i < NUM_EMPLOYEES; i++)
{
wages[i] = hours[i]*payRate[i];
}
}

public static void main(String[] args)
{
int hrs;
double payr;

PayrollTracker pr = new PayrollTracker();
Scanner keyboard = new Scanner(System.in);

for(int i = 0; i < 7; i++)
{
System.out.println(pr.getEmployeeId(i));
System.out.println("Enter the amount of hours the employee worked ");
hrs = keyboard.nextInt();
pr.setHours(i, hrs);
System.out.println("Enter the pay rate at which the employee was paid ");
payr = keyboard.nextDouble();
pr.setPayRate(i, payr);
}

pr.calcGrossWages();

for(int i = 0; i < 7; i++)
{
System.out.println(pr.getEmployeeId(i) + " - " + pr.getWages(i));
}
}

}``````

Earlier, It compiled and run when I didnt have a constructor. I just initialized the fields when I made them. When I switched to the constructor and compiled it, it compiled fine, but when I try to run it, it gives me an exception in thread "main" error. Can someone help me?

2
Contributors
1
4
Views
8 Years
Discussion Span
Last Post by Ezzaral

Please post the exact exception info when you are referring to one. We can't quite see over your shoulder from here.

One thing to note first. You are declaring new variables and initializing them in the constructor

``````public PayrollTracker()
{
final int NUM_EMPLOYEES = 7;
int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489}; // employee id in an array
int[] hours = new int[NUM_EMPLOYEES]; // int array for 7 employees hours
double[] payRate = new double[NUM_EMPLOYEES]; // double array for payrate of 7 employees
double[] wages = new double[NUM_EMPLOYEES]; // double array for the wages of 7 employees
}``````

Drop the type info from the front of those if you want to initialize the variables you've declared at the class level.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.