Out put: Employee# Weekly Hours ---------------------------- 4 45.00 1 35.25 **8
ID: 3682362 • Letter: O
Question
Out put:
Employee# Weekly Hours
----------------------------
4 45.00
1 35.25
**8.4 (Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employ- ee's seven-day work 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 M T W Th F Sa Employee 0 2 43 4 5 8 8 Employee 7 3 4 3 3 4 4 Employee 2 3 3 4 3 3 2 2 Employee 3 Employee 4 3 5 4 3 6 3 8 Employee 5 3 4 4 6 3 4 4 Employee 6 Employee 7 9 34 7 34 1 3 7 4 8 3 8 4 6 3 5 9 2 7 9Explanation / Answer
import java.util.Arrays;
public class EmployeeHours {
public static void main(String[] args) {
/** Initialize an 2D array of employee's hours */
int[][] hours = {{2, 4, 3, 4, 5, 8, 8},
{7, 3, 4, 3, 3, 4, 4},
{3, 3, 4, 3, 3, 2, 2},
{9, 3, 4, 7, 3, 4, 1},
{3, 5, 4, 3, 6, 3, 8},
{3, 4, 4, 6, 3, 4, 4},
{3, 7, 4, 8, 3, 8, 4},
{6, 3, 5, 9, 2, 7, 9}};
/** Sum of hours */
int[] total = new int[8];
/** Sum hours by rows */
for(int i = 0; i < hours.length; i++) {
for(int j = 0; j < hours[i].length; j++) {
total[i] += hours[i][j];
}
}
/** Sort hours in increasing order */
Arrays.sort(total);
/** Reverse sorted array */
for(int i = hours.length-1; i >= 0; i--) {
System.out.println("Employee " + i + " = " + total[i]);
}
}
}
sample output
Employee 7 = 41
Employee 6 = 37
Employee 5 = 34
Employee 4 = 32
Employee 3 = 31
Employee 2 = 28
Employee 1 = 28
Employee 0 = 20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.