0

I'm having an isse with a code that computes the number of hours worked for a set of hourly employees, I can't get the information to line up.

I have the basic information of the code, as shown here:

public static void main(String[] args) {
    double[][] employeeHours = {
            {0.0, 8.0, 8.0, 8.5, 8.0, 9.0, 0.0},
            {0.0, 9.0, 9.0, 8.5, 8.0, 7.5, 0.0},
            {0.0, 8.5, 8.0, 8.6, 8.6, 9.5, 2.0},
            {0.0, 4.75, 6.0, 6.25, 6.5, 0.0, 0.0},
            {0.0, 0.0, 0.0, 0.0, 5.25, 5.25, 6.0}};

    double[] totalHoursPerEmployee = sumEmployeeHours(employeeHours);
    for (double j : totalHoursPerEmployee) {
        System.out.println(" worked hours " + j);
    }
    System.out.println(" worked");
}
public static double[] sumEmployeeHours(double[][] hours) {
    double[] totalHoursPerEmployee = new double[hours.length];
    for (double i = 0; i < hours.length; i++) {
        int sum = 0;
        for (double j = 0; j < hours[(int) i].length; j++) {
            sum += hours[(int) i][(int) j];
        }
        totalHoursPerEmployee[(int) i] = sum;
    }
    return totalHoursPerEmployee;
}
static void end(String[] xnames) {
    String[] names = {"Tara Teamlead", "Harry Hacker",
            "Carl Coder", "Paula Programmer", "Darrin Debugger"};
    for (String name : names) {
        System.out.println(name);
    }
}

But I can't get the final information to stay that way:

Tara Teamlead worked 41.50
Harry Hacker worked 42.00
Carl Coder worked 45.20
Paula Programmer worked 23.50
Darrin Debugger worked 16.50

2 Answers2

1

The problem is in this method:

public static double[] sumEmployeeHours(double[][] hours) {
    double [] totalHoursPerEmployee = new double [hours.length];
    for (double i = 0; i < hours.length; i++) {
        int sum = 0;
        for (double j = 0; j < hours[(int) i].length; j++) {
            sum += hours[(int) i][(int) j];
        }
        totalHoursPerEmployee[(int) i] = sum;
    }
    return totalHoursPerEmployee;
}

First of all, when looping in a forloop, it's easier to use INT as an iterator, not Double. Then you don't need to convert it. And second, to be able to save total hours per employee with decimals, you need to declare the sum as a DOUBLE. When you now declare it as an INT, you can not store decimals.

I tried this method(C#), but you can modify your method to work aswell by doing the following:

Change for loops to INT. Change the sum to DOUBLE.

public static double[] sumEmployeeHours(List<List<double>> hours)
    {
        double[] totalHoursPerEmployee = new double[hours.Count];
        for (int i = 0; i < hours.Count; i++)
        {
            double sum = 0;
            for (int j = 0; j < hours[(int)i].Count; j++)
            {
                var hourss = hours[i][j];
                sum += hourss;
            }
            totalHoursPerEmployee[(int)i] = sum;
        }
        return totalHoursPerEmployee;
    }
Brewsli
  • 127
  • 1
  • 10
0

You can simplify your code as follows:

double[][] hours = {
        {0.0, 8.0, 8.0, 8.5, 8.0, 9.0, 0.0},
        {0.0, 9.0, 9.0, 8.5, 8.0, 7.5, 0.0},
        {0.0, 8.5, 8.0, 8.6, 8.6, 9.5, 2.0},
        {0.0, 4.75, 6.0, 6.25, 6.5, 0.0, 0.0},
        {0.0, 0.0, 0.0, 0.0, 5.25, 5.25, 6.0}};

String[] names = {"Tara Teamlead", "Harry Hacker",
        "Carl Coder", "Paula Programmer", "Darrin Debugger"};

// assume the lengths of 2D and 1D arrays are the same
for (int i = 0; i < names.length; i++) {
    System.out.print(names[i]);
    System.out.printf(" worked %.2f", Arrays.stream(hours[i]).sum());
    System.out.println();
}

Output:

Tara Teamlead worked 41,50
Harry Hacker worked 42,00
Carl Coder worked 45,20
Paula Programmer worked 23,50
Darrin Debugger worked 16,50

See also: How do I sort two arrays relative to each other?