USING C++
PLEASE CREATE THREE FILES!
- NumberGuesser.h: NumberGuesser class definition
- NumberGuesser.cpp: NumberGuesser class implementation
- main.cpp: main program to run the game using your NumberGuesser class, including sample output at the bottom
Each time that the program guesses a number the user responds by telling the program either the correct answer is higher or lower, or that the program’s guess is correct.
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:
> 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 69? (h/l/c): h
> 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
> Goodbye.
Notice that the program is guessing numbers, and the user is responding by entering 'h', 'l', or 'c' for higher, lower, or correct.
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 a NumberGuesser class. This class will contain all of the logic for remembering the current state of the program’s guesses.
When a new instance of a NumberGuesser class is created 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() member function is called. If the higher() or lower() member functions are called, the NumberGuesser object should adjust its private variables 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(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 then called, the object should adjust its state accordingly so that it knows that the correct value is now between 51 and 100, inclusive, and getCurrentGuess() would now return 75, the midpoint between 51 and 100. If the lower() method is then called, it should adjust its state to represent that the possible values are now between 51 and 74, and getCurrentGuess() should return 62, the midpoint between 51 and 74. By following this strategy the number guesser should be able to eventually guess the proper value that the user guessed.
As another example, here a different NumberGuesser is created with range between 25 and 35:
NumberGuesser littleGuesser(25, 35);
littleGuesser.getCurrentGuess(); // should return 30
littleGuesser.higher(); // adjusts range to 31-35
littleGuesser.getCurrentGuess(); // should return 33
littleGuesser.reset(); // reset range back to original range between 25-35
littleGuesser.getCurrentGuess(); // should return 30
Here is the basic design of the NumberGuesser class that you should write. The private data variables have been left up to you.
NumberGuesser Class
NumberGuesser
Private Data ?? |
Public Member Functions and Constructors NumberGuesser(); NumberGuesser(int lowerBound, int upperBound); void higher(); void lower(); int getCurrentGuess(); void reset(); |
The reset() method should return a NumberGuesser object to the state that it was in when it was constructed, i.e. with its original lowerBound and upperBound reset back to their original values. This allows reusing this same NumberGuesser object to play a new game. Hint: you can create extra member variables in your class to store these original values.
Write your NumberGuesser 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 contain any cin or cout statements. It is only responsible for handling the indicated methods. All of the input and output work should be handled elsewhere in your program, e.g. in main().
Trending nowThis is a popular solution!
Step by stepSolved in 4 steps
- C# program in Visual Studio Code. I need to be able to enter five integer values and then have the Unique values entered display. Private member variable to store 5 unique values (hint: use an Array or a List) Public function to get 5 unique values from the user and store them in the member variable loop to get the numbers, if a number is already stored, ignore it and keep looping until you have 5 unique numbers if a number is out of range, don't store the value, throw an exception and handle it in such a way that you don't break the loop but do message the user that the value was out of rangearrow_forwardc++ code Screenshot and code is mustarrow_forwardPython question Application: Python Fragment Formulation (Q1 – Q4) In this group of questions you are asked to produce short pieces of Python code. When you are asked to "write a Python expression" to complete a task, you can either give the expression in one line or break down the task into several lines. The last expression you write must represent the required task. Question 1 (Reduce parentheses) Give an equivalent version of this expression by removing as many redundant parentheses as possible, without expanding the brackets or simplifying. (x**(2**y))+(y*((z+x)**3)) Question 2 (Translate arithmetic concept into Python) You are given a list of numbers, named numbers, containing 3 integers. Write a python expression (one line) that evaluates to True if and only if the product of any two numbers in the given list is greater than the sum of all three numbers. Note: the product of two numbers, x and y is x*y. Question 3 (List/table access) You are given a table,…arrow_forward
- c++ language using just one for looparrow_forwardComputer Science Part C: Interactive Driver Program Write an interactive driver program that creates a Course object (you can decide the name and roster/waitlist sizes). Then, use a loop to interactively allow the user to add students, drop students, or view the course. Display the result (success/failure) of each add/drop.arrow_forwardInteger userValue is read from input. Assume userValue is greater than 1000 and less than 99999. Assign tensDigit with userValue's tens place value. Ex: If the input is 15876, then the output is: The value in the tens place is: 7 2 3 public class ValueFinder { 4 5 6 7 8 9 10 11 12 13 GHE 14 15 16} public static void main(String[] args) { new Scanner(System.in); } Scanner scnr int userValue; int tensDigit; int tempVal; userValue = scnr.nextInt(); Your code goes here */ 11 System.out.println("The value in the tens place is: + tensDigit);arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY