Answer this question using JAVA programming language, it should print the whole
ID: 3585280 • Letter: A
Question
Answer this question using JAVA programming language, it should print the whole chart in decending order from most worked hours to lowest.
1. Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records an employees 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 WTh F Sa Employee 0 243458 8 Employee 1 734 3 34 4 Employee 2 3 3 4 332 2 Employee 3 Employee 4 3 5 4 363 8 Employee 5 3 446 3 4 Employee 6 3 7 48 38 4 Employee 7 6 35 9 279 9 3 4734Explanation / Answer
import java.util.Arrays;
public class EmployeeWorkingHours {
public static void main(String[] args) {
// Employee's weekly hours
int[][] workinghours = {
{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}};
int[] totalhours=new int[8];
for (int i = 0; i < workinghours.length; i++) {
totalhours[i] = totalHours(workinghours, i);
// System.out.println("Employee " + i + ": " + totalhours[i]);
}
int[] weeklyhours=new int[8];
for(int j=0;j<totalhours.length;j++){
int k=0;
int max = totalhours[0];
for(int i=0;i<totalhours.length;i++){
if(max < totalhours[i])
{
max = totalhours[i];
k=i;
}
}
totalhours[k]=0;
weeklyhours[j]=k;
}
System.out.println();
for(int i=0;i<weeklyhours.length;i++){
System.out.println("Employee " + weeklyhours[i] + ": " + Arrays.toString(workinghours[weeklyhours[i]]));
}
}
public static int totalHours(int[][] time, int rowIndex) {
int total = 0;
int i = 0;
for (int column = 0; column < time[i].length; column++) {
total += time[rowIndex][column];
}
return total;
}
}
/* output:-
Employee 7: [6, 3, 5, 9, 2, 7, 9]
Employee 6: [3, 7, 4, 8, 3, 8, 4]
Employee 0: [2, 4, 3, 4, 5, 8, 8]
Employee 4: [3, 5, 4, 3, 6, 3, 8]
Employee 3: [9, 3, 4, 7, 3, 4, 1]
Employee 1: [7, 3, 4, 3, 3, 4, 4]
Employee 5: [3, 4, 4, 6, 3, 4, 4]
Employee 2: [3, 3, 4, 3, 3, 2, 2]
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.