Use array-initializer syntax to store a 2-dimensional array representing employe
ID: 3681459 • Letter: U
Question
Use array-initializer syntax to store a 2-dimensional array representing employees work hours for each day of the week (see sample work hours array in the text but do not assume that because the sample array shows only whole hours that an employee's time can only be recorded in whole hours). The main method prints out the employee# and his/her total weekly hours using the following model: Please no array list. Thanks
Employee# Weekly Hours ----------------------------
4 45.00
1 35.25
3 17.25
2 10.75
Explanation / Answer
import java.util.*;
import java.util.Arrays;
class Codechef
{
public static void main(String[] args) {
int[][] workHours= new int[][]{
{ 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 []empno=new int[8];
int [] totalHours = new int[8];
for (int i = 0; i < 8; i++) {
int sum = 0;
for (int j = 0; j < 7; j++) {
sum += workHours[i][j];
totalHours[i] = sum;
}
}
java.util.Arrays.sort(totalHours);
int[] sorted = new int[totalHours.length];
for (int i = 0; i < totalHours.length; i++) {
sorted[i] = totalHours[i];
}
System.out.println("Employees no. and their weekly hours :");
for (int i = 7; i >=0; i--) { //displayed descending order
System.out.println(i+" : "+sorted[i]);
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.