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.

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;
		int highestGuess= 99;
		boolean guess = false;
		
		for(int i = 0; i < Integer.MAX_VALUE; i++)
		{
			if(guessingGame.guess(lastGuess) == -1)
			{
				guess = false;
				nextGuess = (lastGuess + highestGuess)/2;
				if(guessingGame.guess(nextGuess) != -1)
				{
					highestGuess = nextGuess;
				}
				System.out.println(guessingGame.guess(nextGuess) != -1);
				
			}
			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++;
		}
		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?

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

Can you copy the console from when you execute the program that shows what the problem is.
Add comments to the post where there is a problem and show what you want the output to be.

Why do you use MAX_VALUE in this loop?
for(int i = 0; i < Integer.MAX_VALUE; i++)
Why not make it a while(true) that makes it clear what you are trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.