Question: In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in
In the example the user picks the value 72 for the first game, and then 25 for the second game. The user's input is in bold. Here is how the output might look (exact numbers may vary) :
> Think of a number between 1 and 100
> Is the number 50? (h/l/c): h
> Is the number 75? (h/l/c): l
> Is the number 62? (h/l/c): h
> Is the number 68? (h/l/c): h
> Is the number 71? (h/l/c): h
> Is the number 73? (h/l/c): l
> Is the number 72? (h/l/c): c
> You picked 72? Great pick.
> Do you want to play again? (y/n): y
> Think of a number Between 1 and 100
> Is the number 50? (h/l/c): l
> Is the number 25? (h/l/c): c
> You picked 25? Great pick.
> Do you want to play again: (y/n): n
> Good bye.
The point of interest for us is not necessarily the game - but rather the design of the program. The essential part of this assignment is writing the NumberGuesser class. This class will contain all of the logic for guessing.
Write your NumberGuesser class as if it is going to be used in many different guessing games, created by different developers. You want to create a class that will be a useful tool in different contexts.
When a new instance of a NumberGuesser class is instantiated the upper and lower bounds of the possible values should be passed into its constructor. From that point on a NumberGuesser object will always return the mid-point of the possible values when the getCurrentGuess() method is called.
If the higher() or lower() methods are invoked, the NumberGuesser object should adjust its state to represent the new possible range of values. For example, if a NumberGuesser is created with the following line of code then the range will be the numbers from 1 to 100:
NumberGuesser guesser = new NumberGuesser(1, 100);
If the getCurrentGuess() method is called it should return 50, which is the midpoint between 1 and 100. If the higher() method is invoked then the object should adjust its state accordingly so that it knows that the correct value is between 51 and 100. If the lower() method is invoked then it should adjust its state to represent that the possible values are between 1 and 49.
After that, the getCurrentGuess() should return the value that is in the middle of the new range of possible values. (Either 75 or 25). By following this strategy the number guesser should be able to eventually guess the proper value.
Here is the basic design of the NumberGuesser class that you should write. The instance variables have been left up to you.
NumberGuesser Class
| Private Instance Variables ?? |
| Public Methods and Constructors
NumberGuesser(int lowerBound, int upperBound)
void higher(); void lower(); int getCurrentGuess(); void reset(); |
The reset() method should return the return a NumberGuesser to the state that it was in when it was constructed. In order to do this your class will need to be able to remember its original state. You can use two additional instance variables to store the original upper and lower bounds.
Write your number guess class and test it until you are sure that it is working properly. After it is working, write the rest of the program so that it plays the game by using an instance of your NumberGuesser class.
Note: Your NumberGuesser class should not use a Scanner object or System.out. It is only responsible for handling the indicated methods. All of the input and output work should be handled elsewhere in your program.
What to submit
Submit two .java files in a compressed directory:
NumberGuesser.java
GuessingProgram.java.
Then, rewrite the high scores assignment so that the names and scores are stored in an array of HighScore objects instead of parallel ArrayLists.
The new Program should have the same output as the original. Here is a sample run of the program:
Enter the name for score #1: Suzy
Enter the score for score #1: 600
Enter the name for score #2: Kim
Enter the score for score #2: 9900
Enter the name for score #3: Bob
Enter the score for score #3: 1012
Enter the name for score #4: Armando
Enter the score for score #4: 8000
Enter the name for score #5: Tim
Enter the score for score #5: 514
Top Scorers:
Kim: 9900
Armando: 8000
Bob: 1012
Suzy: 600
Tim: 514
Step 1: The HighScores class
Begin by creating the HighScore class. It should have the following design
Class HighScore
| String name int score |
| HighScore(String n, int s)
void setName(String n) String getName()
void setScore(int s) int getScore() |
Step 2: The HighScoresProgram
Create a separate file named HighScoresProgram.java. This file should contain a class that has only four following static methods.
public static void main(String args[])
public static void initialize(HighScores[] scores)
public static void sort(HighScores[] scores)
public static void display(HighScores[] scores)
The main method should allocate an array of five HighScore references, and then invoke the other three methods.
What to Submit?
Submit HighScores.java HighScoresProgram.java
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
