Fahad/Scala/Chapter6.sc
changeset 50 c603b27083f3
parent 49 c616ec6b1e3c
child 51 3810b37511cb
equal deleted inserted replaced
49:c616ec6b1e3c 50:c603b27083f3
     1 package greeter
       
     2 
       
     3 
       
     4 object Chapter6 {
       
     5   println("Classes and Objects")                  //> Classes and Objects
       
     6 
       
     7   class Rational(n: Int, d: Int) {
       
     8     private def gcd(x: Int, y: Int): Int = {
       
     9       if (x == 0) y
       
    10       else if (x < 0) gcd(-x, y)
       
    11       else if (y < 0) -gcd(x, -y)
       
    12       else gcd(y % x, x)
       
    13     }
       
    14     private val g = gcd(n, d)
       
    15 
       
    16     val numer: Int = n / g
       
    17     val denom: Int = d / g
       
    18     def +(that: Rational) = new Rational(numer * that.denom + that.numer * denom, denom * that.denom)
       
    19     def -(that: Rational) = new Rational(numer * that.denom - that.numer * denom, denom * that.denom)
       
    20     def *(that: Rational) = new Rational(numer * that.numer, denom * that.denom)
       
    21     def /(that: Rational) = new Rational(numer * that.denom, denom * that.numer)
       
    22 
       
    23     //Inheritance and Overriding
       
    24     override def toString = "" + numer + "/" + denom
       
    25     //Parameterless Methods
       
    26     def square = new Rational(numer * numer, denom * denom)
       
    27   }
       
    28   //Inheritance and Overriding
       
    29   var i = 1                                       //> i  : Int = 1
       
    30   var x = new Rational(0, 1)                      //> x  : greeter.Chapter6.Rational = 0/1
       
    31   while (i <= 10) {
       
    32     x += new Rational(1, i)
       
    33     i += 1
       
    34   }
       
    35   println("" + x.numer + "/" + x.denom)           //> 7381/2520
       
    36   //Parameterless Methods
       
    37   val r = new Rational(3, 4)                      //> r  : greeter.Chapter6.Rational = 3/4
       
    38   println(r.square)                               //> 9/16
       
    39 
       
    40   //Abstract Classes
       
    41   abstract class IntSet {
       
    42     def incl(x: Int): IntSet
       
    43     def contains(x: Int): Boolean
       
    44   }
       
    45 
       
    46   //Triats
       
    47   trait IntSett {
       
    48     def incl(x: Int): IntSet
       
    49     def contains(x: Int): Boolean
       
    50   }
       
    51 
       
    52   //Implementing abstract class
       
    53   class EmptySet extends IntSet {
       
    54     def contains(x: Int): Boolean = false
       
    55     def incl(x: Int): IntSet = new NonEmptySet(x, new EmptySet, new EmptySet)
       
    56   }
       
    57 
       
    58   class NonEmptySet(elem: Int, left: IntSet, right: IntSet) extends IntSet {
       
    59     def contains(x: Int): Boolean =
       
    60       if (x < elem) left contains x
       
    61       else if (x > elem) right contains x
       
    62       else true
       
    63     def incl(x: Int): IntSet =
       
    64       if (x < elem) new NonEmptySet(elem, left incl x, right)
       
    65       else if (x > elem) new NonEmptySet(elem, left, right incl x)
       
    66       else this
       
    67   }
       
    68 
       
    69 }