diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-04 00:01:13 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:22 +0100 |
commit | 857838ed6133691aa99c097f522acd4484b953f9 (patch) | |
tree | 99f260811f96e1222b07730cf0475cc977595852 /Scala | |
parent | dadc875c05c830d15ee0aebea70aefaea6d3e55e (diff) | |
download | coursera-857838ed6133691aa99c097f522acd4484b953f9.zip coursera-857838ed6133691aa99c097f522acd4484b953f9.tar.gz |
Scala : add sandbox 3-rationals
Diffstat (limited to 'Scala')
-rw-r--r-- | Scala/sandbox/0-main.scala | 1 | ||||
-rw-r--r-- | Scala/sandbox/3-rationals.scala | 27 |
2 files changed, 28 insertions, 0 deletions
diff --git a/Scala/sandbox/0-main.scala b/Scala/sandbox/0-main.scala index 7a0b791..ee22e71 100644 --- a/Scala/sandbox/0-main.scala +++ b/Scala/sandbox/0-main.scala @@ -4,5 +4,6 @@ object Main extends App { Sqrt.run Recursion.run Curry.run + Rationals.run } diff --git a/Scala/sandbox/3-rationals.scala b/Scala/sandbox/3-rationals.scala new file mode 100644 index 0000000..387e091 --- /dev/null +++ b/Scala/sandbox/3-rationals.scala @@ -0,0 +1,27 @@ + +object Rationals { + + class Rational(x :Int, y: Int) { + def numer = x + def denom = y + + def neg = new Rational(-numer, denom) + + def add(that: Rational) = + new Rational( + numer * that.denom + that.numer * denom, + denom * that.denom) + + def sub(that: Rational) = add(that.neg) + + override def toString = numer + "/" + denom + + } + + 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) + } + +} |