-1

I am trying to make a program which will take a random guess from a range of numbers and checks whether it matches with the given number.

It will run 10times, it the program guesses the correct answer then it will terminate but if not then it will terminate with rejection.

at GuessingTest.main(GuessingTest.java:33)
**current directly** ; /usr/bin/env /Li
brary/Java/JavaVirtualMachines/temurin-17.jdk/Contents/Home/bin/java @/var/folders/zl/r96mn6p116125gw39hst2gp40000gn/T/cp_2tq0yp3psptn6qjx3o6su
w328.argfile GuessingTest 
Exception in thread "main" java.lang.AssertionError
        at org.junit.Assert.fail(Assert.java:87)
        at org.junit.Assert.assertTrue(Assert.java:42)
        at org.junit.Assert.assertTrue(Assert.java:53)
        at GuessingTest.guess(GuessingTest.java:30)
        at GuessingTest.main(GuessingTest.java:35)

Below is my code Guessing.java

public class Guessing {

    // Your local variables here
    private int low = 0;
    private int high = 1000;
    int randomNum;

    /**
     * Implement how your algorithm should make a guess here
     */
    public int guess() {
        randomNum = (int) ((Math.random() * (low - high)) + low);
        return randomNum;
    }

    /**
     * Implement how your algorithm should update its guess here
     */
    public void update(int answer) {
        if (answer == 1) {
            low = randomNum;
        } else if (answer == -1) {
            high = randomNum;
        }
    }
}

GuessingTest.java


import org.junit.Test;

import java.util.Random;

import static org.junit.Assert.*;

public class GuessingTest {


    @Test
    public void guess() {
        Random r = new Random();
        int hiddenNumber = r.nextInt(1001);

        Guessing g = new Guessing();

        int remainingGuesses = 10;
        while (remainingGuesses >= 0) {
            int guess = g.guess();
            if (guess == hiddenNumber) {
                break;
            } else if(guess > hiddenNumber) {
                g.update(1);
                remainingGuesses--;
            } else {
                g.update(-1);
                remainingGuesses--;
            }
        }
        assertTrue(remainingGuesses >= 0);
    }

    public static void main(String[] args) {
        GuessingTest test = new GuessingTest();
        test.guess();
    }
}

  • 1
    Well, that's apparently a failing JUnit test. But your question lacks important information on what are you trying to achieve and how. – Newerth Jan 23 '23 at 06:28
  • 2
    Your loop runs until remainingGuesses is >= 0. So assertTrue(remainingGuesses >=0) is obviously false afterwards if you don't hit the break point. – berse2212 Jan 23 '23 at 06:43

1 Answers1

1

The exception you're getting is a result of failing the guess() test.

assertTrue(remainingGuesses >= 0);

In other words, you're expecting the hiddenNumber to be found within 11 steps (0..10), but it's not. And there's no reason for it to be. Your code returns negative numbers.

Also it wouldn't work even if it was returning positive numbers, since you could easily guess 0, 1, 2 ... up to 10, ending with 10 as low and 1000 as high.

Also -- how to get random numbers within a specific range: https://stackoverflow.com/a/5887745/684296

Newerth
  • 449
  • 2
  • 12
  • meaning my implementation is correct but it's due to assertTrue that I am getting the error? Sorry I have just started this bit, I am trying to understand. thanks for your help. – Suryansh Kushwaha Jan 23 '23 at 07:41
  • 1
    Does your implementation ever guess the correct number? I suggest that you print out each guess. – tgdavies Jan 23 '23 at 07:53
  • 1
    @SuryanshKushwaha Your implementation is probably not quite right. 1) Change `(low - high)` to `(high - low)` to get positive numbers within the `low..high` interval and 2) the assumption that this approach will *always* lead to a correctly guessed number is wrong as long as `high - low` is higher than the number of attempts. Also be aware that 3) `Math.random()` never returns `1`, so your `guess()` method will never return the value of `high`. – Newerth Jan 23 '23 at 08:00
  • 1
    @SuryanshKushwaha And yes, you're getting that error because of `assertTrue`. :-) – Newerth Jan 23 '23 at 08:05
  • @Newerth Thank you so much for your help. How would you suggest me to take care of Math.random(). Do you know any other way around for guessing a number in a range of given number (both inclusive)? – Suryansh Kushwaha Jan 25 '23 at 05:28
  • @SuryanshKushwaha You almost had it. :-) See the link in the updated answer. – Newerth Jan 25 '23 at 05:51