Date: Wednesday, February 19, 2020.
Due date: Wednesday, February 26, 2020.
Objective: To use the while loop in Java.
In this project, we will continue the work we did for Lab 5 asking the user to guess a secret number. However, in this updated lab we will keep asking the user to make a guess until they get it right.
Create a Java project in Eclipse and call it lab6. Create a class in this project called GuessNumber and containing the main method. Write your name, course, semester, and homework number at the top as a comment.
a. Reuse the code.
Copy the code from the the function main from the file SecretNumber.java from the previous lab into the function main in this lab. Copy the two import statements from the previous lab. Run the code to make sure it still works.
b. Guessing the number
Let's modify the program so that it allows the user multiple attempts until they guess the number. For this, we need to place most of the code in a while loop.
For this, initialize the value of the variable guess to 15. This is a value larger than the secret number, so we know that they will be different at the start.
Then right after you initialize the secret number, add a while loop where the condition is that the guess is not equal to the secret number. Open a brace after the while and close it after the second else. Select the code between the while and the end of the program and format it (Source menu - Format).
Now the conditional inside the loop is almost right. We don't want it to tell us what the secret number is, since we're going to guess it. From the second else, delete the statement that outputs the secret number. Now the program should be working.
b. Number of attempts.
Let's add another feature to the program so that it also tells us how many attempts it took to guess the number. Add another variable called attempts and initialize it to 0. Then inside the loop, after the input, increment this variable. Finally, in the output statement for when the user guesses the number, add a message with the number of attempts.
c. Play again.
Let's make the program ask the user if they want to play again. Declare a variable called again and initialize it as 1. Then in the continuation condition for the while loop, add && again == 1. This way, when the value of this variable changes, the game will end.
In the loop, in the case where the user has guessed the number, place the output statement in curly braces. Then output a statement asking the user to enter 1 to play again, and 0 to quit. Read the answer into the variable again. Then test if the value entered was 1, and if that's the case, reset the values of the secret number, guess, and attempts to a random value, 15, and 0 respectively, as you've done before the loop.
Add an output statement after the loop saying goodbye to the user, so that they know that program has ended.
Run the program several times to make sure everything works correctly.
Upload GuessNumber.java to Canvas in Assignments - Homework 6, or wait until you have completed the homework to upload both files at the same time.