Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose the weekly hours for all employees are storedina two-dimensional array.

ID: 2247040 • Letter: S

Question

Suppose the weekly hours for all employees are storedina two-dimensional array. Eachrowrecords an employee’s n-day work hours with n columns where n 1 and n 7 representing the number of days in a week that these employees work. For example, the table shown below represents an array that stores the work hours of eight employees for 7 days in a week. Write a program that takes in as inputs, the number of employees and the number of working days in a week. Then it takes in all the Employee information (name and number of daily hours worked). This program should display employees and their total hours worked in a week in decreasing order of thetotal hours.

The only issue I'm having is I'm not sure how to print the names with coressponding hours worked in a week in the order from the most to the least.

EmplyeeWorkHours.java 3 1 import java.util.scanner; 2 3 public class EmplyeeWorkHours 4. 6 public static void main(String[] args) 7 8 9 Scanner scan = new scanner(System.in); 10 System.out.println( "Please enter the number of employees") int number-scan.nextInt); 12 13 14 15 16 17 18 19 20 21 System.out.println( "Please enter the number of work days per week"); int days = scan.nextInt(); int[][] employees-new int[number] [days]; string[] names new String[number]; int dayNum; for(int i = 0; i

Explanation / Answer

EmployeeHours.java

import java.util.Scanner;

public class EmployeeHours {

public static void main(String[] args) {
//Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);

//Getting the no of employees entered by the user
System.out.println("Please enter the number of employees");
int number = sc.nextInt();

//Getting the number of days entered by the user
System.out.println("Please enter the number of days per week");
int days = sc.nextInt();

//Creating the 2-D Array which stores no of hours worked for each employee
int employees[][] = new int[number][days];

//Creating 1-D Array which Stores names of employees
String names[] = new String[number];
int empTotHours[] = new int[number];
int dayNum = 0;

//Getting the inputs entered by the user
for (int i = 0; i < employees.length; i++) {
System.out.println("Please enter the name of an Employee#" + (i + 1));
names[i] = sc.next();

for (int j = 0; j < employees[i].length; j++) {
System.out.println("Please enter the number of hours worked on day " + (++dayNum) + " for " + names[i]);
employees[i][j] = sc.nextInt();
}
dayNum = 0;
}


//Calculating the total worked hours of each employee
for (int i = 0; i < employees.length; i++) {
empTotHours[i] = calEmpTotHours(employees, i);
}

//Calling the methods
sortInDecending(names, empTotHours);
display(names, empTotHours);

}

//This method is used to Display the names of employees and total worked hours in decending order
private static void display(String[] names, int[] empTotHours) {
System.out.println(":: After Sorting in Decending order ::");
for (int i = 0; i < empTotHours.length; i++) {
System.out.println(names[i] + "-----" + empTotHours[i]);
}

}

//This method will sort the total total working hours of each employee in deceding order
private static void sortInDecending(String[] names, int[] empTotHours) {
//This Logic will Sort the Array of elements in Decending order
int temp1;
String tempName;
for (int i = 0; i < empTotHours.length; i++) {
for (int j = i + 1; j < empTotHours.length; j++) {
if (empTotHours[i] < empTotHours[j]) {
temp1 = empTotHours[i];
empTotHours[i] = empTotHours[j];
empTotHours[j] = temp1;

tempName = names[i];
names[i] = names[j];
names[j] = tempName;
}
}
}

}

//This method will calculate the total working hours of each employee
private static int calEmpTotHours(int[][] employees, int i) {
int tot = 0;
for (int j = 0; j < employees[i].length; j++) {
tot += employees[i][j];
}
return tot;
}


}

___________________

Output:

Please enter the number of employees
3
Please enter the number of days per week
5
Please enter the name of an Employee#1
John
Please enter the number of hours worked on day 1 for John
10
Please enter the number of hours worked on day 2 for John
10
Please enter the number of hours worked on day 3 for John
9
Please enter the number of hours worked on day 4 for John
8
Please enter the number of hours worked on day 5 for John
8
Please enter the name of an Employee#2
Mike
Please enter the number of hours worked on day 1 for Mike
10
Please enter the number of hours worked on day 2 for Mike
10
Please enter the number of hours worked on day 3 for Mike
12
Please enter the number of hours worked on day 4 for Mike
13
Please enter the number of hours worked on day 5 for Mike
11
Please enter the name of an Employee#3
Ken
Please enter the number of hours worked on day 1 for Ken
12
Please enter the number of hours worked on day 2 for Ken
10
Please enter the number of hours worked on day 3 for Ken
9
Please enter the number of hours worked on day 4 for Ken
8
Please enter the number of hours worked on day 5 for Ken
9
:: After Sorting in Decending order ::
Mike-----56
Ken-----48
John-----45

_____________Could u plz rate me well.Thank u .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote