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