Fahad/Scala/Chapter6.sc
author fahadausaf <fahad.ausaf@icloud.com>
Sun, 26 Oct 2014 16:42:28 +0000
changeset 32 fa92e8f089a2
child 43 10e7a90d8e7a
permissions -rw-r--r--
no message
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     1
package greeter
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     2
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     3
object Chapter6 {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     4
  println("Classes and Objects")                  //> Classes and Objects
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     5
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     6
  class Rational(n: Int, d: Int) {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     7
    private def gcd(x: Int, y: Int): Int = {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     8
      if (x == 0) y
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
     9
      else if (x < 0) gcd(-x, y)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    10
      else if (y < 0) -gcd(x, -y)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    11
      else gcd(y % x, x)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    12
    }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    13
    private val g = gcd(n, d)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    14
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    15
    val numer: Int = n / g
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    16
    val denom: Int = d / g
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    17
    def +(that: Rational) = new Rational(numer * that.denom + that.numer * denom, denom * that.denom)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    18
    def -(that: Rational) = new Rational(numer * that.denom - that.numer * denom, denom * that.denom)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    19
    def *(that: Rational) = new Rational(numer * that.numer, denom * that.denom)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    20
    def /(that: Rational) = new Rational(numer * that.denom, denom * that.numer)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    21
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    22
    //Inheritance and Overriding
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    23
    override def toString = "" + numer + "/" + denom
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    24
    //Parameterless Methods
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    25
    def square = new Rational(numer * numer, denom * denom)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    26
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    27
  //Inheritance and Overriding
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    28
  var i = 1                                       //> i  : Int = 1
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    29
  var x = new Rational(0, 1)                      //> x  : greeter.Chapter6.Rational = 0/1
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    30
  while (i <= 10) {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    31
    x += new Rational(1, i)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    32
    i += 1
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    33
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    34
  println("" + x.numer + "/" + x.denom)           //> 7381/2520
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    35
  //Parameterless Methods
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    36
  val r = new Rational(3, 4)                      //> r  : greeter.Chapter6.Rational = 3/4
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    37
  println(r.square)                               //> 9/16
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    38
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    39
  //Abstract Classes
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    40
  abstract class IntSet {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    41
    def incl(x: Int): IntSet
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    42
    def contains(x: Int): Boolean
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    43
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    44
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    45
  //Triats
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    46
  trait IntSett {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    47
    def incl(x: Int): IntSet
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    48
    def contains(x: Int): Boolean
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    49
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    50
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    51
  //Implementing abstract class
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    52
  class EmptySet extends IntSet {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    53
    def contains(x: Int): Boolean = false
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    54
    def incl(x: Int): IntSet = new NonEmptySet(x, new EmptySet, new EmptySet)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    55
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    56
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    57
  class NonEmptySet(elem: Int, left: IntSet, right: IntSet) extends IntSet {
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    58
    def contains(x: Int): Boolean =
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    59
      if (x < elem) left contains x
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    60
      else if (x > elem) right contains x
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    61
      else true
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    62
    def incl(x: Int): IntSet =
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    63
      if (x < elem) new NonEmptySet(elem, left incl x, right)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    64
      else if (x > elem) new NonEmptySet(elem, left, right incl x)
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    65
      else this
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    66
  }
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    67
fa92e8f089a2 no message
fahadausaf <fahad.ausaf@icloud.com>
parents:
diff changeset
    68
}