(b) In this part, we will write a simple game of guessing an integer. In a dialo
ID: 3698438 • Letter: #
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 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.
Explanation / Answer
Since the programing language to be used to achieve the given requirement is not explicitly mentioned.... Java has been used in the following solution. And since the type of Dialogue Box is not mentioned properly, The messages are just printed in the following code:
import java.io.*;
import java.util.*;
public class TestIntegerGuess {
static Scanner in=new Scanner (System.in);
static int guessCount = 0;
static int secretInteger;
static int enteredInteger;
public static void increaseCount()
{
guessCount++;
}
public static int oneGuess(int anInteger)
{
increaseCount();
if(anInteger>secretInteger)
return -1;
else if(anInteger<secretInteger)
return 1;
else
return 0;
}
public static void play()
{
enteredInteger = in.nextInt();
if(enteredInteger==-1)
System.exit(0);
else {
if(oneGuess(enteredInteger)==0)
{
System.out.println("Congratulations! You are correct. Number of guesses = "+guessCount+". Bye.");
System.exit(0);
}
else if(oneGuess(enteredInteger)==-1)
{
System.out.println("The secret number is smaller than your guessed number. Please retry.");
guessCount--;
play();
}
else if(oneGuess(enteredInteger)==1)
{
System.out.println("The secret number is larger than your guessed number. Please retry.");
guessCount-=2;
play();
}
}
}
public static void main(String[] args) {
Random rand = new Random();
secretInteger = rand.nextInt(10)+1;
System.out.println("Guess a secret integer in the range of 1 to 9 inclusively (-1 to terminate) ");
play();
}
}
Assuming that the type of Dialogue Box is Alert Dialogue, you can refer the following code snippet in order to achieve the neccessary;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.