From 859af2fa66c152ab6fa2ddc4003ada65315aa5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Tue, 9 Apr 2013 23:13:23 +0200 Subject: Scala : take care of operators for Rationals class --- Scala/sandbox/3-rationals.scala | 14 +++++++------- 1 file 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) } -- cgit v1.1-2-g2b99