For this assignment you will revisit the number guessing game in which the user picks a...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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
Expert Answer:
Answer rating: 100% (QA)
Step 1 Algorithm Explanation Initialize NumberGuesser The NumberGuesser class is initialized with a lower and upper bound Main Game Loop The game loop ... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
Planning is one of the most important management functions in any business. A front office managers first step in planning should involve determine the departments goals. Planning also includes...
-
What is the equal payment series for 12 years that is equivalent to a payment series of $25,000 at the end of the first year, decreasing by $1,200 each year over 12 years? Interest is 7% compounded...
-
Antarctica uses a system of fixed exchange rates, its current account deficit is USD 6 billion, and its capital account balance is USD 4 billion. Based on this information, answer the following...
-
Let S represent sales of a new product (in thousands of units), let L represent the maximum level of sales (in thousands of units), and let t represent time (in months). The rate of change of S with...
-
Discuss the sampling strategy and technique to be used. Develop a hypothetical research scenario that would necessitate the use of the Action Research Approach and a Posttest With a Historical...
-
Duggan Company applies manufacturing overhead to jobs on the basis of machine hours used. Overhead costs are expected to total $325,000 for the year, and machine usage is estimated at 125,000 hours....
-
My elevator pitch for the purchase of an MRI machine is as follows: Hi there, my name is Kayla and I am one of the administrators here at Highlands Regional. Did you know that our MRI machine was...
-
Tracy Morgan is self-employed as a professional golf instructor. She uses the cash method of accounting and her SS# is 465-34-3389. Her principal business code is 812990. Tracy's business is located...
-
The addition of 0.351 L of 1.25 M KCl to a solution containing Ag+ and Pb2+ ions is just enough to precipitate all of the ions as AgCl and PbCl2. The total mass of the resulting precipitate is 61.60...
-
Collateral is required as enhancement of the borrowers credit. Elaborate on the purpose and extent of offering security from the perspective of the borrower.?
-
Intro Coca-Cola has expected earnings per share (EPS) of $2. Its competitors have the following P/E ratios: P/E ratio Dr Pepper Nestle Pepsico 22.07 23.71 21.37 Part 1 Attempt 1/10 for 10 pts. What...
-
What can cause a cell to become desensitized to a signal? What are two of the mechanisms that the cell can use to reduce or stop its response to a signal?
-
identify and explain the key features of the organizational policies and procedures related to financial administration. How do these policies influence the budget preparation process, resource...
-
You have found the home of your dreams. You have negotiated the best price for the home, $180,663. You have $13,506 to pay as a down payment. And the best interest rate you can get is 4.03%. Based on...
-
Urinary System Anatomy - Practice on models 1. Name the specialized capillary that is the start of the filtration process. 2. Name the first section of the nephron tubule. 3. Name the u-shaped loop...
-
Refer to the Conservation Ecology (Dec. 2003) study of the causes of forest fragmentation, presented in Exercise 2.166 (p. 97). Recall that the researchers used advanced high-resolution satellite...
-
Which of the following is a definite loop? a. A loop that executes as long as a user continues to enter valid data b. A loop that executes 1,000 times c. Both of the above d. None of the above
-
When you do not know how many items need to be sorted in a program, you can create an array that has _____. a. Variable-sized elements b. At least as many elements as the number you predict you will...
-
a. Draw the hierarchy chart and then plan the logic for a program needed by Hometown Bank. The program determines a monthly checking account fee. Input includes an account balance and the number of...
-
Consider a Lagrangian \(L^{\prime}=L+d f / d t\), where the Lagrangian is \(L=\) \(L\left(q_{k}, \dot{q}_{k}, tight)\), and the function \(f=f\left(q_{k}, tight)\). (a) Show that...
-
Show that the function \(L^{\prime}\) given in the preceding problem must obey Lagrange's equations if \(L\) does, directly from the principle of stationary action. Lagrange's equations do not have...
-
In Example 4.8 we analyzed the case of a bead on a rotating parabolic wire. The energy of the bead was not conserved, but the Hamiltonian was: There is an equilibrium point at \(r=0\) which is...
Study smarter with the SolutionInn App