summaryrefslogtreecommitdiffstats
path: root/Scala/sandbox/3-rationals.scala
blob: 387e091b2768831674d0a72cc26d376aae7659d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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)
  }

}