+1 (315) 557-6473 

Hourly Wage Calculator using Java Assignment Solution


Calculate wage based on hours worked

Summary

Write a java homework that will calculate two weeks of hourly pay for an individual. Payment will depend on the actual hours worked, overload, and the week of the year the hours were worked. The results will be displayed to the user.

Overall Requirements

  • From the user, get the number of the week worked.
  • From the user, get the hours worked for the week. The user should enter seven double values.
  • The standard pay rate is $15.00 / hour.
  • Overtime pay is 1.5 times the standard pay, for hours worked over 40 hours per week.
  • High demand pay is an additional $2.00 / hour or $17.00 / hour.
  • High demand pay will be calculated for Weeks 1, 2, 44-54.
  • Allow users to calculate wages for another week.

Functional requirements:

  • Declare the following constant variables to utilize in your program:

o HOUR_RATE, double, equal to 15.00

o BONUS_RATE, double, equal to 2.00

o OVERLOAD_PERCENT, double, equal to 1.5

o REGULAR_HOURS, double, equal to 40.0

  • Validate the week entered (the week should be between 1 and 54). If a user enters an invalid week, output a short message and ask them to enter a week number again.
  • An invalid week should produce a short error “Week must be between 1 and 54, please try again.”
  • The system will loop until a valid value is entered. If the user enters -1, the program should end.
  • Validate the hours entered for the week.
  • If the user enters more or less than 7 values, instruct the user to try again.
  • If the user enters non-numeric values, instruct the user to try again.
  • If the user enters a negative value or a value greater than 15, instruct the user to try again.
  • Calculate the payment for the week.
  • Add the number of hours worked for the week.
  • Up to 40 hours will be calculated using the base rate.
  • Hours over 40 will be calculated using the overtime pay of 1.5. For example, if a person works 45 hours, 40 would be calculated at the base rate, and 5 will be calculated at the overtime rate.
  • If the week worked is a high-demand week (1,2, 44-54), add $2.00 to the base rate.

o A year has 54 weeks, Week 1 is the first week of January, Week 54 is the last week of the year.

  • Display the total pay for the week in USD with 2 decimals.
  • Display a breakdown of the payment.

o The total regular hours worked, and the rate applied.

o The total of the base rate pay.

o The total of overtime hours worked, and the overtime rate applied.

o The total of the overtime pay.

  • Allow the user to enter hours for another week.

Organizational requirements:

  • The main method should help as your focus to control the algorithm.
  • Your code should use multiple methods to delegate the work that has to be done.

o main(): should control the overall program, a starting java file is provided.

o getInputFromUser() should take as a parameter a Scanner variable and an integer value for the week number. The method should return an array of type double. This method will ask the user to input the hours of work for the week, call the validateInput() method. The method should continue to ask the user as long as the validateInput() method returns false. Once the values are found to be valid, the method should call convertStringArrayToDouble() to get an array containing all the valid values.

o validateInput() should take as a parameter an array of type String, and return a boolean indicating whether the input is valid (true) or not (false). This method should validate that all the values meet the conditions, if any invalid condition is found, the method returns false. If all values are valid, the method returns true.

o convertStringArrayToDouble() should take as a parameter an array of type String, and return an array of type boolean. The method will take each value in the string array, convert it to double, and put it into the numeric array.

o calculatePayForWeek() should take as a parameter an integer value for the week, and an array of type double; the method does not return any values (void). The method will perform and display all the wage calculations. It should call getTotalHours() to obtain the sum of the hours worked. This method should use if statements to identify the different conditions (regular, overtime, and bonus)

o getTotalHours() should take as a parameter an array of type double and return an integer value equal to the sum of all the hours worked.

  • Follow all coding style guidelines presented thus far (arrays, methods, and loops). Remember to place comments in front of sections of code to help you and me understand your code better.

o For example, do not handle all the conditions at once: validate only that you have seven values and test it, then add another one.

o Another example is to only work with hours <= 40 and normal weeks, then add the checks for bonus weeks, then add the check for overtime.

  • The math may stump you a bit if you are not fully comfortable with using percentages.
  • You will most likely need a combination of if-else statements and loop(s) to generate the appropriate choices and to get the right calculations.
  • Rates will vary depending on the user input for the month.

o Declare constant values.

o Use math and string functions.

 // * write convertStringArrayToDouble method here

 // * write calculatePayForWeek method here

 // * write getTotalHours method here

}

Hourly Wage Calculation

Solution:

package project1; import java.util.Scanner; public class HourlyWageCalculator { // Declare the global constant variables here private static final double BONUS_RATE = 2.0; private static final double HOUR_RATE = 15.0; private static final double OVERLOAD_PERCENT = 1.5; private static final double REGULAR_HOURS = 40.0; // Use the names given in the instructions public static void main(String[] args) { // I am starting out this method for you Scanner scnr = new Scanner(System.in); int weekNumber; char answer = 'y'; double[] weekHours; // use a while loop to run program // this loop should give user the option to calculate wages more than once while (answer == 'y') { // inside this loop // ask user to enter values while(true) { try { System.out.print("Enter week worked: "); weekNumber = Integer.parseInt(scnr.nextLine()); if (weekNumber == -1) { return; } if (weekNumber < 1 || weekNumber > 52) { throw new NumberFormatException(); } break; } catch (NumberFormatException e) { System.out.println("Week must be between 1 and 54, please try again."); } } weekHours = getInputFromUser(scnr, weekNumber); calculatePayForWeek(weekNumber, weekHours); // call methods getInputFromUser and calculatePayForWeek // at the end of the loop ask user if they want to do another calculation System.out.println(); System.out.print("Would you like to calculate pay for another week? y/n: "); String line = scnr.nextLine().trim(); answer = (line.length() == 0 || Character.toLowerCase(line.charAt(0)) != 'y') ? 'n' : 'y'; } } // *** declare methods beginning here // * write getInputFromUser method here public static double[] getInputFromUser(Scanner scanner, int i) { while (true) { System.out.print("Enter hours for week " + i + ": "); String[] parts = scanner.nextLine().trim().split("\\s+"); if (validateInput(parts)) { return convertStringArrayToDouble(parts); } } } // * write validateInput method here public static boolean validateInput(String[] input) { // complete all validation cases for the method // enter code to check for seven values if (input.length != 7) { System.out.println("7 values expected"); return false; } // here is the code to check values are all numeric // we will see exceptions soon... for (int i = 0; i < input.length; i++) try { Double.valueOf(input[i]); // } catch (NumberFormatException e) { // this is how java tells us a value was not numeric System.out.println("Input included invalid numbers. Try again please."); return false; } // enter code to check all positive values for (int i = 0; i < input.length; i++) if (Double.parseDouble(input[i]) < 0.0) { System.out.println("Input included negative numbers. Try again please."); return false; } // if we make to the end, then all checks are good, return true return true; } // * write convertStringArrayToDouble method here public static double[] convertStringArrayToDouble(String[] s) { double[] d = new double[s.length]; for (int i = 0; i= 44 && i <= 54)) { payPerHour += BONUS_RATE; System.out.println("Week " + i + " receives a bonus of " + String.format("$%.2f", BONUS_RATE) + " per hour"); } double totalHours = getTotalHours(d); double baseHoursWorked = Math.min(40.0, totalHours); double basePay = payPerHour * baseHoursWorked; double overtimeHoursWorked = Math.max(0.0, totalHours - REGULAR_HOURS); double overtimePerHour = payPerHour * OVERLOAD_PERCENT; double overtimePay = overtimePerHour * overtimeHoursWorked; double totalPay = basePay + overtimePay; System.out.println("Your total pay for week " + i + " is: " + String.format("$%.2f", totalPay)); System.out.println("Your worked a total of " + String.format("$%.2f", totalHours) + " hours"); System.out.println("Here is your summary:"); System.out.printf("\tBase hours worked: %.2f at $%.2f%n", baseHoursWorked, payPerHour); System.out.printf("\tBase pay: $%.2f%n", basePay); System.out.printf("\tOvertime hours worked: %.2f at $%.2f%n", overtimeHoursWorked, overtimePerHour); System.out.printf("\tOvertime pay: $%.2f%n", overtimePay); } // * write getTotalHours method here public static double getTotalHours(double[] d) { double sum = 0.0; for (double val : d) { sum += val; } return sum; } }