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");
}
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.
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!
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).
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
@sanderploegsma Sander i think i did what you asked me to do 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.