# Guessing class

• (2 Pages)
• 1
• 2

## 19 Replies - 1311 Views - Last Post: 19 February 2012 - 04:41 PMRate Topic: 2 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=267134&amp;s=eead4a5664b6f4d1f95642a5e23d2d6c&md5check=' + ipb.vars['secure_hash'], cur_rating: 3, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

# Guessing class

Posted 17 February 2012 - 09:34 PM

```import java.util.Random;

public class GuessMyNumber
{
static int randomNumber;
static int count; //checks the number of times the guess method was called.
static int currentGuess;

public GuessMyNumber()
{
Random r = new Random();
System.out.println("Guess: " + (randomNumber = r.nextInt(99)+1));
count = 0;
}

public int guess(int guess)
{
currentGuess = guess;
count++;
if(guess < randomNumber)
{
return -1;
}
else if(guess == randomNumber)
{
return 0;
}
else
{
return 1;
}
}
}

```

```public class HalvsiesGuesser extends Guesser
{
int lastGuess = 50;
int count = 0;

public boolean guess(GuessMyNumber guessingGame)
{
int nextGuess = 0;
boolean guess = false;

for(int i = 1; i < Integer.MAX_VALUE; i++)
{
if(guessingGame.guess(lastGuess) == -1)
{
guess = false;
nextGuess = lastGuess/2;
lastGuess = nextGuess;
}
else if(guessingGame.guess(lastGuess) == 0)
{
guess = true;
break;
}
else
{
guess = false;
nextGuess = ((lastGuess + nextGuess)/2);
}
System.out.println(lastGuess);
count++;
}
return guess;
}

public int getNumberOfGuesses()
{
return count;
}

public void reset()
{
GuessMyNumber.currentGuess = lastGuess;
}
}

```

my halvesguesser method isn't working the way it is supposed to. It should work something like this:

Randomnumber = 27
it's first guess should be 50 too high, the next guess should be 25 too low, the next guess = 37 too high, 18 too low, 27 guess correct. what is it that I am doing wrong?

Is This A Good Question/Topic? 0

## Replies To: Guessing class

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Guessing class

Posted 17 February 2012 - 09:54 PM

What the hell are you trying to code ?

### #3 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 17 February 2012 - 10:00 PM

read the example it explains it all

here is the details I got:

1. Create a class called 'GuessMyNumber'. The constructor should chose a random number between 1 and 100. There should be a method 'guess(int guess)' that returns -1 if the guess is too low, 0 if the guess is correct, and 1 if the guess is too high.

2. Create a class called 'HalvsiesGuesser' that overrides the guess method. It should take the full range of numbers possible and guess the middle one each time. (for example, it's first guess should be 50, if that's too high, the next guess should be 25....) Reset should be overridden to reset the guess to 50.

This post has been edited by jot121: 17 February 2012 - 10:00 PM

### #4 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Guessing class

Posted 17 February 2012 - 10:07 PM

Perhaps if you told us what it is doing instead of not doing, we don't to have to guess what is wrong with your program, "its not working the way it's supposed to" is very vague.

Edit: ok, that helps with the information

Edit2: You never gave us a guesser class

This post has been edited by Mylo: 17 February 2012 - 10:12 PM

### #5 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 17 February 2012 - 10:17 PM

oh sorry.
it is printing the Integer.MAX_VALUE for the getnumberofguessesmethods.

and when I try to print last guess, it prints 25 for Integer.MAX_VALUE times

the guesser classes is not connected to any of the classes above

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Guessing class

Posted 17 February 2012 - 10:18 PM

withing the for() loop that goes from 1 to Interger.MAXVALUE, the hell if I know why, why are you setting guess to false or true if the only case you will exit the loop if is guess == true ?
Your boolean guess() method can only return true so completly useless to make it a boolean method

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Guessing class

Posted 17 February 2012 - 10:47 PM

jot121, on 18 February 2012 - 12:17 AM, said:

and when I try to print last guess, it prints 25 for Integer.MAX_VALUE times

Really not a very good algorithm if it can take up to Intger.MAX_INTEGER iterations to find the number

```int findNumber(int n) {
int i;
for(i = 0; i < Integer.MAX_VALUE; ++i) {
if(i == n)
break;
}
return i;
}

```

Scratch all your code and start over again

### #8 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 17 February 2012 - 10:51 PM

I am not supposed to change the setting of the method. so its not what I was looking for. thanks anyway

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Guessing class

Posted 17 February 2012 - 10:56 PM

so change teacher

### #10 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Guessing class

Posted 17 February 2012 - 11:08 PM

Support ^

I think you are getting stuck at the if(guessingGame.guess(lastGuess) == -1) block. Lets say you have your guess at 10, with the randomNumber at 27. You are halving it each time so it will never go above or higher. I think you want to do something like randomNumber + guess where guess could be a -ve, then check if its higher or lower. Or check if guess is >, <, or == to 0.

I've managed to get my own implementation working of it, you are close, but for thing you havent added a way for negative numbers

You can write down on paper what exactly needs to happen

```- is below number

50  // start at 50
-25 // 50 is above number so you need to divide by 2 to half it, then make it negative
37  // -25 is negative, so you have to add them together by using absolute values then divide by 2 to make positive again
-18 // 37 is positive so divide by 2 and make negative again
27 // -18 is negative so you add them together with absolute values and divide by 2 again

```

Edit: I also must ask, are you sure this is what your meant to do? At the moment, this is comparing the difference of the guess compared to the number, and checking if that is equal to the number.

I would have thought that you want to keep dividing numbers until you reach the target

This post has been edited by Mylo: 17 February 2012 - 11:57 PM

### #11 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 18 February 2012 - 05:04 PM

I was doing it wrong. It is supposed to print something like this:
numbertoGuess: 40
- so it will start with 50 since 50 is too high it will find the middle number of 50 and 0.
- the next number is 25 which is too low so it will find the middle number of 50 and 25.
- next number is 37.5(will be count as 37) which is still too low so it will find the middle number of 50 and 37.
- next number will be 43.5(will be count as 43) which is too high so it will find the middle number of 43 and 37.
- that is 40 the correct answer.

so the number of guesses it took to get the correct number is 5

also I have no idea what mine is doing so if anyone has any idea of how to do it please help me a little. thanks

This post has been edited by jot121: 18 February 2012 - 05:05 PM

### #12 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 18 February 2012 - 05:12 PM

here is the code that I have now:
```public class HalvsiesGuesser extends Guesser
{
int lastGuess = 50;
int count = 0;

public boolean guess(GuessMyNumber guessingGame)
{
int nextGuess = 0;
int highestGuess= 99;
boolean guess = false;

for(int i = 0; i < 50; i++)
{
if(guessingGame.guess(lastGuess) == -1)
{
guess = false;
//nextGuess = (lastGuess + highestGuess)/2;
if(lastGuess == 50)
{
nextGuess = (lastGuess + highestGuess)/2;
}
else
{
nextGuess = (lastGuess + 50)/2;
}

if(guessingGame.guess(nextGuess) != -1)
{
highestGuess = nextGuess;
}
lastGuess = nextGuess;
}
else if(guessingGame.guess(lastGuess) == 0)
{
guess = true;
break;
}
else if(guessingGame.guess(lastGuess) == 1)
{
guess = false;
nextGuess = lastGuess/2;
lastGuess = nextGuess;
}
//System.out.println(lastGuess);
count++;

System.out.println(lastGuess);

}
return guess;
}

public int getNumberOfGuesses()
{
return count;
}

public void reset()
{
GuessMyNumber.currentGuess = lastGuess;
}
}

```

the other class is still the same.

output:
NumberToGuess: 57
74
37
43
46
48
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
49
Number of Guess it took to get it correct using HALVSIES GUESSER is 50

### #13 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 18 February 2012 - 05:43 PM

here is what I have now. It is pretty close to the correct method but I am not sure what I am doing wrong.
```public class HalvsiesGuesser extends Guesser
{
int lastGuess = 50;
int count = 0;

public boolean guess(GuessMyNumber guessingGame)
{
int nextGuess = 0;
int highestGuess= 99;
boolean guess = false;

for(int i = 0; i < 50; i++)
{
if(guessingGame.guess(lastGuess) == -1)
{
guess = false;
if(lastGuess < 50)
{
nextGuess = (lastGuess + 50)/2;
}
else
{
nextGuess = (lastGuess + highestGuess)/2;
}

if(guessingGame.guess(nextGuess) != -1)
{
highestGuess = nextGuess;
}
lastGuess = nextGuess;
}
else if(guessingGame.guess(lastGuess) == 0)
{
guess = true;
break;
}
else if(guessingGame.guess(lastGuess) == 1)
{
guess = false;
if(lastGuess > 50)
{
nextGuess = (lastGuess + 50)/2;
}
else
{
nextGuess = lastGuess/2;
}
lastGuess = nextGuess;
}
count++;
System.out.println(lastGuess);

}
return guess;
}

public int getNumberOfGuesses()
{
return count;
}

public void reset()
{
GuessMyNumber.currentGuess = lastGuess;
}
}

```

### #14 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

## Re: Guessing class

Posted 18 February 2012 - 05:57 PM

Well, there's probably many problems there, but one thing to look at is how you assign lastguess.

nextguess = 4;
lastguess = nextguess; // lastguess will now equal 4 also, which is not what you want, you wanted what was before it turned 4.

int temp = 4;
int nextguess = lastguess + nextguess / 2
int lastguess = temp

You need to rethink how to approach this problem, it has become unreadable for me.

```if (guesstohigh)
// change guess
else if (guesstolow)
// change guess
else
// exit loop, you have found the number

```

No need for embedded ifs, and as pbl said, your loop wont be returning false since it will just continue, so no need to make guess = false

Also, numbers other than 50, 37, -25, -18, 27 (unless you change initial nextguess) will not work, since you are halfing each time, some of the inbetween numbers will be skipped over and your loop will not end.

### #15 jot121

Reputation: -8
• Posts: 144
• Joined: 30-October 11

## Re: Guessing class

Posted 18 February 2012 - 06:10 PM

I don't understand what you mean by that. Can you please explain a little

This post has been edited by jot121: 18 February 2012 - 06:10 PM