From 532cfcdca8b43b1d1cfce0b3aba4e94df2908e9d Mon Sep 17 00:00:00 2001 From: manumafe98 Date: Fri, 12 Jan 2024 21:04:19 -0300 Subject: [PATCH 1/3] Sync tests for practice exercise space age, and reformat the exercise due to changes --- .../.meta/src/reference/java/SpaceAge.java | 82 ++++++------------- exercises/practice/space-age/.meta/tests.toml | 16 +++- .../space-age/src/main/java/SpaceAge.java | 36 +------- .../space-age/src/test/java/SpaceAgeTest.java | 42 ++++++---- 4 files changed, 64 insertions(+), 112 deletions(-) diff --git a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java index 6143ed0b5..f225f0db4 100644 --- a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java +++ b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java @@ -1,73 +1,37 @@ -import java.math.BigDecimal; +import java.util.Map; +import java.util.HashMap; class SpaceAge { - private enum Planet { - EARTH(1.0), - MERCURY(0.2408467), - VENUS(0.61519726), - MARS(1.8808158), - JUPITER(11.862615), - SATURN(29.447498), - URANUS(84.016846), - NEPTUNE(164.79132); - - private final double relativeOrbitalPeriod; - - Planet(double relativeOrbitalPeriod) { - this.relativeOrbitalPeriod = relativeOrbitalPeriod; - } - - private double getRelativeOrbitalPeriod() { - return relativeOrbitalPeriod; - } - } + private Map planetsOrbitalPeriod = new HashMap<>() {{ + put("Mercury", 0.2408467); + put("Venus", 0.61519726); + put("Mars", 1.8808158); + put("Jupiter", 11.862615); + put("Saturn", 29.447498); + put("Uranus", 84.016846); + put("Neptune", 164.79132); + put("Earth", 1.0); + }}; private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0; - private static final int PRECISION = 2; - private double seconds; + private String planet; - SpaceAge(double seconds) { + SpaceAge(double seconds, String planet) { this.seconds = seconds; - } - - double onEarth() { - return calculateAge(Planet.EARTH); - } - - double onMercury() { - return calculateAge(Planet.MERCURY); - } + this.planet = planet; - double onVenus() { - return calculateAge(Planet.VENUS); - } - - double onMars() { - return calculateAge(Planet.MARS); - } - - double onJupiter() { - return calculateAge(Planet.JUPITER); - } - - double onSaturn() { - return calculateAge(Planet.SATURN); - } - - double onUranus() { - return calculateAge(Planet.URANUS); - } - - double onNeptune() { - return calculateAge(Planet.NEPTUNE); + if (planetsOrbitalPeriod.get(planet) == null) { + throw new IllegalArgumentException("not a planet"); + } } - private double calculateAge(Planet planet) { - double age = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.getRelativeOrbitalPeriod()); - - return new BigDecimal(age).setScale(PRECISION, BigDecimal.ROUND_HALF_UP).doubleValue(); + double calculateAge() { + double earthyears = seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS; + double years = earthyears / planetsOrbitalPeriod.get(planet); + + return (double) Math.round(years * 100) / 100; } } diff --git a/exercises/practice/space-age/.meta/tests.toml b/exercises/practice/space-age/.meta/tests.toml index b4a221dc9..7957bb779 100644 --- a/exercises/practice/space-age/.meta/tests.toml +++ b/exercises/practice/space-age/.meta/tests.toml @@ -1,6 +1,13 @@ -# This is an auto-generated file. Regular comments will be removed when this -# file is regenerated. Regenerating will not touch any manually added keys, -# so comments can be added in a "comment" key. +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. [84f609af-5a91-4d68-90a3-9e32d8a5cd34] description = "age on Earth" @@ -25,3 +32,6 @@ description = "age on Uranus" [80096d30-a0d4-4449-903e-a381178355d8] description = "age on Neptune" + +[57b96e2a-1178-40b7-b34d-f3c9c34e4bf4] +description = "invalid planet causes error" diff --git a/exercises/practice/space-age/src/main/java/SpaceAge.java b/exercises/practice/space-age/src/main/java/SpaceAge.java index e1a9de7f7..66f2caa50 100644 --- a/exercises/practice/space-age/src/main/java/SpaceAge.java +++ b/exercises/practice/space-age/src/main/java/SpaceAge.java @@ -1,42 +1,10 @@ class SpaceAge { - SpaceAge(double seconds) { + SpaceAge(double seconds, String planet) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } - double getSeconds() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onEarth() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onMercury() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onVenus() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onMars() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onJupiter() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onSaturn() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onUranus() { - throw new UnsupportedOperationException("Delete this statement and write your own implementation."); - } - - double onNeptune() { + double calculateAge() { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } diff --git a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java index 72a551dc4..ef244d1f2 100644 --- a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java +++ b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java @@ -2,6 +2,7 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.within; public class SpaceAgeTest { @@ -10,64 +11,73 @@ public class SpaceAgeTest { @Test public void ageOnEarth() { - SpaceAge age = new SpaceAge(1000000000); + SpaceAge age = new SpaceAge(1000000000, "Earth"); - assertThat(age.onEarth()).isEqualTo(31.69, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(31.69, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnMercury() { - SpaceAge age = new SpaceAge(2134835688); + SpaceAge age = new SpaceAge(2134835688, "Mercury"); - assertThat(age.onMercury()).isEqualTo(280.88, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(280.88, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnVenus() { - SpaceAge age = new SpaceAge(189839836); + SpaceAge age = new SpaceAge(189839836, "Venus"); - assertThat(age.onVenus()).isEqualTo(9.78, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(9.78, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnMars() { - SpaceAge age = new SpaceAge(2129871239L); + SpaceAge age = new SpaceAge(2129871239L, "Mars"); - assertThat(age.onMars()).isEqualTo(35.88, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(35.88, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnJupiter() { - SpaceAge age = new SpaceAge(901876382); + SpaceAge age = new SpaceAge(901876382, "Jupiter"); - assertThat(age.onJupiter()).isEqualTo(2.41, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(2.41, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnSaturn() { - SpaceAge age = new SpaceAge(2000000000L); + SpaceAge age = new SpaceAge(2000000000L, "Saturn"); - assertThat(age.onSaturn()).isEqualTo(2.15, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(2.15, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnUranus() { - SpaceAge age = new SpaceAge(1210123456L); + SpaceAge age = new SpaceAge(1210123456L, "Uranus"); - assertThat(age.onUranus()).isEqualTo(0.46, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(0.46, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnNeptune() { - SpaceAge age = new SpaceAge(1821023456L); + SpaceAge age = new SpaceAge(1821023456L, "Neptune"); - assertThat(age.onNeptune()).isEqualTo(0.35, within(MAXIMUM_DELTA)); + assertThat(age.calculateAge()).isEqualTo(0.35, within(MAXIMUM_DELTA)); + } + + @Ignore("Remove to run test") + @Test + public void invalidPlanetCausesError() { + + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> new SpaceAge(680804807L, "Sun")) + .withMessage("not a planet"); } } From 434b8d57948232e93d0fda4a08b4338fbbe3ee56 Mon Sep 17 00:00:00 2001 From: manumafe98 Date: Tue, 16 Jan 2024 17:51:42 -0300 Subject: [PATCH 2/3] Rollbacking changes that were applied after an invalid tests, setting that tests to include = false --- .../.meta/src/reference/java/SpaceAge.java | 78 +++++++++++++------ exercises/practice/space-age/.meta/tests.toml | 2 + .../space-age/src/main/java/SpaceAge.java | 36 ++++++++- .../space-age/src/test/java/SpaceAgeTest.java | 42 ++++------ 4 files changed, 107 insertions(+), 51 deletions(-) diff --git a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java index f225f0db4..0d80a4dcc 100644 --- a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java +++ b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java @@ -1,37 +1,69 @@ -import java.util.Map; -import java.util.HashMap; - class SpaceAge { - private Map planetsOrbitalPeriod = new HashMap<>() {{ - put("Mercury", 0.2408467); - put("Venus", 0.61519726); - put("Mars", 1.8808158); - put("Jupiter", 11.862615); - put("Saturn", 29.447498); - put("Uranus", 84.016846); - put("Neptune", 164.79132); - put("Earth", 1.0); - }}; + private enum Planet { + EARTH(1.0), + MERCURY(0.2408467), + VENUS(0.61519726), + MARS(1.8808158), + JUPITER(11.862615), + SATURN(29.447498), + URANUS(84.016846), + NEPTUNE(164.79132); + + private final double relativeOrbitalPeriod; + + Planet(double relativeOrbitalPeriod) { + this.relativeOrbitalPeriod = relativeOrbitalPeriod; + } + + private double getRelativeOrbitalPeriod() { + return relativeOrbitalPeriod; + } + } private static final double EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0; private double seconds; - private String planet; - SpaceAge(double seconds, String planet) { + SpaceAge(double seconds) { this.seconds = seconds; - this.planet = planet; + } - if (planetsOrbitalPeriod.get(planet) == null) { - throw new IllegalArgumentException("not a planet"); - } + double onEarth() { + return calculateAge(Planet.EARTH); + } + + double onMercury() { + return calculateAge(Planet.MERCURY); + } + + double onVenus() { + return calculateAge(Planet.VENUS); + } + + double onMars() { + return calculateAge(Planet.MARS); + } + + double onJupiter() { + return calculateAge(Planet.JUPITER); + } + + double onSaturn() { + return calculateAge(Planet.SATURN); + } + + double onUranus() { + return calculateAge(Planet.URANUS); + } + + double onNeptune() { + return calculateAge(Planet.NEPTUNE); } - double calculateAge() { - double earthyears = seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS; - double years = earthyears / planetsOrbitalPeriod.get(planet); + private double calculateAge(Planet planet) { + double planetYears = (seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS) / planet.getRelativeOrbitalPeriod(); - return (double) Math.round(years * 100) / 100; + return (double) Math.round(planetYears * 100) / 100; } } diff --git a/exercises/practice/space-age/.meta/tests.toml b/exercises/practice/space-age/.meta/tests.toml index 7957bb779..4b65f23af 100644 --- a/exercises/practice/space-age/.meta/tests.toml +++ b/exercises/practice/space-age/.meta/tests.toml @@ -35,3 +35,5 @@ description = "age on Neptune" [57b96e2a-1178-40b7-b34d-f3c9c34e4bf4] description = "invalid planet causes error" +include = false +comment = "Excluded because the design of the exercise does not allow for arbitrary planets" diff --git a/exercises/practice/space-age/src/main/java/SpaceAge.java b/exercises/practice/space-age/src/main/java/SpaceAge.java index 66f2caa50..e1a9de7f7 100644 --- a/exercises/practice/space-age/src/main/java/SpaceAge.java +++ b/exercises/practice/space-age/src/main/java/SpaceAge.java @@ -1,10 +1,42 @@ class SpaceAge { - SpaceAge(double seconds, String planet) { + SpaceAge(double seconds) { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } - double calculateAge() { + double getSeconds() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onEarth() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onMercury() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onVenus() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onMars() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onJupiter() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onSaturn() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onUranus() { + throw new UnsupportedOperationException("Delete this statement and write your own implementation."); + } + + double onNeptune() { throw new UnsupportedOperationException("Delete this statement and write your own implementation."); } diff --git a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java index ef244d1f2..72a551dc4 100644 --- a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java +++ b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java @@ -2,7 +2,6 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.within; public class SpaceAgeTest { @@ -11,73 +10,64 @@ public class SpaceAgeTest { @Test public void ageOnEarth() { - SpaceAge age = new SpaceAge(1000000000, "Earth"); + SpaceAge age = new SpaceAge(1000000000); - assertThat(age.calculateAge()).isEqualTo(31.69, within(MAXIMUM_DELTA)); + assertThat(age.onEarth()).isEqualTo(31.69, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnMercury() { - SpaceAge age = new SpaceAge(2134835688, "Mercury"); + SpaceAge age = new SpaceAge(2134835688); - assertThat(age.calculateAge()).isEqualTo(280.88, within(MAXIMUM_DELTA)); + assertThat(age.onMercury()).isEqualTo(280.88, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnVenus() { - SpaceAge age = new SpaceAge(189839836, "Venus"); + SpaceAge age = new SpaceAge(189839836); - assertThat(age.calculateAge()).isEqualTo(9.78, within(MAXIMUM_DELTA)); + assertThat(age.onVenus()).isEqualTo(9.78, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnMars() { - SpaceAge age = new SpaceAge(2129871239L, "Mars"); + SpaceAge age = new SpaceAge(2129871239L); - assertThat(age.calculateAge()).isEqualTo(35.88, within(MAXIMUM_DELTA)); + assertThat(age.onMars()).isEqualTo(35.88, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnJupiter() { - SpaceAge age = new SpaceAge(901876382, "Jupiter"); + SpaceAge age = new SpaceAge(901876382); - assertThat(age.calculateAge()).isEqualTo(2.41, within(MAXIMUM_DELTA)); + assertThat(age.onJupiter()).isEqualTo(2.41, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnSaturn() { - SpaceAge age = new SpaceAge(2000000000L, "Saturn"); + SpaceAge age = new SpaceAge(2000000000L); - assertThat(age.calculateAge()).isEqualTo(2.15, within(MAXIMUM_DELTA)); + assertThat(age.onSaturn()).isEqualTo(2.15, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnUranus() { - SpaceAge age = new SpaceAge(1210123456L, "Uranus"); + SpaceAge age = new SpaceAge(1210123456L); - assertThat(age.calculateAge()).isEqualTo(0.46, within(MAXIMUM_DELTA)); + assertThat(age.onUranus()).isEqualTo(0.46, within(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @Test public void ageOnNeptune() { - SpaceAge age = new SpaceAge(1821023456L, "Neptune"); + SpaceAge age = new SpaceAge(1821023456L); - assertThat(age.calculateAge()).isEqualTo(0.35, within(MAXIMUM_DELTA)); - } - - @Ignore("Remove to run test") - @Test - public void invalidPlanetCausesError() { - - assertThatExceptionOfType(IllegalArgumentException.class) - .isThrownBy(() -> new SpaceAge(680804807L, "Sun")) - .withMessage("not a planet"); + assertThat(age.onNeptune()).isEqualTo(0.35, within(MAXIMUM_DELTA)); } } From a97f14af9ab406df7083b283c2e1e14eb8fc7531 Mon Sep 17 00:00:00 2001 From: manumafe98 Date: Thu, 18 Jan 2024 16:24:27 -0300 Subject: [PATCH 3/3] Modifying the test exercises so the students do not have to round the value to make it work --- .../.meta/src/reference/java/SpaceAge.java | 4 +--- .../space-age/src/test/java/SpaceAgeTest.java | 18 +++++++++--------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java index 0d80a4dcc..67e1bbaf1 100644 --- a/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java +++ b/exercises/practice/space-age/.meta/src/reference/java/SpaceAge.java @@ -61,9 +61,7 @@ private double getRelativeOrbitalPeriod() { } private double calculateAge(Planet planet) { - double planetYears = (seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS) / planet.getRelativeOrbitalPeriod(); - - return (double) Math.round(planetYears * 100) / 100; + return (seconds / EARTH_ORBITAL_PERIOD_IN_SECONDS) / planet.getRelativeOrbitalPeriod(); } } diff --git a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java index 72a551dc4..fdf5172d8 100644 --- a/exercises/practice/space-age/src/test/java/SpaceAgeTest.java +++ b/exercises/practice/space-age/src/test/java/SpaceAgeTest.java @@ -2,7 +2,7 @@ import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.within; +import static org.assertj.core.api.Assertions.offset; public class SpaceAgeTest { @@ -12,7 +12,7 @@ public class SpaceAgeTest { public void ageOnEarth() { SpaceAge age = new SpaceAge(1000000000); - assertThat(age.onEarth()).isEqualTo(31.69, within(MAXIMUM_DELTA)); + assertThat(age.onEarth()).isEqualTo(31.69, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -20,7 +20,7 @@ public void ageOnEarth() { public void ageOnMercury() { SpaceAge age = new SpaceAge(2134835688); - assertThat(age.onMercury()).isEqualTo(280.88, within(MAXIMUM_DELTA)); + assertThat(age.onMercury()).isEqualTo(280.88, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -28,7 +28,7 @@ public void ageOnMercury() { public void ageOnVenus() { SpaceAge age = new SpaceAge(189839836); - assertThat(age.onVenus()).isEqualTo(9.78, within(MAXIMUM_DELTA)); + assertThat(age.onVenus()).isEqualTo(9.78, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -36,7 +36,7 @@ public void ageOnVenus() { public void ageOnMars() { SpaceAge age = new SpaceAge(2129871239L); - assertThat(age.onMars()).isEqualTo(35.88, within(MAXIMUM_DELTA)); + assertThat(age.onMars()).isEqualTo(35.88, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -44,7 +44,7 @@ public void ageOnMars() { public void ageOnJupiter() { SpaceAge age = new SpaceAge(901876382); - assertThat(age.onJupiter()).isEqualTo(2.41, within(MAXIMUM_DELTA)); + assertThat(age.onJupiter()).isEqualTo(2.41, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -52,7 +52,7 @@ public void ageOnJupiter() { public void ageOnSaturn() { SpaceAge age = new SpaceAge(2000000000L); - assertThat(age.onSaturn()).isEqualTo(2.15, within(MAXIMUM_DELTA)); + assertThat(age.onSaturn()).isEqualTo(2.15, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -60,7 +60,7 @@ public void ageOnSaturn() { public void ageOnUranus() { SpaceAge age = new SpaceAge(1210123456L); - assertThat(age.onUranus()).isEqualTo(0.46, within(MAXIMUM_DELTA)); + assertThat(age.onUranus()).isEqualTo(0.46, offset(MAXIMUM_DELTA)); } @Ignore("Remove to run test") @@ -68,6 +68,6 @@ public void ageOnUranus() { public void ageOnNeptune() { SpaceAge age = new SpaceAge(1821023456L); - assertThat(age.onNeptune()).isEqualTo(0.35, within(MAXIMUM_DELTA)); + assertThat(age.onNeptune()).isEqualTo(0.35, offset(MAXIMUM_DELTA)); } }