Your task is to implement a number guesser that works on the principle of a binary search. In each step, the guesser cuts the query interval in half. When the interval contains a single number, it proclaims the answer.

Suppose your guesser is asked to guess a number between 1 and 100. It starts with the query

>50

50 is the midpoint between 1 and 100.

The person who knows the number says false. The next query is

>25

25 is the midpoint between 1 and 50.

The answer is still false, and the next query is

>13

The answer is still false, and the next query is

>7

Now the answer is true, and the next query is

>10

The answer is again true, and the next query is

>12

The answer is once again true, and now the guesser proclaims

=13

An object of your class will be called like this:

while (...)
{
   String guess = guesser.nextGuess();
   if (guess.startsWith("="))
   {
      // display answer and terminate loop
      ...
   }
   else
   { 
      // show guess to person who knows the answer
      ...
      // get response from person
      boolean resp = ...; 
      guesser.response(resp);
   }
}

Complete the following file:

NumberGuesser.java

import java.util.ArrayList; public class NumberGuesser { public NumberGuesser(int lowest, int highest) { low = lowest; high = highest; } /** Returns the next guess. @return a string of the form >n or =n */ public String nextGuess() { // TODO: Complete method } /** Sets the response by the person who knows the number @param guessCorrect true if the preceding guess was correct */ public void response(boolean guessCorrect) { // TODO: Complete method } private int low; private int mid; private int high; // this method is used to check your work public static String[] check(int n) { NumberGuesser guesser = new NumberGuesser(1, 100); ArrayList<String> guesses = new ArrayList<String>(); while (true) { String guess = guesser.nextGuess(); guesses.add(guess); System.out.println(guess); if (guess.startsWith("=")) return guesses.toArray(new String[]{}); int g = Integer.parseInt(guess.substring(1)); guesser.response(n > g); } } }