Question: Instructions Study the prewritten code to understand what has already been done. Write the control break code, including the code for the dayChange() method, in
Instructions Study the prewritten code to understand what has already been done. Write the control break code, including the code for the dayChange() method, in the main() method. Execute this program using the following input values: Monday 6 hours (employee 1), 3 hours (employee 2), 4 hours (employee 3) Tuesday 4 hours (employee 1), 2 hours (employee 2) Wednesday 2 hours (employee 1), 4 hours (employee 2), 6 hours (employee 3) Thursday 4 hours (employee 1) Friday 3 hours (employee 1), 4 hours (employee 2), 3 hours (employee 3) Saturday 7 hours (employee 1), 7 hours (employee 2), 7 hours (employee 3) Sunday 0 hours An example program execution is shown below: WEEKLY HOURS WORKED Enter day of week or done to quit: Monday Enter hours worked: 2 Monday 2.0 Enter a day of week or done to quit: Monday Enter hours worked: 4 Monday 4.0 Enter a day of week or done to quit: Tuesday Enter hours worked: 8 Day Total (Monday) 6.0 Tuesday 8.0 Enter a day of week or done to quit: done Day Total (Tuesday) 8.0 Grading When you have completed your program, click the Submit button to record your score.
import java.util.Scanner;
public class SuperMarket { public static void main(String args[]) { // Declare variables. final String HEAD1 = "WEEKLY HOURS WORKED"; final String DAY_FOOTER = " Day Total "; // Leading spaces are intentional. final String SENTINEL = "done"; // Named constant for sentinel value. double hoursWorked = 0; // Current record hours. String hoursWorkedString = ""; // String version of hours String dayOfWeek; // Current record day of week. double hoursTotal = 0; // Hours total for a day. String prevDay = ""; // Previous day of week. boolean done = false; // loop control Scanner input = new Scanner(System.in); // Print two blank lines. System.out.println(); System.out.println(); // Print heading. System.out.println(HEAD1); // Print two blank lines. System.out.println(); System.out.println();
// Read first record System.out.println("Enter day of week or done to quit: "); dayOfWeek = input.nextLine(); if(dayOfWeek.compareTo(SENTINEL) == 0) done = true; else { System.out.print("Enter hours worked: "); hoursWorkedString = input.nextLine(); hoursWorked = Integer.parseInt(hoursWorkedString); prevDay = dayOfWeek; } while(done == false) { // Implement control break logic here // Include work done in the dayChange() method System.out.println("Enter day of week or done to quit:"); dayOfWeek=input.nextLine(); if(dayOfWeek.compareTo(SENTINEL)==0) done=true; else{ System.out.println("Enter hours worked: "); hoursWorkedString=input.nextLine(); hoursWorked=Integer.parseInt(hoursWorkedString); } if(!prevDay.equalsIgnoreCase(dayOfWeek)){ System.out.println(DAY_FOOTER+"(" +prevDay+")"+hoursTotal); hoursTotal = 0; } if(done==false){ System.out.println(dayOfWeek+""+hoursWorked); hoursTotal+=hoursWorked; prevDay=dayOfWeek; } } System.out.println(DAY_FOOTER + "(" + prevDay + ") " + hoursTotal); System.exit(0);
} // End of main() method. } // End of SuperMarket class.
The program work, the only problem I have is I can't get the first employee hours to add with the rest. when I put any day in first the first employee hours do not add with the rest. Example: Monday 6, 3, 4 hours, the only hours adds is the 3 and 4. Why I can't get the first employee hours to add. Please help.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
