Write a Java class that provides methods to play a guessing game. It has three i
ID: 3930002 • Letter: W
Question
Write a Java class that provides methods to play a guessing game. It has three instance variables to store an integer to be guessed, the number of guesses, and whether a correct guess has been received. The constructor intializes all three instance variables. The number to be guessed is generated pseudo- randomly between 1 and 100 (inclusive) using the random () method of the Math class. Implement the following methods: .guess ): accepts a guess as a parameter, and returns -1 if the guess is too low, 1 if the guess is too high, and 0 if the guess is correct. .getNumberofGuesses ): returns the number of guesses made so far. .gameIsComplete : returns true if the game is complete (the number has been guessed correctly). false otherwise. .reset : resets all instance variables so that a new game can be played.Explanation / Answer
Please follow the code and comments for description :
CODE :
public class GuessingGame { // class to run the code
int integerToGuess; // instance variables
int numberOfGuesses;
boolean correctGuessReceived;
int guessApprox;
public GuessingGame(int intToGuessIn, int numOfGuessesIn, boolean correctGuessIn) { // constructor to get the data
integerToGuess = intToGuessIn;
numberOfGuesses = numOfGuessesIn;
correctGuessReceived = correctGuessIn;
}
//integerToGuess = (100 * Math.random());//identifier expected?
public int guess(int userGuess) { // method to guess the value the user given
if (userGuess < integerToGuess) {
guessApprox = -1; //guess is too low
} else if (userGuess > integerToGuess) {
guessApprox = 1; //guess is too high
} else {
guessApprox = 0; //guess is correct
}
return guessApprox;
}
public int getNumberOfGuesses() { // method to return the number of guesses
return numberOfGuesses;
}
public boolean gameIsComplete() { // method that checks if the game is done or not
correctGuessReceived = false;
if (guessApprox == 0) {
correctGuessReceived = true;
}
return correctGuessReceived;
}
public void reset() { // method to reset the values
integerToGuess = 0;
numberOfGuesses = 0;
correctGuessReceived = false;
}
public static void main(String[] args) { // temporary main driver method
GuessingGame gg = new GuessingGame(1, 5, true); // passing the values to the constructor
System.out.println("Guess Approx Value : "+gg.guess(1)); // checking the result of the guess
System.out.println("Result? "+gg.gameIsComplete()); // check if the result is winor loss
}
}
OUTPUT :
Case 1 :
Guess Approx Value : 0
Result? true
Case 2 :
Guess Approx Value : 1
Result? false
Description :
Here the glitch is that the value (guessApprox) that we use for the guess value checking is a local variable to the method so that cannot be used in the other methods, but we have used in the method gameIsComplete to check if the game is completed or not which is not accessible by the method that results in a compilation error. To over come this we need to make the variable global or an intanse variable so that the value could be found anywhere in the class.
If this is not the case the we need to call the mathod to get the guessApprox value in the main method and then check for the game is a win or not by passing the same value to the gameIsComplete method, and thus changes the method signature.
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.