JAVA Programming
Revisit previous number guessing game in which the user picks a number from 1 to 100 and your program tries to guess the number. Sample runs of the program for refresher: - https://www.youtube.com/watch?v=ypZUZ3sBfaI
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.
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 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 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.
Trending nowThis is a popular solution!
Step by stepSolved in 5 steps with 3 images
- JAVA PROGRAMMING LANGUAGE PLEASE. Convert the following UML to code in the image given below.arrow_forwardBAGEL FILES import javax.swing.JFrame; public class Bagel{//-----------------------------------------------------------------// Creates and displays the controls for a bagel shop.//-----------------------------------------------------------------public static void main (String[] args){JFrame frame = new JFrame ("Bagel Shop");frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new BagelControls()); frame.pack();frame.setVisible(true);}} import java.awt.*;import java.awt.event.*; import javax.swing.*; public class BagelControls extends JPanel{private JComboBox bagelCombo;private JButton calcButton;private JLabel cost;private double bagelCost;public BagelControls(){String[] types = {"Make A Selection...", "Plain","Asiago Cheese", "Cranberry"};bagelCombo = new JComboBox(types);calcButton = new JButton("Calc");cost = new JLabel("Cost = " + bagelCost);setPreferredSize (new Dimension (400,…arrow_forwardDesign a class named CustomerRecord that holds a customer number, name, and address. Include separate methods to 'set' the value for each data field using the parameter being passed. Include separate methods to 'get' the value for each data field, i.e. "return" the field's value. Pseudocode:arrow_forward
- InheritanceDemo.java: // StudentName Today's Date public class InheritanceDemo { public static void main(String[] args) { KleenexBox kb1 = new KleenexBox(); KleenexBox kb2 = new KleenexBox(100); Box justABox = new Box(); System.out.println("justABox has area " + BoxArea(justABox)); System.out.println("kb1 has area " + BoxArea(kb1)); System.out.println("kb2 has this many square inches of tissue: " + KleenexArea(kb2)); } public static double BoxArea (Box b) { return b.width * b.length; } public static double KleenexArea (KleenexBox a) { return a.width * a.length * a.numTissues; } } Box.java: // Student Name Today's Date public class Box { public double width; public double length; public String label; } KleenexBox.java: // Student's Name Today's Date public class KleenexBox extends Box { public int numTissues; // A new Kleenex box public KleenexBox() { numTissues = 50; length = 8.0; width = 5.0; } // A new Kleenex box with a specific number of tissues public…arrow_forwardJava Question - Instructions are in the attached pictures. The picture titled "Screenshot..." are the instructions for building the specific code, while the "Final Output" picture shows what the output of the code should look like. Thank you.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education