Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Complex(real: Double, imaginary: Double) //scala classes can have parameters unlike Java
- {
- def re() = real
- def im() = imaginary
- }
- class ComplexTwo(real: Double, imaginary: Double)
- {
- def re = real //method without argument. Different from methods with 0 arguments.
- def im = imaginary // methods with 0 arguments have brackets
- }
- class ComplexThree(real: Double, imaginary: Double)
- {
- def re = real
- def im = imaginary
- override def toString() =
- "" + re + (if (im < 0) "" else "+") + im + "i"
- }
- object ComplexClass {
- def main(args: Array[String]) {
- val c = new Complex(1.2, 3.4)
- println("imaginary part: " + c.im())
- val c2 = new Complex(3.0, 1.14)
- println("imaginary part using methods without arguments : " + c2.im)
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement