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 from 1 to 100 and your program

image text in transcribed image

For this assignment you will revisit the number guessing game in which the user picks a number from 1 to 100 and your program tries to guess the number. Here are some sample runs of the program if you want a refresher: Sample Runs. To the user this week's program will look the same. But in the first assignment we structured the logic using static methods in Main.java. This time we will move the number guessing logic into a class named NumberGuesser. We will define that class in NumberGuesser.java. The number guesser class has three essential methods: int getCurrentGuess() . void higher() void lower() Your class should use member fields to keep track of the range of possible guesses. The getCurrentGuess method should return the midpoint of this range. The higher() and lower() methods are void methods. They do not accept parameters and they do not return values. When a number guesser has its higher() method invoked it should interpret it like this: "The correct guess is higher than the current guess". Similarly when the lower() method is invoked it should interpret that as meaning "The correct guess is lower than the current guess()". In those cases a NumberGuesser can change its private member fields to reflect this new information. Inside your game code in Main.java (perhaps in the static playOneGameMethod) you will have logic that creates a NumberGuesser object and then calls these three methods. In pseudo code it might look like this: create a new number Guesser object while the user has not responded with a 'c' guess = numberGuesser.getCurrent Guess () get the users response to this guess ('h', '1', or 'c') if response is 'h' guesser guesser.higher () if response if '1' guesser.lower () This video captures some more thoughts on how Main and NumberGuesser work together: NumberGuesser and Main Main.java int guess guesser.getCurrentuess NumberGuesser lower Bound $1 upperBound 180 Creating the NumberGuesser class 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. Later this semester we will use it in a program with a Graphical User Interface (GUI). 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 midpoint 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 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 Member Fields ?? Public Methods and Constructors NumberGuesser (int lowerBound, int upperBound) void higher (); void lower (); int getCurrentGuess (); void reset (); The reset() method should return the 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. You might not need to invoke this method in your game for this project, but you will need to in later projects. 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

Heres an example implementation of the NumberGuesser class based on the provided requirements java public class NumberGuesser private int lowerBound p... 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

Programming Logic & Design Comprehensive

Authors: Joyce Farrell

9th edition

978-1337102070

More Books

Students also viewed these Programming questions

Question

Is the human body radioactive? Explain.

Answered: 1 week ago