Write a subclass of the NumberGuesser class named RandomNumberGuesser . The subc
ID: 3710043 • Letter: W
Question
Write a subclass of the NumberGuesser class named RandomNumberGuesser. The subclass should change the behavior of the getCurrentGuess method.
In the current NumberGuesser class the getCurrentGuess method returns the midpoint of the range of possible values. In the RandomNumberGuesser class the getCurrentGuess 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.println(ng.getCurrentGuess());
System.out.println(ng.getCurrentGuess());
System.out.println(ng.getCurrentGuess());
Each invocation of getCurrentGuess should return 5. The value should not change until the higher() or lower() 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.
What to Submit
Submit a zip file that contains your NumberGuesser.java file, and RandomNumberGuesser.java file, and a working guessing game that uses the RandomNumberGuesser.
Explanation / Answer
Hi Dear,
Please find my implementation.
Output:
Get a number between 1 and 10
Was that number : 3? [Higher(h)/Lower(l)/Correct(y)]
l
Was that number : 1? [Higher(h)/Lower(l)/Correct(y)]
h
Was that number : 2? [Higher(h)/Lower(l)/Correct(y)]
y
You picked 2.
Great.!
Wanna Play Again(y/n)?
n
Bye!
Process finished with exit code 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.