Added example program "Rational".

This commit is contained in:
Paul Phillips 2011-02-14 14:48:57 -08:00
parent 91f89bb873
commit 292533ab06
1 changed files with 26 additions and 0 deletions

View File

@ -0,0 +1,26 @@
package example
// Primary constructor takes two arguments.
class Rational(n: Int, d: Int) {
// an auxiliary constructor: must call the primary constructor first.
def this(n: Int) = this(n, 1)
// A requirement: an exception is thrown upon construction if the condition is false.
require(d > 0, "denominator must be greater than zero")
// A grossly inefficient greatest common divisor, for illustrative purposes only.
private def gcd(n: Int, d: Int) = (
n max d to 2 by -1 find (g => n % g == 0 && d % g == 0) getOrElse 1
)
// Using the gcd to calculate reduced numerator and denominator.
private val g = gcd(n, d)
// Public, immutable values.
val numerator = n / g
val denominator = d / g
// toString overrides a method in AnyRef so "override" is required.
override def toString = n + "/" + d + (
// The result of the if/else is a String:
if (numerator == n) "" // the empty string if it is irreducible
else " (" + numerator + "/" + denominator + ")" // the reduced form otherwise
)
}