Skip to content

Commit 41ca0a6

Browse files
Add concept exercise: captains-log
1 parent 017ac09 commit 41ca0a6

File tree

12 files changed

+410
-0
lines changed

12 files changed

+410
-0
lines changed

config.json

+13
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,19 @@
234234
"switch-statement",
235235
"constructors"
236236
]
237+
},
238+
{
239+
"slug": "captains-log",
240+
"name": "Captain's Log",
241+
"uuid": "1ade8233-7a73-4fd9-afe2-f80d7cca14ab",
242+
"concepts": [
243+
"randomness"
244+
],
245+
"prerequisites": [
246+
"arrays",
247+
"numbers",
248+
"strings"
249+
]
237250
}
238251
],
239252
"practice": [
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Hints
2+
3+
## 1. Generate a random planet
4+
5+
- Java does not provide a function to choose an element from a collection at random.
6+
- Remember that you can retrieve an element from an array by its index, which is an integer.
7+
8+
## 2. Generate a random starship registry number
9+
10+
- The `java.util.Random` class provides a method to generate a random `int` between 0 and a given maximum.
11+
12+
## 3. Generate a random stardate
13+
14+
- The `java.util.Random` class method to generate a random `double` always returns a number between `0.0` and `1.0`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Instructions
2+
3+
Mary is a big fan of the TV series _Star Trek: The Next Generation_.
4+
She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the _Starship Enterprise_.
5+
Mary's character is Captain Picard, which means she has to keep the captain's log.
6+
She loves the creative part of the game, but doesn't like to generate random data on the spot.
7+
8+
Help Mary by creating random generators for data commonly appearing in the captain's log.
9+
10+
~~~~exercism/note
11+
The starter implementation of this exercise takes a `java.util.Random` instance as constructor argument.
12+
This allows the exercise's tests to pass an instance with a predefined seed, which makes the test results predictable.
13+
14+
Therefore, you are expected to use provided the `java.util.Random` instance in your implementation.
15+
~~~~
16+
17+
## 1. Generate a random planet
18+
19+
The _Starship Enterprise_ encounters many planets in its travels.
20+
Planets in the Star Trek universe are split into categories based on their properties.
21+
For example, Earth is a class `M` planet.
22+
All possible planetary classes are: `D`, `H`, `J`, `K`, `L`, `M`, `N`, `R`, `T`, and `Y`.
23+
24+
Implement the `randomPlanetClass()` method.
25+
It should return one of the planetary classes at random.
26+
27+
```java
28+
captainsLog.randomPlanetClass();
29+
// => "K"
30+
```
31+
32+
## 2. Generate a random starship registry number
33+
34+
Enterprise (registry number `NCC-1701`) is not the only starship flying around!
35+
When it rendezvous with another starship, Mary needs to log the registry number of that starship.
36+
37+
Registry numbers start with the prefix "NCC-" and then use a number from `1000` to `9999` (inclusive).
38+
39+
Implement the `randomShipRegistryNumber()` method that returns a random starship registry number.
40+
41+
```java
42+
captainsLog.randomShipRegistryNumber();
43+
// => "NCC-1947"
44+
```
45+
46+
## 3. Generate a random stardate
47+
48+
What's the use of a log if it doesn't include dates?
49+
50+
A stardate is a floating point number.
51+
The adventures of the _Starship Enterprise_ from the first season of _The Next Generation_ take place between the stardates `41000.0` and `42000.0`.
52+
The "4" stands for the 24th century, the "1" for the first season.
53+
54+
Implement the `randomStardate()` method that returns a floating point number between `41000.0` (inclusive) and `42000.0` (exclusive).
55+
56+
```java
57+
captainsLog.randomStardate();
58+
// => 41458.15721310934
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Introduction
2+
3+
## Randomness
4+
5+
An instance of the `java.util.Random` class can be used to generate random numbers in Java.
6+
7+
### Random integers
8+
9+
A random integer can be generated using the `nextInt()` method.
10+
This will generate a value in the range from `Integer.MIN_VALUE` to `Integer.MAX_VALUE`.
11+
12+
```java
13+
Random random = new Random();
14+
15+
random.nextInt();
16+
// => -1169335537
17+
```
18+
19+
To limit the range of generated values, use `nextInt(int)`.
20+
This will generate a value in the range from `0` (inclusive) to the given upper bound (exclusive).
21+
22+
For example, this will generate a random number from `0` through `9`.
23+
24+
```java
25+
Random random = new Random();
26+
27+
random.nextInt(10);
28+
// => 6
29+
```
30+
31+
And this will generate a random number from `10` through `19`.
32+
33+
```java
34+
Random random = new Random();
35+
36+
10 + random.nextInt(10);
37+
// => 11
38+
```
39+
40+
### Random doubles
41+
42+
A random double can be generated using the `nextDouble()` method.
43+
This will generate a value in the range from `0.0` to `1.0`.
44+
45+
```java
46+
Random random = new Random();
47+
48+
random.nextDouble();
49+
// => 0.19250004204021398
50+
```
51+
52+
And this will generate a random number from `100.0` to `200.0`.
53+
54+
```java
55+
Random random = new Random();
56+
57+
100.0 + 100.0 * random.nextDouble();
58+
// => 111.31849856260328
59+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Introduction
2+
3+
## Randomness
4+
5+
%{concept:randomness}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"authors": [
3+
"sanderploegsma"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/main/java/CaptainsLog.java"
8+
],
9+
"test": [
10+
"src/test/java/CaptainsLogTest.java"
11+
],
12+
"exemplar": [
13+
".meta/src/reference/java/CaptainsLog.java"
14+
],
15+
"invalidator": [
16+
"build.gradle"
17+
]
18+
},
19+
"forked_from": [
20+
"elixir/captains-log"
21+
],
22+
"blurb": "Learn about randomness by helping Mary generate stardates and starship registry numbers for her Star Trek themed pen-and-paper role playing sessions."
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Design
2+
3+
## Learning objectives
4+
5+
- Know how to generate a random `Integer`.
6+
- Know how to generate a random `Double`.
7+
- Know how to select a random element from a collection.
8+
9+
## Out of scope
10+
11+
- `java.util.SecureRandom`.
12+
13+
## Concepts
14+
15+
- `randomness`
16+
17+
## Prerequisites
18+
19+
- `strings`: know how to use string formatting.
20+
- `numbers`: know how to apply basic mathematical operators.
21+
- `arrays`: know how to retrieve array elements by their index.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Random;
2+
3+
class CaptainsLog {
4+
5+
private static final char[] PLANET_CLASSES = new char[]{'D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'};
6+
7+
private final Random random;
8+
9+
CaptainsLog(Random random) {
10+
this.random = random;
11+
}
12+
13+
char randomPlanetClass() {
14+
var index = random.nextInt(PLANET_CLASSES.length);
15+
return PLANET_CLASSES[index];
16+
}
17+
18+
String randomShipRegistryNumber() {
19+
var start = 1000;
20+
var end = 9999;
21+
return String.format("NCC-%d", start + random.nextInt(end - start));
22+
}
23+
24+
double randomStardate() {
25+
var start = 41000.0;
26+
var end = 42000.0;
27+
return start + random.nextDouble() * (end - start);
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.15.0"
13+
}
14+
15+
test {
16+
useJUnitPlatform()
17+
18+
testLogging {
19+
exceptionFormat = "full"
20+
showStandardStreams = true
21+
events = ["passed", "failed", "skipped"]
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Random;
2+
3+
class CaptainsLog {
4+
5+
private static final char[] PLANET_CLASSES = new char[]{'D', 'H', 'J', 'K', 'L', 'M', 'N', 'R', 'T', 'Y'};
6+
7+
private Random random;
8+
9+
CaptainsLog(Random random) {
10+
this.random = random;
11+
}
12+
13+
char randomPlanetClass() {
14+
throw new UnsupportedOperationException("Please implement the CaptainsLog.randomPlanetClass() method");
15+
}
16+
17+
String randomShipRegistryNumber() {
18+
throw new UnsupportedOperationException("Please implement the CaptainsLog.randomShipRegistryNumber() method");
19+
}
20+
21+
double randomStardate() {
22+
throw new UnsupportedOperationException("Please implement the CaptainsLog.randomStardate() method");
23+
}
24+
}

0 commit comments

Comments
 (0)