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:
- void methods without parameters
- void methods with parameters
Steps
-
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!
-
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
-
Then, it prompts for the choice and reads it:
Your choice:
-
After the user selects a valid option, your program carries out the desired task by calling a
suitable method.
-
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:
-
pause(): to prompt for the Enter key and read it. It should be called from
findRecommendedHours() and calculateSleepStatistics() methods.
-
displayIntroduction(): This methods displays what is explained in step 1 above.
-
displayChoices(): This methods displays what is explained in step 2 above.
-
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 group | Recommended hours of sleep |
1 year | 12-16 hours |
2 years | 11-14 hours |
3-5 years | 10-13 hours |
6-12 years | 9-12 hours |
13-18 years | 8-10 hours |
19-60 years | At least 7 hours |
Above 60 years | 7-9 hours |
Hint: The program (main method) prompts for the age and then passes it to this
method when it calls it.
-
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
- 5 points: pause() has been implemented properly.
- 5 points: pause() has been called from the right places.
- 10 points: displayIntroduction() has been implemented properly.
- 10 points: displayMenu() has been implemented properly.
- 5 points: findRecommendedHours() has the right set of parameter(s).
- 10 points: findRecommendedHours() has the proper logical implementation: the
method prints the correct recommendation for each given age group
- 5 points: calculateSleepStatistics() reads numbers properly.
- 5 points: calculateSleepStatistics() calculates average correctly.
- 5 points: calculateSleepStatistics() calculates max and min correctly.
- 5 points: calculateSleepStatistics() prints the message requested properly.
- 5 points: the program redisplay the menu as long as the user hasn’t requested to exit.
- 5 points: the program handles invalid inputs properly.
- 5 points: the program shows the requested message for exit option before exiting.
- 8 points: Proper code formatting and meaning variable names.
- 12 points: Proper Javadoc for the class and all defined methods.
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