From b91c83d85143958a8bba79c59936cd1740a42eae Mon Sep 17 00:00:00 2001 From: Elena Neuschild Date: Tue, 14 Mar 2023 13:09:56 +0100 Subject: [PATCH 1/3] make instructions for Remote Control Competition more helpful --- .../.docs/instructions.md | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/exercises/concept/remote-control-competition/.docs/instructions.md b/exercises/concept/remote-control-competition/.docs/instructions.md index fe6500702..062b16d33 100644 --- a/exercises/concept/remote-control-competition/.docs/instructions.md +++ b/exercises/concept/remote-control-competition/.docs/instructions.md @@ -6,19 +6,19 @@ An experimental car has been developed and the test track needs to be adapted to In addition, production cars are beginning to have some success. The team boss is keen to maintain the competitive spirit by publishing a ranking of the production cars. -## 1. Enable cars to be driven on the same test track +## 1. Race! -Please add a method to the `RemoteControlCar` interface to encapsulate the behavior of `drive()` for the two types of car. +Please add two methods to the `RemoteControlCar` interface: +- `drive()`, returning nothing, and +- `getDistanceTravelled()`, returning an `int`. -```java -TestTrack.race(new ProductionRemoteControlCar()); -TestTrack.race(new ExperimentalRemoteControlCar()); -// this should execute without an exception being thrown -``` +Then make `ProductionRemoteControlCar` and `ExperimentalRemoteControlCar` implement the `RemoteControlCar` interface. This includes implementing all methods required by the interface. -## 2. Enable the distance travelled by different models on the test track to be compared +Each call of `.drive()` should make the car travel a certain distance: +- a `ProductionRemoteControlCar` drives 10 units, +- an `ExperimentalRemoteControlCar` drives 20 units. -Please add a method to the `RemoteControlCar` interface to encapsulate the behavior of the `getDistanceTravelled()` method for the two types of car. Notice that the Experimental model has a different speed than the Production model. +The `.getDistanceTravelled()` method should return the number of units that ```java ProductionRemoteControlCar prod = new ProductionRemoteControlCar(); @@ -31,7 +31,14 @@ exp.getDistanceTravelled(); // => 20 ``` -## 3. Allow the production cars to be ranked +Implement the `TestTrack.race(RemoteControlCar car)` method in which the `car`s get to `drive()`. +```java +TestTrack.race(new ProductionRemoteControlCar()); +TestTrack.race(new ExperimentalRemoteControlCar()); +// this should execute without an exception being thrown +``` + +## 2. Allow the production cars to be sorted Please implement the `Comparable` interface in the `ProductionRemoteControlCar` class. The default sort order for cars should be ascending order of victories. @@ -42,6 +49,7 @@ ProductionRemoteControlCar prc1 = new ProductionRemoteControlCar(); ProductionRemoteControlCar prc2 = new ProductionRemoteControlCar(); prc1.setNumberOfVictories(3); prc2.setNumberOfVictories(2); -int rankings = TestTrack.getRankedCars(prc1, prc2); -// => rankings[1] == prc1 +List rankings = TestTrack.getRankedCars(prc1, prc2); +// => rankings.get(0) == prc2 +// => rankings.get(1) == prc1 ``` From 6c3e85874a2107990ad1db4b90ff2249d45603ba Mon Sep 17 00:00:00 2001 From: Elena Neuschild Date: Wed, 5 Apr 2023 12:20:48 +0200 Subject: [PATCH 2/3] add tests to RemoteControlCompetition - adapt instructions to match new tests - change ranking semantics to more intuitive more-wins-ranks-higher order --- .../.docs/instructions.md | 24 ++++-- .../src/test/java/RemoteControlCarTest.java | 75 +++++++++++++++++-- 2 files changed, 83 insertions(+), 16 deletions(-) diff --git a/exercises/concept/remote-control-competition/.docs/instructions.md b/exercises/concept/remote-control-competition/.docs/instructions.md index 062b16d33..00a74d8eb 100644 --- a/exercises/concept/remote-control-competition/.docs/instructions.md +++ b/exercises/concept/remote-control-competition/.docs/instructions.md @@ -6,7 +6,7 @@ An experimental car has been developed and the test track needs to be adapted to In addition, production cars are beginning to have some success. The team boss is keen to maintain the competitive spirit by publishing a ranking of the production cars. -## 1. Race! +## 1. Implement the Interface Please add two methods to the `RemoteControlCar` interface: - `drive()`, returning nothing, and @@ -14,23 +14,28 @@ Please add two methods to the `RemoteControlCar` interface: Then make `ProductionRemoteControlCar` and `ExperimentalRemoteControlCar` implement the `RemoteControlCar` interface. This includes implementing all methods required by the interface. +## 2. Drive. + Each call of `.drive()` should make the car travel a certain distance: - a `ProductionRemoteControlCar` drives 10 units, - an `ExperimentalRemoteControlCar` drives 20 units. -The `.getDistanceTravelled()` method should return the number of units that +The `.getDistanceTravelled()` method should return the number of units that the car has travelled in its lifetime. ```java ProductionRemoteControlCar prod = new ProductionRemoteControlCar(); -TestTrack.race(prod); -ExperimentalRemoteControlCar exp = new ExperimentalRemoteControlCar(); -TestTrack.race(exp); +prod.drive(); prod.getDistanceTravelled(); // => 10 + +ExperimentalRemoteControlCar exp = new ExperimentalRemoteControlCar(); +exp.drive(); exp.getDistanceTravelled(); // => 20 ``` +## 3. Race! + Implement the `TestTrack.race(RemoteControlCar car)` method in which the `car`s get to `drive()`. ```java TestTrack.race(new ProductionRemoteControlCar()); @@ -38,17 +43,20 @@ TestTrack.race(new ExperimentalRemoteControlCar()); // this should execute without an exception being thrown ``` -## 2. Allow the production cars to be sorted +## 4. Allow the production cars to be sorted -Please implement the `Comparable` interface in the `ProductionRemoteControlCar` class. The default sort order for cars should be ascending order of victories. +Please implement the `Comparable` interface in the `ProductionRemoteControlCar` class. The default sort order for cars should be descending order of victories. -Implement the static `TestTrack.getRankedCars()` to return the cars passed in, sorted in ascending order of number of victories. +Implement the static `TestTrack.getRankedCars()` to return the cars passed in, sorted in descending order of number of victories. ```java ProductionRemoteControlCar prc1 = new ProductionRemoteControlCar(); ProductionRemoteControlCar prc2 = new ProductionRemoteControlCar(); prc1.setNumberOfVictories(3); prc2.setNumberOfVictories(2); +List unsortedCars = new ArrayList<>(); +unsortedCars.add(prc1); +unsortedCars.add(prc2); List rankings = TestTrack.getRankedCars(prc1, prc2); // => rankings.get(0) == prc2 // => rankings.get(1) == prc1 diff --git a/exercises/concept/remote-control-competition/src/test/java/RemoteControlCarTest.java b/exercises/concept/remote-control-competition/src/test/java/RemoteControlCarTest.java index f80fe3c7b..19d341b8b 100644 --- a/exercises/concept/remote-control-competition/src/test/java/RemoteControlCarTest.java +++ b/exercises/concept/remote-control-competition/src/test/java/RemoteControlCarTest.java @@ -1,10 +1,39 @@ import org.junit.Test; +import java.util.ArrayList; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; public class RemoteControlCarTest { + @Test + public void productionRccIsRemoteControlCar() { + ProductionRemoteControlCar productionCar = new ProductionRemoteControlCar(); + assertThat(productionCar instanceof RemoteControlCar).isTrue(); + } + + @Test + public void experimentalRccIsRemoteControlCar() { + ExperimentalRemoteControlCar experimentalCar = new ExperimentalRemoteControlCar(); + assertThat(experimentalCar instanceof RemoteControlCar).isTrue(); + } + + @Test + public void productionCarTravels10UnitsPerDrive() { + ProductionRemoteControlCar car = new ProductionRemoteControlCar(); + assertThat(car.getDistanceTravelled()).isEqualTo(0); + car.drive(); + assertThat(car.getDistanceTravelled()).isEqualTo(10); + } + + @Test + public void experimentalCarTravels20UnitsPerDrive() { + ExperimentalRemoteControlCar car = new ExperimentalRemoteControlCar(); + assertThat(car.getDistanceTravelled()).isEqualTo(0); + car.drive(); + assertThat(car.getDistanceTravelled()).isEqualTo(20); + } + @Test public void race() { ProductionRemoteControlCar productionCar = new ProductionRemoteControlCar(); @@ -17,17 +46,47 @@ public void race() { } @Test - public void rankCars() { + public void ensureCarsAreComparable() { + assertThat(Comparable.class).isAssignableFrom(ProductionRemoteControlCar.class); + } + + private static ProductionRemoteControlCar getCarWithVictories(int numberOfVictories) { ProductionRemoteControlCar prc1 = new ProductionRemoteControlCar(); - ProductionRemoteControlCar prc2 = new ProductionRemoteControlCar(); - prc1.setNumberOfVictories(3); - prc2.setNumberOfVictories(2); - List rankings = TestTrack.getRankedCars(prc1, prc2); - assertThat(rankings.get(1)).isEqualTo(prc1); + prc1.setNumberOfVictories(numberOfVictories); + return prc1; } @Test - public void ensureCarsAreComparable() { - assertThat(Comparable.class).isAssignableFrom(ProductionRemoteControlCar.class); + public void rankTwoCars() { + List unsortedCars = new ArrayList<>() { + { + add(getCarWithVictories(2)); + add(getCarWithVictories(3)); + } + }; + List rankings = TestTrack.getRankedCars(unsortedCars); + assertThat(rankings.get(0).getNumberOfVictories()).isEqualTo(3); + assertThat(rankings.get(1).getNumberOfVictories()).isEqualTo(2); + } + + @Test + public void rankManyCars() { + List unsortedCars = new ArrayList<>() { + { + add(getCarWithVictories(0)); + add(getCarWithVictories(3)); + add(getCarWithVictories(5)); + add(getCarWithVictories(7)); + add(getCarWithVictories(2)); + add(getCarWithVictories(1)); + } + }; + List rankings = TestTrack.getRankedCars(unsortedCars); + assertThat(rankings.get(0).getNumberOfVictories()).isEqualTo(7); + assertThat(rankings.get(1).getNumberOfVictories()).isEqualTo(5); + assertThat(rankings.get(2).getNumberOfVictories()).isEqualTo(3); + assertThat(rankings.get(3).getNumberOfVictories()).isEqualTo(2); + assertThat(rankings.get(4).getNumberOfVictories()).isEqualTo(1); + assertThat(rankings.get(5).getNumberOfVictories()).isEqualTo(0); } } From dd5b295297250e72b8b04d11aab07c949ce15440 Mon Sep 17 00:00:00 2001 From: Derk-Jan Karrenbeld Date: Wed, 5 Apr 2023 16:52:28 +0200 Subject: [PATCH 3/3] Match Exercism Markdown format --- .../remote-control-competition/.docs/instructions.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/exercises/concept/remote-control-competition/.docs/instructions.md b/exercises/concept/remote-control-competition/.docs/instructions.md index 00a74d8eb..bfb67d976 100644 --- a/exercises/concept/remote-control-competition/.docs/instructions.md +++ b/exercises/concept/remote-control-competition/.docs/instructions.md @@ -12,7 +12,8 @@ Please add two methods to the `RemoteControlCar` interface: - `drive()`, returning nothing, and - `getDistanceTravelled()`, returning an `int`. -Then make `ProductionRemoteControlCar` and `ExperimentalRemoteControlCar` implement the `RemoteControlCar` interface. This includes implementing all methods required by the interface. +Then make `ProductionRemoteControlCar` and `ExperimentalRemoteControlCar` implement the `RemoteControlCar` interface. +This includes implementing all methods required by the interface. ## 2. Drive. @@ -45,7 +46,8 @@ TestTrack.race(new ExperimentalRemoteControlCar()); ## 4. Allow the production cars to be sorted -Please implement the `Comparable` interface in the `ProductionRemoteControlCar` class. The default sort order for cars should be descending order of victories. +Please implement the `Comparable` interface in the `ProductionRemoteControlCar` class. +The default sort order for cars should be descending order of victories. Implement the static `TestTrack.getRankedCars()` to return the cars passed in, sorted in descending order of number of victories.