diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-09 23:13:23 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 859af2fa66c152ab6fa2ddc4003ada65315aa5de (patch) | |
tree | fb6e61f9bbac923752fb651fed9423d2c2296d3c | |
parent | f3e457db6fbefe3acc95c97e04d1a972ebc5f123 (diff) | |
download | coursera-859af2fa66c152ab6fa2ddc4003ada65315aa5de.zip coursera-859af2fa66c152ab6fa2ddc4003ada65315aa5de.tar.gz |
Scala : take care of operators for Rationals class
-rw-r--r-- | Scala/sandbox/3-rationals.scala | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/Scala/sandbox/3-rationals.scala b/Scala/sandbox/3-rationals.scala index 8cdf4d7..d1eda22 100644 --- a/Scala/sandbox/3-rationals.scala +++ b/Scala/sandbox/3-rationals.scala @@ -14,18 +14,18 @@ object Rationals { def numer = x / g def denom = y / g - def neg = new Rational(-numer, denom) + def unary_- = new Rational(-numer, denom) - def less(that: Rational) = this.numer * that.denom < that.numer * this.denom + def < (that: Rational) = this.numer * that.denom < that.numer * this.denom - def max(that: Rational) = if (this.less(that)) that else this + def max(that: Rational) = if (this < (that)) that else this - def add(that: Rational) = + def + (that: Rational) = new Rational( numer * that.denom + that.numer * denom, denom * that.denom) - def sub(that: Rational) = add(that.neg) + def - (that: Rational) = this + -that override def toString = numer + "/" + denom @@ -33,8 +33,8 @@ object Rationals { def run = { println("Rationals") - println(new Rational(2, 3) add (new Rational(3, 4)) toString) - println(new Rational(1, 3) sub (new Rational(5 ,7)) sub (new Rational(3, 2)) toString) + println(new Rational(2, 3) + (new Rational(3, 4)) toString) + println(new Rational(1, 3) - (new Rational(5 ,7)) - (new Rational(3, 2)) toString) println(new Rational(1, 3) max (new Rational(5 ,7)) toString) } |