Question

In: Computer Science

Assignment - Number Guessing with a Class For this assignment you will revisit the number guessing...

Assignment - Number Guessing with a Class

For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number.

For review, 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 (exact numbers may vary) :

> 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 68? (h/l/c): h

> Is the number 71? (h/l/c): h

> Is the number 73? (h/l/c): l

> 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

> 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 = 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 Instance Variables

??

Public Methods and Constructors

NumberGuesser(int lowerBound, int upperBound)

void higher();

void lower();

int getCurrentGuess();

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 be 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.

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

What to submit

Submit two .java files in a compressed directory:

  • NumberGuesser.java
  • GuessingProgram.java.

For this assignment you will revisit the number guessing game in which the user picks a number, and your program tries to guess the number.

For review, 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 (exact numbers may vary) :

> 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 68? (h/l/c): h

> Is the number 71? (h/l/c): h

> Is the number 73? (h/l/c): l

> 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

> 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 = 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 Instance Variables

??

Public Methods and Constructors

NumberGuesser(int lowerBound, int upperBound)

void higher();

void lower();

int getCurrentGuess();

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 be 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.

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:

https://www.google.com/url?q=https://docs.google.com/document/d/1WtbkpOZ3OoKuf94HaBCC5or8-dUjClNey6C0wKUNi28/pub?embedded%3Dtrue&sa=D&ust=1571212772373000

What to submit

Submit two .java files in a compressed directory:

  • NumberGuesser.java
  • GuessingProgram.java.

Solutions

Expert Solution

Code

NumberGuesser.java class file


public class NumberGuesser
{
private int originalUpperBound,originalLowerBound;
private int currentUpperBound,currentLowerBound;

public NumberGuesser(int originalLowerBound,int originalUpperBound)
{
this.originalUpperBound = originalUpperBound;
this.originalLowerBound = originalLowerBound;
  
this.currentLowerBound=originalLowerBound;
this.currentUpperBound=originalUpperBound;
}
  
public int getCurrentGuesse()
{
return ((currentUpperBound-currentLowerBound)/2)+currentLowerBound;
}
  
public void higher()
{
currentLowerBound=getCurrentGuesse();
}
public void lower()
{
currentUpperBound=getCurrentGuesse();
}
public void reset()
{
this.currentLowerBound=originalLowerBound;
this.currentUpperBound=originalUpperBound;
}
}

TestClass.java contains the main method that will check the above class


import java.util.Scanner;


public class TestClass
{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
String again,userResponce;   
NumberGuesser obj=new NumberGuesser(1, 100);
do
{
obj.reset();
System.out.println("Think of a number between 1 and 100");
while(true)
{
System.out.print("Is the number "+obj.getCurrentGuesse()+"? (h/l/c):");
userResponce=input.next();
if(userResponce.equalsIgnoreCase("h"))
obj.higher();
else if(userResponce.equalsIgnoreCase("l"))
obj.lower();
else if(userResponce.equalsIgnoreCase("c"))
{
System.out.println(" You picked "+obj.getCurrentGuesse()+"? Great pick.");
break;
}
}
System.out.print("Do you want to play again? (y/n): ");
again=input.next();
}while(again.equalsIgnoreCase("y"));
System.out.println("GoodBye");
}
  
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

This is for java For my assignment, I was supposed to make a number guessing game....
This is for java For my assignment, I was supposed to make a number guessing game. The class, HiLo, should pick a random number between 1 and 100 (inclusive). Then, prompt the user to guess the number. On each guess, print if the user is correct or if the guess is too high or too low. Allow only 5 guesses. In the end, print out the correct answer and how many attempts were made. Then give the user the option...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program...
You are asking to develop a “Triangle Guessing” game in Python for the assignment. The program accepts the lengths of three (3) sides of a triangle as input from a player. The program output should indicate whether the triangle is a right triangle, an acute triangle, or an obtuse triangle. 1.Tips to help you for this assignment: Make sure each side of the triangle is a positive integer. Use try/except for none positive integer input. 2. Validating the triangle. That...
guessing game in Java. It will have a guess input used for guessing the random number...
guessing game in Java. It will have a guess input used for guessing the random number that is generated from 1 - 100. When the user makes a guess it will tell them if the number is lower or higher than the guess. There is also a choice to give up which then reveals the correct number. The last choice will be new game which resets the random number. Last, the program should keep counts of tries. When the user...
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
Guessing the class average game: Suppose we have 30 students enrolled in intermediate microeconomics class and...
Guessing the class average game: Suppose we have 30 students enrolled in intermediate microeconomics class and everyone participates in the game to guess the class average. Each student can pick an integer between 0 and 100( with 0 and 100 included), whoever picks the number that is closest to the class average( calculated from all the guessed numbers) will be the winner(s). What is (are) the Nash Equilibrium( Equilibria) for this game? Explain.(1 point) Guessing the 1/3 of class average...
using xsd, Create a number guessing game You would need an input box and buttons Ask...
using xsd, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison ​a. Make sure to provide hints back to the user if the number enter is low or high ​​-this element should be automatically added ​b. Clear the value enter in the text ​c. Give them 3 tries Whether the user wins or loses, display all the guesses along with the...
using xml, Create a number guessing game You would need an input box and buttons Ask...
using xml, Create a number guessing game You would need an input box and buttons Ask the user for a number Produce a random number from 1-50 Do the comparison             a. Make sure to provide hints back to the user if the number enter is low or high                         -this element should be automatically added             b. Clear the value enter in the text             c. Give them 3 tries Whether the user wins or loses, display all the...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number...
Write a Java program that implements the Number Guessing Game: 1. First generate a random number (int) between 0 and 100, call it N 2. Read user input (a guess) 3. check the number, if it's smaller than N, output "The number is larger than that" 4. If the input is larger than N, output "The number is smaller than that" 5. If the input is equal to N, output " You got it!", and exit 6. Repeat until the...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT