A05

Due Date: Friday, October 20
File(s) to be submitted: SleepGadget.java
Sample Output: SampleOutput.html


SUBMIT   /   Check

Sleep Gadget (Void Methods)

Synopsis

Create a program, called SleepGadget, which displays a menu of three choices to the user of your program (one choice will be to exit the Menu). When the user selects from the choices available, you will call a method to complete the necessary tasks. After each method has done its task, the menu will be redisplayed. This interaction will continue until the user selects the exit option from the menu which will cause your program to finish running. Ensure that you handle invalid user input without your program crashing or ending prematurely. This assignment will help you practice the following concepts:

Steps

  1. Your program starts by identifying itself and you (its author).
    Sleep Gadget By Somayeh Kafaie Welcome to our sleep app! It will provide you with some fun facts regarding your sleep habits!
  2. Then it presents a menu of options to the user.
    Here are available choices: 1. Find recommended hours of sleep 2. Calculate sleep statistics 0. Exit
  3. Then, it prompts for the choice and reads it:
    Your choice:
  4. After the user selects a valid option, your program carries out the desired task by calling a suitable method.
  5. After carrying out that task, the program repeats steps 2 to 5. (That is, it keeps reading choices until the user chooses the Exit option.)

    Hint: The program is looping until the user enters a zero, and for each valid number it is doing something. How do we write that kind of a loop?

Further Details

You need to implement the following methods:

  1. pause(): to prompt for the Enter key and read it. It should be called from findRecommendedHours() and calculateSleepStatistics() methods.
  2. displayIntroduction(): This methods displays what is explained in step 1 above.
  3. displayChoices(): This methods displays what is explained in step 2 above.
  4. findRecommendedHours(int age): This method will receive the user’s age as a parameter and find the recommended hours of sleep per day for it using the following table.
    Age groupRecommended hours of sleep
    1 year12-16 hours
    2 years11-14 hours
    3-5 years10-13 hours
    6-12 years9-12 hours
    13-18 years8-10 hours
    19-60 yearsAt least 7 hours
    Above 60 years7-9 hours

    Hint: The program (main method) prompts for the age and then passes it to this method when it calls it.

  5. calculateSleepStatistics(): The method prompts for user’s sleep hours in the last few days (one value per day ending with a negative number) and calculates the total number of days for which the user entered a value, their average sleep time per day as well as the maximum and minimum time they have slept per day.

    Hint1: The method is looping until the user enters a negative number. How do we write that kind of a loop?

    Hint2: The statistics should be updated during each iteration of the loop. For each non-negative number, update the current total as well as the minimum and maximum values. After all the numbers have been entered, the method should display the various statistics that have been calculated.

Note1: I recommend that you complete the methods in the order listed above. I also recommend that you start with stubs of all the methods. That way you can test your main method by running the program and seeing that everything happens in the right order. For a stub, the basic idea is to make the smallest possible method that compiles. It generally doesn't do what it's supposed to -- but because it compiles, you can run your driver program.

Note2: Make sure your program code is neat and orderly, and follows the conventions described in the rules for submissions and the style rules, available on the course website.

Btw, Don’t forget to add Javadocs for the class (program) as well as all methods.

Grading Outline

Note: There will be penalties for submissions that do not compile (that Netbeans shows with those red squiggly underlines) or that "crash" on sample input (do input in an unexpected way). The grader may assign a grade of zero if they deem the code too hard to fix.
SUBMIT   /   Check