Issue
So I'm trying to create a number guessing game in Java. The user thinks of a number between 1 - 100 and then the computer tries to guess the number using a binary search method. So the first guess that the computer makes is 50, if the user chooses higher than it goes to 75 and if its lower than that then it will guess 37.
So I'm trying to set a new upper and lower boundary every time the user enters in his/her choice. If the computer guesses 75 and the user says too high then the new upper boundary should now be 74 and if 37 is too low than 38 becomes the new lower boundary. I am trying to do this within a constructor called NumberGuesser(int lowerBound, int upperBound). It seems that I have the lower limit working, but I need help working the upper boundary. Maybe, you can guys can give my lower boundary a once over to see if that works correctly. I also can't seem to get the computer to guess above 50. If I choose higher, it doesn't go above 50.
Right now we are learning classes in my Java class and I have written most of the decision making algorithm within the class and the objects within my program reference my class called NumberGuesser.
Here is my code:
public class NumberGuesser
{
private int userGuess = 50;
private int max = 100;
private int min = 1;
NumberGuesser(int lowerBound, int upperBound)
{
max = userGuess;
/*if(userGuess > max)
{
max = userGuess;
}
else if(min < userGuess)
{
min = userGuess;
}*/
if(userGuess < max)
{
max = userGuess;
}
else if(userGuess > max)
{
min = userGuess;
}
}
void lower()
{
userGuess = userGuess / 2;
}
void higher()
{
userGuess = ((max - userGuess) / 2) + userGuess;
}
public int getCurrentGuess()
{
return userGuess;
}
void reset()
{
}
}
Main driver class
import java.util.*;
public class Assignment7
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
char guess;
int min = 1;
int max = 100;
NumberGuesser numGuess = new NumberGuesser(min, max);
System.out.println("Please think of a number between 1 and 100");
do
{
System.out.print("Is the number "+numGuess.getCurrentGuess()+"(h/l/c): ");
guess = input.next().charAt(0);
if(guess == 'h')
{
numGuess.higher();
}
else if(guess == 'l')
{
numGuess.lower();
}
else if(guess == 'c')
{
System.out.println("Correct!");
}
}while(guess != 'c');
}
}
Any help would greatly be appreciated guys! Cheers!
Solution
Your code makes no sense. If you're going to pass parameters into a constructor, usually you use those numbers to set class fields, and I see you doing nothing of the sort.
But having said this, I'm not so sure that I'd do this with constructor parameters. What I'd suggest instead would be
- First and foremost, give your class upper and lower bound int fields.
- Change the value held by these fields based on the user's response. Perhaps give it a
setUpperBound(int upperBound)
and asetLowerBound(int lowerBound)
methods. - Use the values to have your class make the next guess.
Based on your driver code, your code calls the constructor once, and that's fine, and you will need to give your class upperBound and lowerBound int fields, as I have already recommended, then set your lowerBound and upperBound with your constructor, which you've yet to do, and then change the values of these fields in your lower()
and higher()
method calls. You will want to attempt to do this now.
In your higher and lower methods, you must first set the upper bound and lower bound, you can use min and max, before making a new guess. Then use the min and max when making the guess. You're not doing this.
Answered By - Hovercraft Full Of Eels
Answer Checked By - Cary Denson (JavaFixing Admin)