0

Hey there everyone, first post here so please excuse any mistakes :)

Ok so I have this assignment here:

Suppose the weekly hours for all employees are stored in a two dimensional array. Each row records an employee's seven-day works hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours.
Su Mon Tue Wed Thu Fri Sa
Emp 0 2 4 3 4 5 8 8
Emp 1 7 3 4 3 3 4 4
Emp 2 3 3 4 3 3 2 2
Emp 3 9 3 4 7 3 4 1
Emp 4 3 5 4 3 6 3 8
Emp 5 3 4 4 6 3 4 4
Emp 6 3 7 4 8 3 8 4
Emp 7 6 3 5 9 2 7 9

The final output is supposed to look like this:

Employee 7 : 41
Employee 6 : 37
Employee 0 : 34
Employee 4 : 32
Employee 3 : 31
Employee 1 : 28
Employee 5 : 28
Employee 2 : 20

Ok... so what i have done so far is create the 2D array and then made a new loop to add across each row and store that value into a new array. that looks like this
[34 28 20 31 32 28 37 41]

And I used the reverseOrder method to sort it into descending order. I then use the binarySearch function to find out which employee had what hours... but it doesnt seem to work. Any inputs would be greatly appreciated!

import java.util.*;
public class WeeklyHrs {
    static void list(Object[] a, Object n) {
        int where = Arrays.binarySearch(a, n);
        System.out.println("Employee " + where +
                " has this many hours:  " + n);
    }

    public static void main(String[] args) {

        int[][] board = new int[][]{
            {1, 2, 4, 3, 4, 5, 8, 8},
            {2,7, 3, 4, 3, 3, 4, 4},
            {3,3, 3, 4, 3, 3, 2, 2},
            {4, 9, 3, 4, 7, 3, 4, 1},
            {5, 3, 5, 4, 3, 6, 3, 8},
            {6, 3, 4, 4, 6, 3, 4, 4},
            {7, 3, 7, 4, 8, 3, 8, 4},
            {8, 6, 3, 5, 9, 2, 7, 9}};
        System.out.println("Total Hours unsorted");
        Integer[] totalHours = new Integer[8];
        for (int i = 0; i < 8; i++) {
            int sum = 0;
            for (int j = 1; j < 8; j++) {
                sum += board[i][j];
                totalHours[i] = sum;
            }
            System.out.println(sum);
        }
        System.out.println("");

        Integer[] sorted;

        sorted = new Integer[totalHours.length];
        for (int i = 0; i < totalHours.length; i++) {
            sorted[i] = totalHours[i];
        }
System.out.println("Total Hours sorted");
        Arrays.sort(sorted, Collections.reverseOrder());
        for (int i = 0; i < sorted.length; i++) {
            System.out.println(sorted[i]);
        }

        for (int i = 0; i < sorted.length; i++) {
            list(totalHours, sorted[i]);
        }

    }
}

Oh and my current output is this:

Total Hours unsorted
34
28
20
31
32
28
37
41

Total Hours sorted
41
37
34
32
31
28
28
20
Employee 7 has this many hours: 41
Employee 6 has this many hours: 37
Employee -7 has this many hours: 34
Employee -7 has this many hours: 32

Employee 3 has this many hours: 31
Employee 1 has this many hours: 28
Employee 1 has this many hours: 28
Employee -1 has this many hours: 20

I dont get why the negative numbers are appearing?? and how do i get it to read employee 5 and not just stop at employee 1?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by BestJewSinceJC
0

You'll have to post the binarySearch method, since it is returning the -7 and all the other values (at least it seems that way). Also, thank you for posting in code tags. It is much appreciated.

Edited by BestJewSinceJC: n/a

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.