Issues with Captain's Log exercise

Hi ! i’m having issues solving the
Captain’s Log exercise that introduces the Random class and methods, i did the exercise pretty easily but somehow no matter what i do i keep failing the same test, the testing wants my random code to generate specific values which makes no sense to me, i’m not sure if i’m the one wrong or if there’s a problem with the testing, thank you in advance for your help/advice. Here’s my code:
import java.util.Random;

class CaptainsLog {

private static final char[] PLANET_CLASSES = new char[]{'D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'};

private Random random;
CaptainsLog(Random random) {
    this.random = random;
}

char randomPlanetClass() {
    int i = random.nextInt(PLANET_CLASSES.length);
    return PLANET_CLASSES[i];
}

String randomShipRegistryNumber() {
    int i = 1000 + random.nextInt(9000);
    return String.format("NCC-%d", i);
}

double randomStardate() {
    double i = 41000 + 1000 * random.nextDouble();
    return i;
}

Here’s the test i’m failing:

@Test
@Tag("task:2")
@DisplayName("Generating a random ship registry number")
public void testRandomShipRegistryNumber() {
    assertThat(new CaptainsLog(random1).randomShipRegistryNumber()).isEqualTo("NCC-8773");
    assertThat(new CaptainsLog(random2).randomShipRegistryNumber()).isEqualTo("NCC-2473");
    assertThat(new CaptainsLog(random3).randomShipRegistryNumber()).isEqualTo("NCC-9576");
}

Confirmed.

In the web editor, the error message is

Expecting:
 <"NCC-6258">
to be equal to:
 <"NCC-8773">
but was not.
Exception: org.opentest4j.AssertionFailedError: 
Expecting:
 <"NCC-6258">
to be equal to:
 <"NCC-8773">
but was not.
	at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
	at CaptainsLogTest.testRandomShipRegistryNumber(CaptainsLogTest.java:48)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

Looks like random1 may not be seeded to the expected value.

1 Like

Hi @santidevtrack, welcome to the forum! :wave:

I double-checked the example implementation (which is what I used to generate the expected values using the seeds provided by the test suite) and it looks like it contains a bug: it calls random.nextInt(8999) instead of random.nextInt(9000)… How embarrassing! :see_no_evil:

Would you be willing to submit a PR to fix this? It would involve changing the example implementation to fix the bug and then updating the test case to expect the correct values (which are NCC-6258, NCC-1683 and NCC-4922 respectively).

2 Likes

The test file creates a new Random with a specific seed, so the first random value is always the same.

groovy:000> 1000 + new Random(47).nextInt()
===> -1172027779
groovy:000> 1000 + new Random(47).nextInt()
===> -1172027779
groovy:000> 1000 + new Random(47).nextInt()
===> -1172027779

We’re seeing this:

groovy:000> 1000 + new Random(47).nextInt(8999)
===> 8773
groovy:000> 1000 + new Random(47).nextInt(9000)
===> 6258

The first is what the tests expect (incorrectly), the second is what correct code generates.

1 Like

Exactly, thanks for pointing this out!

1 Like

Thank you guys for such a quick answer on my doubt, i’m fairly new to exercism(and to programming in general) so i don’t know how to submit a PR to fix the bug. I’m willing to help if you teach me how though

1 Like

@sanderploegsma Sander i think i did what you asked me to do :slight_smile: i hope i did good because it’s my first PR. I submitted two one on the implementation code and one on the testing file.

I see one PR: Update CaptainsLog.java by santialb · Pull Request #2588 · exercism/java · GitHub

Great work @santidevtrack, and thanks for providing us with your feedback! It is very much appreciated.

Could you do me one favor and mark this form topic as solved? You can pick any post in here as the solution, up to you :slight_smile:

1 Like