Java programming: Write a RandomNumberGuesser game that extends from the NumberGuesser Game. your program should have : main.java, NumberGuesser.java and a RandomNumberGuesser.java that is an extended class of NumberGuesser. public class RandomNumberGuesser extends NumberGuesser { // your code here } Somewhere in the code in main.java should look something like this: NumberGuesser guesser = new RandomNumberGuesser(1, 100);
Java
Write a RandomNumberGuesser game that extends from the NumberGuesser Game.
your program should have : main.java, NumberGuesser.java and a RandomNumberGuesser.java that is an extended class of NumberGuesser.
public class RandomNumberGuesser extends NumberGuesser {
// your code here
}
Somewhere in the code in main.java should look something like this:
NumberGuesser guesser = new RandomNumberGuesser(1, 100);
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 4 images
Here is another feature we are asked to add to Number Guessing game. A "Guess History". When the program guesses the correct number, it should show the guesses it made along the way.
We'll return to our command line game. Here is a sample run with the new feature:
Guess a number from 1 to 100.
Is it 27? (h/l/c): h
Is it 86? (h/l/c): l
Is it 68? (h/l/c): l
Is it 59? (h/l/c): l
Is it 40? (h/l/c): c
Here are the guesses: 27 86 68 59 40
Great! Do you want to play again? (y/n): y
Guess a number from 1 to 100.
Is it 44? (h/l/c): l
Is it 27? (h/l/c): c
Here are the guesses: 44 27
Great! Do you want to play again? (y/n): n
The last lines of each game are outcome of new added feauture.
You can add the feature by adding a method to NumberGuesser that returns the guesses using this data type: ArrayList<Integer>
The signature should look like this:
public ArrayList<Integer> getGuessHistory()
The method will be inherited by RandomNumberGuesser. Make sure that it works in both classes. Once it is working you will be able to print out the guess history like this:
System.out.print("Here are the guesses: ");
ArrayList<Integer> guesses = guesser.getGuessHistory();
for (Integer g: guesses) {
System.out.print(g + " ");
}
System.out.println();
HINT:
You can add a member field to NumberGeusser that will hold the ArrayList:
private ArrayList<Integer> guessHistory;
You should initialize that member field in the constructor, and re-initialize it in the reset() method.
Your getGuessHistory method should return a copy of that member field. You can make a copy using the ArrayList constructor:
return new ArrayList<Integer>(this.guessHistory);
You could add a protected helper method to NumberGuesser that adds a guess into that array list:
protected void addGuess(int guess) {
this.guessHistory.add(guess);
}
That could be called where it is that you want to add the guesses to the history. Make sure not to add duplicates.
We are asked to add an exception to the game. Please see below.
For this part of the assignment you can start by creating your own exception in a file named NumberGuesserIllegalStateException.java, with this code:
public class NumberGuesserIllegalStateException extends Exception {
public NumberGuesserIllegalStateException(String errorMessage) {
super(errorMessage);
}
}
Next modify your code so that the higher and lower methods of both the NumberGuesser and RandomNumberGuesser should throw the exception if there are no more remaining numbers to guess. You might be able to achieve this by adding the logic to your NumberGuesser and letting RandomNumberGuesser inherit the behavior. Or you might need to add the logic to both classes. It will depend on your implementation.
Finally add a try-catch block to your number guessing game so that the user is notified if the user cheats.