Question
My java program takes in employees and their weekly hours and outputs their total hours. What I can not figure out how to do is
My java program takes in employees and their weekly hours and outputs their total hours. What I can not figure out how to do is to sort the emplyees in descending order from the ammount of hours they worked a week. My program currently sorts them in the order they were entered by the user. Can you walk me through the steps I need to take to sort them by hours worked? Below is my program.
import java.util.Scanner;
public class EmployeeWorkHours {
public static void main(String[] args) {
int numberOfEmployees=0;
int workDays=0;
Scanner in = new Scanner(System.in);
//asks for number of employees
System.out.println("Please enter the number of employees");
boolean employeeNumberflag = true;
while (employeeNumberflag) {
try {
numberOfEmployees = Integer.valueOf(in.nextLine());
if (numberOfEmployees <= 0)
System.out.println("Please enter an positive integer.");
else
employeeNumberflag = false;
}
catch (NumberFormatException e) {
System.out.println("Please enter an positive integer.");
}
}
//asks length of work week from 1-7
System.out.println("Please enter the number of work days in a week. Accepable values are 1-7.");
boolean daysFlag = true;
while (daysFlag) {
try {
workDays = Integer.valueOf(in.nextLine());
if (workDays > 0 && workDays < 8) {
daysFlag=false;
}
else {
daysFlag = true;
System.out.println("Please enter an integer from 1-7.");
}
}
catch (NumberFormatException e) {
System.out.println("Please enter an integer from 1-7.");
}
}
//creating empty arrays for employee names and hours
String[] employees = new String[numberOfEmployees];
int[][] hoursWorked = new int[numberOfEmployees][workDays];
//outer loop takes employee names and inputs them into employee[]
for (int i=1; i System.out.println("Please enter the name of employee " + i + "."); employees[i-1] = in.nextLine(); //inner loop takes number of hours each day worked for each employee System.out.println("Please enter " + workDays + " work day hours."); for (int j=1; j System.out.println("Please enter an integer from 0-24 for work day " + j +"."); boolean hoursFlag = true; while (hoursFlag) { try { int hours = Integer.valueOf(in.nextLine()); if (hours > -1 && hours < 25) { hoursWorked[i-1][j-1] = hours; hoursFlag = false; } else { hoursFlag = true; System.out.println("Please enter an integer from 0-24."); } } catch (NumberFormatException e) { System.out.println("Please enter an integer from 0-24."); } } } } //print out desired output table for employees and hours for (int h=0; h System.out.printf("%-15s%14d ",employees[h], calculateHours(hoursWorked[h])); } } //method that takes each employee hours and adds them all up public static int calculateHours(int[] hours) { int total = 0; for (int i=0; i total += hours[i]; } return total; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started