Suppose the weekly hours for all employees are stored in a two-dimensional array
ID: 667534 • Letter: S
Question
Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records 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 the total hours.
Explanation / Answer
Complete Program:
import java.util.Scanner;
public class Employee
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
// declare variables for number of employees and number of working days
int numberOfEmployees;
int numberOfWorkingdays;
// Prompt and accept number of employees
System.out.print("Enter the number of employees: ");
numberOfEmployees = input.nextInt();
// Prompt and accept number of working days
System.out.print("Enter the number of working days: ");
numberOfWorkingdays = input.nextInt();
//Loop that runs until a valid number of working days is given. number of working days should be >0 and <8
while(numberOfWorkingdays < 0 || numberOfWorkingdays > 7)
{
System.out.println("Enter the valid number of working days (1-7): ");
numberOfWorkingdays = input.nextInt();
}
// Accept array to store name and number of working hours
String[] employees = new String[numberOfEmployees];
int[][] workingHours = new int[numberOfEmployees][numberOfWorkingdays];
// Accept name and number of working hours for each day
for(int i = 0; i < numberOfEmployees; i++)
{
System.out.print(" Enter the name of the employee: ");
employees[i] = input.next();
System.out.print("Enter the working hours for " + numberOfWorkingdays + " days: ");
for(int j = 0; j < numberOfWorkingdays; j++)
workingHours[i][j] = input.nextInt();
}
System.out.printf(" %-15s%12s ", "Name", "Hours");
System.out.println("----------------------------");
for(int i = 0; i < numberOfEmployees; i++)
{
System.out.printf("%-15s%12d ", employees[i], getTotalWorkingHours(workingHours[i]));
}
}
public static int getTotalWorkingHours(int[] hours)
{
int totalHours = 0;
for(int i = 0; i < hours.length; i++)
totalHours += hours[i];
return totalHours;
}
}
Sample Output:
Enter the number of employees: 4
Enter the number of working days: 8
Enter the valid number of working days (1-7):4
Enter the name of the employee: Jason
Enter the working hours for 4 days: 8 7 9 6
Enter the name of the employee: Kevin
Enter the working hours for 4 days: 8 7 6 7
Enter the name of the employee: Nygel
Enter the working hours for 4 days: 8 8 8 8
Enter the name of the employee: Jasper
Enter the working hours for 4 days: 8 8 8 8
Name Hours
----------------------------
Jason 30
Kevin 28
Nygel 32
Jasper 32
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.