Question
(b) In this part, we will write a simple game of guessing an integer. In a dialog box (see the bottom of p.36 for an
(b) In this part, we will write a simple game of guessing an integer. In a dialog box (see the bottom of p.36 for an example of such a dialog box) displaying "Guess a secret integer in the range of 1 to 9 inclusively (-1 to terminate)", the player of the game is asked to guess the correct secret integer in the range. Each time after an integer is entered, one of the following messages is given in another dialog box :
"Congratulations! You are correct. Number of guesses = xx. Bye.", where xx is the number of guesses made by the player.
"The secret number is larger than your guessed number. Please retry."
"The secret number is smaller than your guessed number. Please retry."
(ii) Add another attribute guessCount which stores the number of trials the player made. The attribute should be initialized to zero when it is declared. Write a method increaseCount() which adds 1 to guessCount and its getter method. Copy the declaration and the methods as the answers to this part.
(iii) Write a method oneGuess(int anInteger) which calls increaseCount() and returns
0 if secretInteger is the same as anInteger;
1 if secretInteger is larger than anInteger;
-1 if secretInteger is less than anInteger;
Copy the content of the method as the answers to this part.
(iv) Write a method play() to display the dialog box asking the player to enter an integer, display the appropriate message after checking the guess using oneGuess() and repeat the guessing process until the guess is correct or the player enter -1 to terminate. You can assume the input is a valid integer and no checking is required. Copy the content of the method as the answer to this part.
(v) Write a driver class TestIntegerGuess with a main() method to initialize secretInteger in the range 1 to 9 using random() and play the game. See p.73 of Unit 2 for an example of using random(). Copy the content of the class as the answers to this part.
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