Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this assignment, you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number.

For this assignment, you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number. For review, a sample run of the program is printed below. 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.

Testing your NumberGuesser class

You might find it useful to test your number guesser class with the main method in this program. This is not required. Testing NumberGuesser with Random Numbers

What to submit

Submit two .java files in a compressed directory:

NumberGuesser.java

GuessingProgram.java.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions

Question

Find the derivative. f(x) 8 3 4 mix X O 4 x32 4 x32 3 -4x - x2

Answered: 1 week ago