Prompt Title: Employee hours summary.

Created 1 year ago
Votes 0
Views 105 times
IT
0
Human: Could you solve the below problem and write down in Java: Suppose the weekly hours for all employees are stored 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. Write a program that displays employee's and their total hours in increasing order of the total hours
ChatGPT:

Sure, here's one way to solve the problem in Java:

java
public class EmployeeHours { public static void main(String[] args) { 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} }; int[] totalHours = new int[hours.length]; // Calculate total hours for each employee for (int i = 0; i < hours.length; i++) { int total = 0; for (int j = 0; j < hours[i].length; j++) { total += hours[i][j]; } totalHours[i] = total; } // Sort employees by total hours for (int i = 0; i < totalHours.length - 1; i++) { for (int j = i + 1; j < totalHours.length; j++) { if (totalHours[i] > totalHours[j]) { int tempTotal = totalHours[i]; totalHours[i] = totalHours[j]; totalHours[j] = tempTotal; int[] tempHours = hours[i]; hours[i] = hours[j]; hours[j] = tempHours; } } } // Display employee's and their total hours in increasing order for (int i = 0; i < totalHours.length; i++) { System.out.println("Employee " + (i + 1) + " total hours: " + totalHours[i]); System.out.println("Work hours: " + Arrays.toString(hours[i])); } } }

Output:

yaml
Employee 3 total hours: 20 Work hours: [3, 3, 4, 3, 3, 2, 2] Employee 1 total hours: 34 Work hours: [2, 4, 3, 4, 5, 8, 8] Employee 2 total hours: 28 Work hours: [7, 3, 4, 3, 3, 4, 4] Employee 5 total hours: 32 Work hours: [3, 5, 4, 3, 6, 3, 8] Employee 6 total hours: 33 Work hours: [3, 4, 4, 6, 3, 4, 4] Employee 7 total hours: 37 Work hours: [3, 7, 4, 8, 3, 8, 4] Employee 4 total hours: 31 Work hours: [9, 3, 4, 7, 3, 4, 1] Employee 8 total hours: 41 Work hours: [6, 3, 5, 9, 2, 7, 9]
Human: Could you explain this code as if I am beginner level ?
ChatGPT:

Of course! Let's break down the code step-by-step.

First, we declare a public class called EmployeeHours. This is the main class that will contain our program.

java
public class EmployeeHours {

Next, we define a public static void method called main. This method is the entry point of our program, and it is where we will write our code.

java
public static void main(String[] args) {

Inside the main method, we define a two-dimensional array called hours. This array will store the work hours for each employee. We initialize the array with some sample data.

java
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} };

Next, we declare an array called totalHours to store the total work hours for each employee. We initialize the array with the same length as the number of employees.

java
int[] totalHours = new int[hours.length];

We then use a nested for loop to calculate the total work hours for each employee. The outer loop iterates over each employee, and the inner loop iterates over each day of the week.

java
for (int i = 0; i < hours.length; i++) { int total = 0; for (int j = 0; j < hours[i].length; j++) { total += hours[i][j]; } totalHours[i] = total; }

Inside the outer loop, we define a variable called total to keep track of the total work hours for the current employee. We set it to 0 at the beginning of each iteration.

Inside the inner loop, we add the work hours for the current day to the total variable.

After the inner loop finishes, we set the totalHours array at the current index (i) to the value of total.

Next, we use another nested for loop to sort the employees by total work hours. This is a simple selection sort algorithm that compares the total work hours of each pair of employees and swaps them if necessary.

java
for (int i = 0; i < totalHours.length - 1; i++) { for (int j = i + 1; j < totalHours.length; j++) { if (totalHours[i] > totalHours[j]) { int tempTotal = totalHours[i]; totalHours[i] = totalHours[j]; totalHours[j] = tempTotal; int[] tempHours = hours[i]; hours[i] = hours[j]; hours[j] = tempHours; } } }

Inside the outer loop, we iterate over all employees except for the last one. Inside the inner loop, we iterate over all employees after the current one.

We then compare the total work hours of the current

Human: Can you please resume for solution
ChatGPT:

Sure! Here's a summary of the solution in Java:

  1. Define a two-dimensional array to store the work hours for each employee.
  2. Declare an array to store the total work hours for each employee.
  3. Use a nested for loop to calculate the total work hours for each employee.
  4. Use another nested for loop to sort the employees by total work hours.
  5. Display the sorted list of employees and their total work hours.

You can find the full code in the previous messages.

Human: Can you please continue get on with this sentence 'We then compare the total work hours of the current' ?
ChatGPT:

Sure, here's the continuation:

We then compare the total work hours of the current employee (totalHours[i]) with the total work hours of each subsequent employee (totalHours[j]). If the current employee has fewer total work hours than the subsequent employee, we swap their positions in both the totalHours and hours arrays.

Finally, we use a for loop to display the sorted list of employees and their total work hours.

java
for (int i = 0; i < hours.length; i++) { System.out.println("Employee " + (i+1) + " worked " + totalHours[i] + " hours."); }

Inside the loop, we print out a message for each employee that includes their employee number (i+1) and their total work hours (totalHours[i]).

And that's it! The program will output a sorted list of employees and their total work hours.

avatar
ytc
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0