Skip to content

Commit

Permalink
Update space-age: less strict on precision (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderploegsma authored Jul 28, 2023
1 parent 38a5a48 commit 9d983ba
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
1 change: 1 addition & 0 deletions exercises/practice/space-age/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lihofm",
"mdowds",
"nithia",
"sanderploegsma",
"sjwarner-bp",
"SleeplessByte",
"stkent",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import java.math.BigDecimal
import java.math.RoundingMode

class SpaceAge(private val seconds: Long) {

companion object {
const val EARTH_ORBITAL_PERIOD_IN_SECONDS = 31557600.0
const val PRECISION = 2

private enum class Planet(val relativeOrbitalPeriod: Double) {
EARTH(1.0),
Expand All @@ -28,9 +24,6 @@ class SpaceAge(private val seconds: Long) {
fun onUranus() = calculateAge(Planet.URANUS)
fun onNeptune() = calculateAge(Planet.NEPTUNE)

private fun calculateAge(planet: Planet): Double {
val age: Double = seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.relativeOrbitalPeriod)

return BigDecimal(age).setScale(PRECISION, RoundingMode.HALF_UP).toDouble()
}
private fun calculateAge(planet: Planet): Double =
seconds / (EARTH_ORBITAL_PERIOD_IN_SECONDS * planet.relativeOrbitalPeriod)
}
5 changes: 4 additions & 1 deletion exercises/practice/space-age/src/test/kotlin/SpaceAgeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ class SpaceAgeTest {
fun `age on Neptune`() = assertYearsEqual(0.35, SpaceAge(1821023456).onNeptune())
}

private fun assertYearsEqual(expectedYears: Double, actualYears: Double) = assertEquals(expectedYears, actualYears)
private const val TOLERANCE = 0.01

private fun assertYearsEqual(expectedYears: Double, actualYears: Double) =
assertEquals(expectedYears, actualYears, absoluteTolerance = TOLERANCE)

0 comments on commit 9d983ba

Please sign in to comment.