Julio Rosario Julio Rosario - 3 months ago 6
Java Question

I'm basically finish with the class, but I still get an error, every time I call the getWage from the main class it display 0.00?

I really need help, I been trying to learn java on my own, right now i'm in arrays, I finished the chapter in 3 days. I guess that's one the reasons i'm still making mistakes. any recommendation how to fix this error would be awesome. In addition, any advice on how to improve this class would helpful as well and thanks.

package chapter7;

public class PayRoll {

private int[] employeeid = {5658845, //Employee identification numbers.
4520125,
7895122,
8777541,
8451277,
1302850,
7580489};

private int[] hours = new int[7]; //Numbers of hours worked by each employee.
private double[] payRate = new double[7]; //each employee's hourly pay rate.
private double[] wages = new double[7]; //each employee's gross pay.

public void setHours(int[] h){

for(int index = 0; index<h.length; index++){
hours[index] = h[index];
}
}

/**
* The setPayRate method copy
* an array's elements and stored
* it in the payRate array field.
* @param p Each employee's hourly
* pay rate.
*/

public void setPayRate(double[] p){

for(int index = 0; index<p.length; index++){
payRate[index] = p[index];
}
}

/**
* The setWages method copy
* an array's elements and stored
* it in the wages array field.
*/

public void setWages(){

for(int n = 0; n<wages.length; n++){
wages[n] = (payRate[n]*hours[n]);
}
}

/**
* The getID method find the employee's
* subscript.
* @param id An employee's id.
* @return An employee subscript.
*/

public int getID(int id){

int emp = 0;

for(int index = 0; index<employeeid.length; index++)
{
if(employeeid[index] == id){
emp = index;
}
}
return emp;
}

/**
* The getHours method find
* the numbers of hours worked by
* an employee.
* @return An employee's worked hours.
*/

public int getHours(){

return hours[getID(0)];
}

/**
* The getPayRate method find
* the pay rate of an employee.
* @return An employee's payRate.
*/

public double getPayRate(){
return payRate[getID(0)];
}

/**
* The getSize method
* @return The numbers of employees.
*/

public int getSize(){
return employeeid.length;
}

/**
* The getWage
* @return An employee's gross wage.
*/

public double getWage(){
return wages[getID(0)];
}
}


Here is the main class.

package chapter7;

import java.util.Scanner;

public class PayRollTest {

public static void main(String[]args){

int h = 0; //Hours
double p = 0 ; //Pay rate

Scanner keyboard = new Scanner(System.in);

PayRoll pay = new PayRoll();

int[] hours = new int[pay.getSize()];
double[] payRate = new double[pay.getSize()];


//Store the number of hours of each employee.
for(int index = 0; index<hours.length; index++){

System.out.println("Enter the number of hours"
+" work employee #" + (index+1)
+ ":");
h = keyboard.nextInt();

//Input validation statement for # of hours.
while(h<1){
System.out.println("Invalid input!");
System.out.println("number of hours worked should be >=1");
h = keyboard.nextInt();
}
hours[index] = h;
}

//Store the hourly pay rate of each employee.
for(int index = 0; index<hours.length; index++){

System.out.println("Enter the pay rate for"
+" employee #" + (index+1)
+ ":");
p = keyboard.nextDouble();

//Input validation for pay rate.
while(p<9.25){
System.out.println("Invalid input!");
System.out.println("Pay rate should be >=9.25");
p = keyboard.nextInt();
}
payRate[index] = p;
}

//pass the array as an argument to the class pay.
pay.setHours(hours);
pay.setPayRate(payRate);

//Prompt the user for an employee id.
System.out.println("Enter your employee id");
pay.getID(keyboard.nextInt());

System.out.println("Hourly Pay Rate: " + pay.getPayRate() +
"\nHours Worked: " + pay.getHours() +
"\nGross wage: " + pay.getWage());
}//End of class.
}//End of main.

Answer

You are not calling public void setWages() so the wages array is never filled in.

pay.setHours(hours);
pay.setPayRate(payRate);

// add this
pay.setWages ();

Also you should change your getWage method to be something like

 public double getWage(int id){
        return wages[getID(id)];
  }
Comments