Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.77 KB | None | 0 0
  1. class Complex(real: Double, imaginary: Double) //scala classes can have parameters unlike Java
  2. {
  3.   def re() = real
  4.   def im() = imaginary
  5. }
  6.  
  7. class ComplexTwo(real: Double, imaginary: Double)
  8. {
  9.   def re = real //method without argument. Different from methods with 0 arguments.
  10.   def im = imaginary // methods with 0 arguments have brackets
  11. }
  12.  
  13. class ComplexThree(real: Double, imaginary: Double)
  14. {
  15.   def re = real
  16.   def im = imaginary
  17.   override def toString() =
  18.     "" + re + (if (im < 0) "" else "+") + im + "i"
  19. }
  20. object ComplexClass {
  21.   def main(args: Array[String]) {
  22.     val c = new Complex(1.2, 3.4)
  23.     println("imaginary part: " + c.im())
  24.  
  25.     val c2 = new Complex(3.0, 1.14)
  26.     println("imaginary part using methods without arguments : " + c2.im)
  27.   }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement