From f7027732a1d1e92f4d7525f2a984a24fdb7a0053 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 5 Apr 2017 17:09:15 +0200 Subject: Implementation of proposal changes - rename utility methods - generate utility methods also for object cases --- tests/run/planets.scala | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 tests/run/planets.scala (limited to 'tests/run/planets.scala') diff --git a/tests/run/planets.scala b/tests/run/planets.scala new file mode 100644 index 000000000..2fff01edc --- /dev/null +++ b/tests/run/planets.scala @@ -0,0 +1,26 @@ +enum class Planet(mass: Double, radius: Double) { + private final val G = 6.67300E-11 + def surfaceGravity = G * mass / (radius * radius) + def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity +} +object Planet { + case MERCURY extends Planet(3.303e+23, 2.4397e6) + case VENUS extends Planet(4.869e+24, 6.0518e6) + case EARTH extends Planet(5.976e+24, 6.37814e6) + case MARS extends Planet(6.421e+23, 3.3972e6) + case JUPITER extends Planet(1.9e+27, 7.1492e7) + case SATURN extends Planet(5.688e+26, 6.0268e7) + case URANUS extends Planet(8.686e+25, 2.5559e7) + case NEPTUNE extends Planet(1.024e+26, 2.4746e7) +} +object Test { + def main(args: Array[String]) = { + import Planet._ + assert(enumValueNamed("SATURN") == SATURN) + assert(enumValue(2) == EARTH) + val earthWeight = args(0).toDouble + val mass = earthWeight/EARTH.surfaceGravity + for (p <- enumValues) + println(s"Your weight on $p is ${p.surfaceWeight(mass)}") + } +} -- cgit v1.2.3 From 92018967be69ffe660a225f0b58f9772388678fe Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 5 Apr 2017 17:21:48 +0200 Subject: Update test and add check file --- tests/run/planets.check | 8 ++++++++ tests/run/planets.scala | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/run/planets.check (limited to 'tests/run/planets.scala') diff --git a/tests/run/planets.check b/tests/run/planets.check new file mode 100644 index 000000000..feb6f737d --- /dev/null +++ b/tests/run/planets.check @@ -0,0 +1,8 @@ +Your weight on MERCURY is 37.775761520093525 +Your weight on SATURN is 106.60155388115666 +Your weight on VENUS is 90.49990998410455 +Your weight on URANUS is 90.51271993894251 +Your weight on EARTH is 100.0 +Your weight on NEPTUNE is 113.83280724696579 +Your weight on MARS is 37.873718403712886 +Your weight on JUPITER is 253.05575254957407 diff --git a/tests/run/planets.scala b/tests/run/planets.scala index 2fff01edc..bcbfd7eeb 100644 --- a/tests/run/planets.scala +++ b/tests/run/planets.scala @@ -18,7 +18,7 @@ object Test { import Planet._ assert(enumValueNamed("SATURN") == SATURN) assert(enumValue(2) == EARTH) - val earthWeight = args(0).toDouble + val earthWeight = 100 val mass = earthWeight/EARTH.surfaceGravity for (p <- enumValues) println(s"Your weight on $p is ${p.surfaceWeight(mass)}") -- cgit v1.2.3