Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. object Planet extends Enumeration {
  2.  
  3. // universal gravitational constant (m3 kg-1 s-2)
  4. val G = 6.67300E-11
  5.  
  6. val Mercury = Planet(3.303e+23, 2.4397e6)
  7. val Venus = Planet(4.869e+24, 6.0518e6)
  8. val Earth = Planet(5.976e+24, 6.37814e6)
  9. val Mars = Planet(6.421e+23, 3.3972e6)
  10. val Jupiter = Planet(1.9e+27, 7.1492e7)
  11. val Saturn = Planet(5.688e+26, 6.0268e7)
  12. val Uranus = Planet(8.686e+25, 2.5559e7)
  13. val Neptune = Planet(1.024e+26, 2.4746e7)
  14.  
  15. case class Planet(mass: Double, radius: Double) extends Val {
  16.  
  17. def surfaceGravity: Double = G * mass / (radius * radius)
  18.  
  19. def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity
  20.  
  21. }
  22.  
  23. }
  24.  
  25. object PlayEnumeration extends App {
  26.  
  27.  
  28. val earthWeight = 175
  29. val mass = earthWeight / Planet.Earth.surfaceGravity
  30.  
  31. Planet.values.foreach {
  32. // Does not compile as might be expected.
  33. // value surfaceWeight is not a member of play.Planet.Value
  34. p => println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
  35. }
  36.  
  37. println
  38.  
  39. }
  40.  
  41. scala> implicit def `plutoid`(p: Planets.Value): Planets.Planet = p.asInstanceOf[Planets.Planet]
  42. warning: there were 1 feature warning(s); re-run with -feature for details
  43. plutoid: (p: Planets.Value)Planets.Planet
  44.  
  45. scala> for (p <- Planets.values) Console println s"Your weight on $p is ${p.surfaceWeight(mass)}"
  46. [snip]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement