Write a program that computes weekly hours for each employee. Store the weekly h
ID: 3690121 • Letter: W
Question
Write a program that computes weekly hours for each employee. Store the weekly hours for all employees in a two-dimensional array. Each row records an employee’s seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Display employees and the total hours of each employee in decreasing order of the total hours.
Write a program that computes weekly hours for each employee. Store the weekly hours for all employees in a two-dimensional array. Each row records an emplyee's seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Display employees and the total hours of each employee in decreasing order of the total hours. Design a method that takes the 2-D array of weekly hours of all employees as an input parameter, calculates the sum of the total hours of all employees and returns the total hour results in an array. Design a method to sort the above returned array in Requirement 1 in descending order by selection sort. Design a method to display the results of employees and their total hours in decreasing order. The sample output for the above example is as follows: Design a main method that uses the short hand initializer to initialize the 2-D array of weekly hours of all employees and invokes all the three methods in Requirements 1-3 in a sequence.Explanation / Answer
public class Program{
static final int workers = 0;
static final int HR = 1;
public static void main(String[] args) {
int[][] empHR = 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[][] sumHRChart = calcSumHR(empHR);
descendSort(sumHRChart);
for (int i = 0; i < sumHRChart.length; i++) {
System.out.println("Employee #"+ sumHRChart[i][EMP] + " total hours = " +
sumHRChart[i][HR]);
}
}
public static int[][] calcSumHR(int[][] m) {
int[][] sumHRChart = new int[m.length][2];
for (int i = 0; i < m.length; i++) {
sumHRChart[i][HR] = getRwSumHour(m, i);
sumHRChart[i][worker] = i;
}
return sumHRChart;
}
public static int getRwSumHour(int[][] m, int rw) {
int sum = 0;
for (int colm = 0; colm < m[rw].length; colm++) {
sum += m[rw][colm];
}
return sum;
}
public static void descendSort(int[][] m) {
for (int i = 0; i < m.length - 1; i++) {
int presentIndex = i;
int presentMax = m[i][HR];
int empNum = m[i][workers];
for (int k = i + 1; k < m.length; k++) {
if (presentMax < m[k][HR]) {
presentMax = m[k][HR];
presentIndex = k;
empNum = m[k][workers];
}
}
if (presentIndex != i) {
m[presentIndex][workers] = m[i][workers];
m[presentIndex][HR] = m[i][HR];
m[i][workers] = empNum;
m[i][HR] = presentMax;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.