Question
PART I Create the Number Guessing Game In this exercise, youll create a simple number guessing game that allows you to guess the value of
PART I
Create the Number Guessing Game
In this exercise, youll create a simple number guessing game that allows you to guess the value of a random number. Each time you make a guess, the game tells you whether your guess is too high or too low. This process repeats until you guess the correct number. A sample run of the application should look like this:
Welcome to the Number Guessing Game
Enter the upper limit for the number: 50
OK, I'm thinking of a number between 0 and 50
Enter your guess: 25
Your guess is too high.
Enter your guess: 20
Your guess is too low.
Enter your guess: 23
Correct!
Bye!
Create the game
1. Start NetBeans and open the project named ch03_ex3_GuessingGame in the extra_ex_starts directory.
2. Review the existing code. Note the use of the Random class. This code generates a random integer between 0 and upperLimit. For the purposes of this exercise, you dont need to understand how it works.
3. Write the code that prompts the user for an upper limit for the guess. This code should set the upperLimit variable which is already defined.
4. Write the code that prompts the user for their first guess.
5. Write the while loop that allows the user to guess again if their guess was wrong. This loop should inform the user whether their guess was too high or too low. Then, it should prompt the user for a new guess. The loop should exit when the users guess is equal to the random number the computer generated.
6. After the while loop, write code that tells the user they guessed correctly.
7. Run the application and make sure it works correctly. Enhance the game
8. Before the while loop, add a variable to keep track of the number of guesses the user has made. Remember to initialize this variable to 1 instead of 0 since the user will need at least one guess to get the correct number.
9. Inside the while loop, increment the variable by one each time the user guesses incorrectly.
10. After the while loop, add a message that tells the user how many guesses they took.
PART II
Create a class that stores the data for the game
1. In this class, create one instance variable for storing the upper limit of the number, a second for storing the number, and a third for the number of guesses the user has made.
2. Add a constructor to the class that takes an integer value for the upper limit and uses it to set the upper limit instance variable. Then, generate the number that the user should try to guess and set that instance variable. You can copy and modify the relevant lines of code from the main method to do this. Finally, initialize the instance variable for the number of guesses to 1.
3. Add get methods for all three instance variables. Use your IDE to do this if possible. You dont need to create set methods.
4. Add a method named incrementGuessCount that adds 1 to the instance variable for the number of guesses. Use the class
5. In the Main class modify the code so it uses the new object. For example, use the getUpperLimit method to display the upper limit to the user. Then, remove any unnecessary code.
6. Run the project again and makes sure it still works correctly. Add a second constructor and use it
7. In the NumberGame class, add a constructor to the class that takes no arguments. The code for this constructor can call the other constructor in this class and pass it a value of 50 for the upper limit.
8. In the Main class, modify the code so it uses the zero-argument constructor. Then, comment out the statements that get the upper to the user. These statements are no longernecessary since the constructor automatically sets the upperlimit to 50.
9. Run the project agin and makes sue it still works correctly. It should set an upper limit of 50 by default.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started