summaryrefslogtreecommitdiff
path: root/test/pos/Rational.scala
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2003-02-14 14:39:24 +0000
committerMartin Odersky <odersky@gmail.com>2003-02-14 14:39:24 +0000
commit21b147f7caf8b558b00044f493cf9da392c7e30e (patch)
treea964142a10a16e7c9a64b03e44fdde1d9f1b63be /test/pos/Rational.scala
parentfd3f10df3cd88fe3b970f8312a479ae63f0803b8 (diff)
downloadscala-21b147f7caf8b558b00044f493cf9da392c7e30e.tar.gz
scala-21b147f7caf8b558b00044f493cf9da392c7e30e.tar.bz2
scala-21b147f7caf8b558b00044f493cf9da392c7e30e.zip
Initial version.
Diffstat (limited to 'test/pos/Rational.scala')
-rw-r--r--test/pos/Rational.scala25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/pos/Rational.scala b/test/pos/Rational.scala
new file mode 100644
index 0000000000..14236975d4
--- /dev/null
+++ b/test/pos/Rational.scala
@@ -0,0 +1,25 @@
+class Rational(x: Int, y: Int) {
+ private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b);
+ private def g = gcd(x, y);
+ def numer = x / g;
+ def denom = y / g;
+ def add(r: Rational) =
+ new Rational(
+ numer * r.denom + r.numer * denom,
+ denom * r.denom);
+ def sub(r: Rational) =
+ new Rational(
+ numer * r.denom - r.numer * denom,
+ denom * r.denom);
+ def mul(r: Rational) =
+ new Rational(
+ numer * r.numer,
+ denom * r.denom);
+ def equal(r: Rational) =
+ new Rational(
+ numer * r.denom,
+ denom * r.numer);
+
+ def asString: String = numer + "/" + denom;
+ override def toString(): String = numer + "/" + denom
+}