In: Computer Science
Consider the data sheet below that tracks employee hours worked over four days:
Day 1 Day 2 Day 3 Day 4
Employee 1 5 8 6 2
Employee 2 2 0 8 6
Employee 3 6 4 9 5
Employee 4 7 8 8 4
Employee 5 3 6 2 8
Employee 6 9 5 1 7
Based on the above information, write a Java program that performs the following tasks:
Task 1) Recreate the matrix as a two-dimensional array by
hardcoding values (User input is not needed)
Task 2) Calculate and print the total number of hours worked across
all employees
Task 3) Find the employee who worked the most number of hours
(Hint: Total all of the columns in each row. Keep track of the
"largest" row and the employee associated to that row.
Task 4) Find the employee who worked the least number of hours
(Hint: Total all of the columns in each row. Keep track of the
"smallest" row and the employee associated to that row.
Example run:
The total number of hours worked across all employees: 129
The employee that worked the most number of hours was Employee 4
working 27 hours
The employee that worked the least number of hours was Employee 2
working 16 hours
Note: For output, use the JOptionPane class.
import javax.swing.JOptionPane;
public class EmpHours {
public static void main(String[] args) {
int
hours[][]={{5,8,6,2},{2,0,8,6},{6,4,9,5},{7,8,8,4},{3,6,2,8},{9,5,1,7}};
int
largest=0,smallest=0,largeSum=0,smallSum=1000;
int total=0,sum=0;
for(int i=0;i<hours.length;i++)
{
sum=0;
for(int
j=0;j<hours[i].length;j++) {
sum=sum+hours[i][j];
}
total+=sum;
if(sum>largeSum) {
largest=i;
largeSum=sum;
}
if(sum<smallSum) {
smallest=i;
smallSum=sum;
}
}
String res=("The total number of
hours worked across all employees: "+total+"\n");
res=res+("The employee that worked
the most number of hours was Employee "+(largest+1)+ "working
"+(largeSum)+ "hours\n");
res=res+("The employee that worked
the least number of hours was Employee "+(smallest+1)+ "working
"+(smallSum)+ "hours");
JOptionPane.showMessageDialog(null,
res);
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me