Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object Planet extends Enumeration {
- // universal gravitational constant (m3 kg-1 s-2)
- val G = 6.67300E-11
- val Mercury = Planet(3.303e+23, 2.4397e6)
- val Venus = Planet(4.869e+24, 6.0518e6)
- val Earth = Planet(5.976e+24, 6.37814e6)
- val Mars = Planet(6.421e+23, 3.3972e6)
- val Jupiter = Planet(1.9e+27, 7.1492e7)
- val Saturn = Planet(5.688e+26, 6.0268e7)
- val Uranus = Planet(8.686e+25, 2.5559e7)
- val Neptune = Planet(1.024e+26, 2.4746e7)
- case class Planet(mass: Double, radius: Double) extends Val {
- def surfaceGravity: Double = G * mass / (radius * radius)
- def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity
- }
- }
- object PlayEnumeration extends App {
- val earthWeight = 175
- val mass = earthWeight / Planet.Earth.surfaceGravity
- Planet.values.foreach {
- // Does not compile as might be expected.
- // value surfaceWeight is not a member of play.Planet.Value
- p => println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
- }
- println
- }
- scala> implicit def `plutoid`(p: Planets.Value): Planets.Planet = p.asInstanceOf[Planets.Planet]
- warning: there were 1 feature warning(s); re-run with -feature for details
- plutoid: (p: Planets.Value)Planets.Planet
- scala> for (p <- Planets.values) Console println s"Your weight on $p is ${p.surfaceWeight(mass)}"
- [snip]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement