Reputation: 11
Problem: Write a program to calculate the wages of 5 hourly-paid employees in the company. Your program will input for each of the 5 employees the employee ID, the number of hours they worked and his/her pay rate. These data are to be stored into 3 parallel arrays: employee_ID, hours, and payrate. Your program will then call a method to calculate the wage of each employee, the results will be stored in another parallel array. Output the 4 parallel arrays side-by-side. Your program will then call 3 methods: findAve to determine the average pay of the 5 employees, findMax and findMin to determine and return the ID of the employee who receives the highest and lowest pay respectively. The results will be outputted in the main method.
Trouble with: How do I implement a method call to get an array average?
Code:
public class Employees {
public static void main(String[] args) {
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
int[] id;
id = new int[5];
int[] payrate;
payrate = new int[5];
int[] wage;
wage = new int[5];
final int EMPLOYEES = 5; // Number of employees
int[] hours = new int[EMPLOYEES]; // Array of hours
System.out.println("Enter the hours worked by " +
EMPLOYEES + " employees.");
// Get the hours for each employee.
for (int index = 0; index < EMPLOYEES; index++)
{
System.out.print("Employee " + (index + 1) + ": ");
hours[index] = keyboard.nextInt();
}
System.out.println("The hours you entered are:");
// Display the values entered.
for (int index = 0; index < EMPLOYEES; index++)
System.out.println(hours[index]);
System.out.println("Enter the payrate worked by " +
EMPLOYEES + " employees.");
// Get the payrate for each employee.
for (int index1 = 0; index1 < EMPLOYEES; index1++)
{
System.out.print("Employee " + (index1 + 1) + ": ");
payrate[index1] = keyboard.nextInt();
}
System.out.println("The payrate you entered are:");
// Display the values entered.
for (int index1 = 0; index1 < EMPLOYEES; index1++)
System.out.println(hours[index1]);
System.out.println("Enter the wage worked by " +
EMPLOYEES + " employees.");
// Get the wage for each employee.
for (int index2 = 0; index2 < EMPLOYEES; index2++)
{
System.out.print("Employee " + (index2 + 1) + ": ");
wage[index2] = keyboard.nextInt();
}
System.out.println("The wage you entered are:");
// Display the values entered.
for (int index2 = 0; index2 < EMPLOYEES; index2++)
System.out.println(wage[index2]);
}
//A method that calculates and returns the average of a passed array.
public static void calculateAverage (int[] wage)
{
int average = 0;
int total = 0;
for (int i=0; i<=wage.length; i++)
{
total += wage[i];
}
average = (total / wage.length);
return;
}
System.out.println(average);
}
Upvotes: 0
Views: 2322
Reputation: 2443
You've done most of it in calculateAverage()
already. But there's a couple issues.
You want to set your for loop to be i<wage.length
rather than <=
because arrays start at 0, so when i = length
of the actual array, you'll get an out of bounds exception. ie, if there's 5 employees, wage.length == 5
where as the indexes are 0, 1, 2, 3, 4
.
you're not returning anything. You've calculated the average so return it.
You'll need to set the return type other than void
. I would also recommend NOT using an integer just in case your average turns out to be a decimal. You'd have to cast your int
values first before calculation.
Rough fixes:
public static double calculateAverage (int[] wage) {
double average = 0;
int total = 0;
for (int i=0; i<wage.length; i++) {
total += wage[i];
}
average = (double) total / (double) wage.length;
return average;
}
Your system.out.println
afterwards also can't access average
since it's out of scope of the method. You can call it like this:
System.out.println(calculateAverage(wage));
Upvotes: 1
Reputation: 129
Your calculateAverage method is returning void, which means it doesn't return a value. You should make it return the average.
PS: for monetary values, I suggest you to use the double instead of int. Double accept decimal values when int does not.
public static int calculateAverage (int[] wage)
{
int average = 0;
int total = 0;
for (int i=0; i<wage.length; i++)
{
total += wage[i];
}
average = (total / wage.length);
return average;
}
Upvotes: 0