Java RandomNumberGuesser Write a subclass of the NumberGues
Java - RandomNumberGuesser
Write a subclass of the NumberGuesser class named RandomNumberGuesser. The subclass should change the behavior of the getCurrentGuess method. In the current Number Guesser class the getCurrentGuess method returns the midpoint of the range of possible values. In the RandomNumberGuesser class the getCur rentGuess should return a randomly generated number in the range of possible values. Note that repeated calls to getCurrentGuess() should always return the same value for both classes if neither the higher() or the lower() methods are called. Consider the following example: NumberGuesser ng = new NumberGuesser (1, 10); System, out .prirntln (ng.getCurrentGuess ()); System.out .prir.tln (r.g.getCurrentGuess ()); System.out .println (ng.getCurrentGuess ()); Each invocation of getCurrentGuess should return 5. The value should not change until the higher() or lowerQ method is invoked. Now consider a RandomNumberGuesser example: RandomNumberGuesser rng = new RandomNumberGuesser(1, 10); System.out.println(rng.getCurrentGuess()); System.out.println(rng.getCurrentGuess ()); System.out.println(rng.getCurrentGuess()); The first invocation of getCurrentGuess() should return a random number between 1 and 10. Each subsequent call should return the same number, until higher() or lower() is invoked.You can modify your NumberGuesser to make it a better class for subclassing. You might want to make instance variables or methods protected instead of private. So long as your NumberGuesser class still works as originally described any changes are fine.Submit a zip file that contains yourNumberGuesser.java file, and RandomNumberGuesser. java file, as well as any other code needed for your project to compile.Solution
Please follow the code and comments for description :
CODE :
NumberGusser.java :
public class NumberGuesser { // class to get the code
protected int minVal; // instance variables
protected int maxVal;
private int lowerBoundValue;
private int upperBoundValue;
public NumberGuesser(int lowerBound, int upperBound) { // constructor
this.minVal = lowerBoundValue = lowerBound;
this.maxVal = upperBoundValue = upperBound;
}
public void higher() { // method to get the higher number
minVal = getCurrentGuess() + 1;
}
public void lower() { // method to get the lower number
maxVal = getCurrentGuess() - 1;
}
public int getCurrentGuess() { // method to get the current number
return (maxVal - minVal) / 2;
}
public void reset() { // method to reset
minVal = lowerBoundValue;
maxVal = upperBoundValue;
}
}
RandomNumberGuesser.java :
import java.util.Random; // required imports
public class RandomNumberGuesser extends NumberGuesser { // class that extends the main class to get a subclass
private int randMidPt; // instance variables
private boolean testVal;
private Random newRandVal;
public RandomNumberGuesser(int lowerBound, int upperBound) { // constructor with the parameters
super(lowerBound, upperBound);
testVal = true;
newRandVal = new Random();
}
@Override
public int getCurrentGuess() { // overriden method
if (testVal) {
randMidPt = minVal + (newRandVal.nextInt((maxVal - minVal) + 1)); // returns a random value from the higher and the lower values
testVal = false;
}
return randMidPt;
}
@Override
public void higher() { // overriden method
super.higher();
testVal = true;
}
@Override
public void lower() { // overriden method
super.lower();
testVal = true;
}
@Override
public void reset() { // overriden method
super.reset();
testVal = true;
}
}
GuessingGameProgram.java :
import java.util.*; // required imports
public class GuessingGameProgram { // class to run the code
public static void main(String[] args) { // driver method
int lowerBound = 1; // instance variables
int upperBound = 10;
char choice = \'x\';
boolean play = false;
boolean correct = true;
int midpoint = 0;
RandomNumberGuesser guessNum = new RandomNumberGuesser(lowerBound, upperBound); // calling the class by passing the parameters to the constructor
Scanner sc = new Scanner(System.in);
do {
guessNum.reset(); // reset the values
System.out.println(\"Get a number between \" + lowerBound + \" and \" + upperBound); // print the values to console
correct = true;
do {
midpoint = guessNum.getCurrentGuess(); // get the mid value
System.out.println(\"Was that number : \" + midpoint + \"? [Higher(h)/Lower(l)/Correct(y)]\"); // as for the user
choice = sc.next().charAt(0); // get the value
if (choice == \'h\' || choice == \'H\') { // check for the choice
guessNum.higher();
} else if (choice == \'l\' || choice == \'L\') {
guessNum.lower();
} else if (choice == \'y\' || choice == \'Y\') {
correct = false;
}
} while (correct); // iterate
System.out.println(\"You picked \" + midpoint + \".\ Great.!\"); // print the result
System.out.println(\"Wanna Play Again(y/n)?\");
char playAgain = sc.next().charAt(0);
if (playAgain == \'y\' || playAgain == \'Y\') { // ask for a game
play = true;
} else {
play = false;
}
} while (play); // play again
System.out.println(\"Bye!\"); // exit
}
}
OUTPUT :
Get a number between 1 and 10
Was that number : 2? [Higher(h)/Lower(l)/Correct(y)]
h
Was that number : 6? [Higher(h)/Lower(l)/Correct(y)]
h
Was that number : 9? [Higher(h)/Lower(l)/Correct(y)]
l
Was that number : 8? [Higher(h)/Lower(l)/Correct(y)]
l
Was that number : 7? [Higher(h)/Lower(l)/Correct(y)]
y
You picked 7.
Great.!
Wanna Play Again(y/n)?
n
Bye!
Hope this is helpful.


