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

My java program takes in employees and their weekly hours and outputs their tota

ID: 2246866 • Letter: M

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 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<numberOfEmployees+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<workDays+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<numberOfEmployees; 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<hours.length; i++) {

                                                total += hours[i];

                                }

                                return total;

                                               

                }

}

Explanation / Answer

Hi..

import java.util.Scanner;
public class WeeklyHour
{
public static int[][] readHours()
{
Scanner scan = new Scanner (System.in);
int[][] employees = new int[ ][ ];
for (int i = ;i<employees.length;i++)
{
for (int j = ;j<employees[i].length;j++)
{
employees[i][j] = scan.nextInt();
}
}
return employees;
}

public static int[][] sortEmployees(int[][] employees)
  
{
  
int[][] tempar = new int[ ][ ];
  
for (int i = ;i<employees.length;i++)
  
{
  
tempar[i][ ] =i;
  
for (int j= ;j<employees[i].length;j++)
  
{
  
tempar[i][ ] += employees[i][j];
  
}
  
}
  
for (int i = ;i<employees.length;i++)
  
{
  
for (int j = ;j<employees.length - ;j++)
  
{
  
if ( tempar[j][ ] < tempar[j+ ][ ] )
  
{
  
int temp = tempar[j][ ];
  
int temp = tempar[j][ ];
  

  
tempar[j][ ] = tempar[j+ ][ ];
  
tempar[j][ ] = tempar[j+ ][ ];
  
tempar[j+ ][ ] = temp;
  
tempar[j+ ][ ] = temp ;
  
}
  
}
  
}
  
return tempar;
  
}
  

  
public static void printEmployees(int[][] tempar)
  
{
  
for (int i = ;i<tempar.length;i++)
  
{
  
System.out.println("Employee's " + tempar[i][ ] +
  
" worked the most with " + tempar[i][ ] + " hours" );
  
}
  
}
  

  
public static void main (String[] args)
  
{
  
int[][] nums = readHours();
  
int[][] nums = sortEmployees(nums);
  
printEmployees(nums );
  
}
  
}

  

thanks

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