3

The following is a Java assignment question and I have most of it completed, but I'm having an issue with displaying the output in descending order (starting from the highest number of hours worked down to the lowest number of hours worked, while also matching those hours with the employee that worked them). I highlighted the part of the problem I need help with in bold and included the sample output.

Problem:

Computing the weekly hours of Employees Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee’s n day work hours with n columns where n ≥ 1 and n ≤ 7 representing the number of days in a week that these employees work. For example, the table shown below represents an array that stores the work hours of eight employees for 7 days in a week. Write a program that takes in as inputs, the number of employees and the number of working days in a week. Then it takes in all the Employee information (name and number of daily hours worked). This program should display employees and their total hours worked in a week in decreasing order of the total hours.

Sample Output:

Employee 7 worked 39 hours

Employee 6 worked 37 hours

Employee 0 worked 34 hours

Employee 4 worked 32 hours

Employee 3 worked 31 hours

Employee 1 worked 28 hours

Employee 5 worked 28 hours

Employee 2 worked 24 hours

Here's what I've got so far, I believe I'm missing something in the last for Loop but that's why I'm posting this. Any help is very much appreciated:

import java.util.Scanner;
import java.util.Arrays;

public class EmployeeWorkHours {
public static void main(String[] args)
{
    Scanner scan = new Scanner(System.in);
      System.out.println("How many Employee's do you have?: ");
      int NUM_OF_EMPLOYEES = scan.nextInt();
      scan.nextLine();

      int [][]hours;
      int []totalHours= new int[NUM_OF_EMPLOYEES];

      hours = new int[NUM_OF_EMPLOYEES][7];
      String[] employee = new String[NUM_OF_EMPLOYEES];

      // input Names
      for (int x = 0; x < (employee.length); x++) 
      {
          System.out.println("Name of Employee " + (x + 1) + ": ");
          String name = scan.nextLine();
          employee[x] = name;

      }

      // input Hours
      for (int z = 0; z < employee.length; z++) 
      {
          System.out.println("Beginning on Monday, enter the hours Employee "+ (z + 1)+ " has worked each day (Separate each day by spaces): ");
          for (int a = 0; a < 7; a++) 
          {
              hours[z][a] = scan.nextInt();
          }
          scan.nextLine();
      }

      // Print everything in for loop
      for (int i = 0; i < employee.length; i++) 
      {
          totalHours[i]=0;
          for(int j=0; j<7; j ++)
          {
              totalHours[i] = totalHours[i]+hours[i][j];   
          }
          // I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
          // Arrays.sort(totalHours); gives me the wrong output, I'm stuck

          System.out.println("Employee " + (i + 1) +" worked " + totalHours[i] + " hours");  
      }
}
}
Ryan Horner
  • 123
  • 1
  • 1
  • 7
  • You should start by implementing a class for the `Employee`, with the employee number, hours and total hours. Build a `List` of employees, then sort the list descending by total hours using `Collections.sort()`. Delete this no-attempt question, then ask a new one showing what you have tried, once you have tried. – Andreas Mar 27 '16 at 22:35
  • My EmployeeWorkHours class already has the employee number, hours, and total hours. I'm not sure what you mean. Also I've already made several attempts but I'm stuck here which is why I posted this. – Ryan Horner Mar 27 '16 at 23:38
  • You have static code. You don't use objects. I meant that you should create instances of an `Employee` class, carrying the values as fields. That way, when you sort, they all sort together. – Andreas Mar 28 '16 at 01:24

1 Answers1

1

A quick but bad performance patch to your code below:

// Print everything in for loop
for (int i = 0; i < employee.length; i++) {
    totalHours[i] = 0;
    for (int j = 0; j < 7; j++) {
        totalHours[i] = totalHours[i] + hours[i][j];
    }
    // I THINK I NEED TO ADD SOMETHING HERE BUT I'M NOT SURE WHAT
    // Arrays.sort(totalHours); gives me the wrong output, I'm stuck

}

//patch starts here
int[] totalHoursSortedAsc = Arrays.copyOf(totalHours, totalHours.length);
Arrays.sort(totalHoursSortedAsc);
for (int i = totalHoursSortedAsc.length - 1;i>=0;i--) {
    for (int j = 0;j < totalHours.length;j++) {
        if (totalHoursSortedAsc[i] == totalHours[j]) {
            System.out.println("Employee " + (j + 1) + " worked " + totalHours[j] + " hours");
            totalHours[j] = -1; //Employees may work the same time
        }
    }
}

A better way will be sorting your EmployeeWorkHours by Collections.sort() as Andreas suggested. Look here for the example code.

Community
  • 1
  • 1
free6om
  • 438
  • 3
  • 10
  • Thank you very much! I updated the code and it works great, I was reading up on sorting collections and arrays but I wasn't getting anywhere. Thanks again! – Ryan Horner Mar 28 '16 at 01:47