Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java > Think of a number between 1 and 100 > Is the number 50? (h/1/c): h > Is the number 75 (h/l/c):1 Is the

java
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
> Think of a number between 1 and 100 > Is the number 50? (h/1/c): h > Is the number 75 (h/l/c):1 Is the number 62 (h/l/c): h > Is the number 68? (h/1/c): h > Is the number 71? (h/1/c): h > Is the number 73 (h/1/c):1 > Is the number 72? (h/l/c): c > You picked 72? Great pick > Do you want to play again? (y) : y Think ofa number Between 1 and 100 > Is the number 50? (h/1/c):1 > Is the number 25? (h/l/c): c You picked 25? Great pick. >Do you want to play again: (y): 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 100); new NumberGuesser(1, 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 NunberGuesser (int lowerBound, int upperBound) void higher ; void lower int getCurrentGuess (0 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 bee 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 import java.util.Scanner; public class NumberGuesser //Midpoint of numbers public static int getMidpoint(int low, int high) return (lowthigh)/2; / User input if numbers is higher lower or correct public static char getUserResponse ToGuess (int guess) System.out.print ('is it "+guess? (h/l/c):): Scanner scnew Scanner (System.in): return sc.next ().charAt (8): //Asks user if they would like to continue playing public static boolean shouldPlayAgain) Scanner sc new Scanner (System.in): return sc.next().tolowerCase().charAt(0)y public static void playOneGame(EEE/mHmld System.out.println( Guess a number between 1 and 188.): int start 1; int end 100; int mid; while(start

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions