Scala Quick Notes :: Part - 3


Bhaskar S 01/18/2015


Overview

In this part of the series, we will look at the Object-Oriented capabilities of Scala such as defining and using a class, as well as defining and using a companion object.

Hands-on With Scala - III

The following is the Scala program named Sample08.scala:

Sample08.scala
/*
 *
 * Name  : Sample08
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/18/2015
 *  
 */

package com.polarsparc.scala

class EContact1 {
  private var name: String = ""
  private var email: String = ""
  private var zip: String = "10001"
   
  def this(n: String, e: String, z: String) = {
    // NOTE: The first line must be a constructor
    this()
    if (n != null) name = n
    if (e != null) email = e
    if (z != null) zip = z
  }
    
  def getName = name
  def setName(n: String) = {
    if (n != null) name = n
  }
    
  def getEmail = email
  def setEmail(e: String) = {
    if (e != null) email = e
  }
    
  def getZip = zip
  def setZip(z: String) = {
    if (z != null) zip = z
  }
    
  override def toString() = {
    "EContact1-[" + this.getName + ", " + this.getEmail + ", " + this.getZip + "]"
  }
}

class EContact2 {
  var name: String = ""
  var email: String = ""
  var zip: String = "10001"
    
  override def toString() = {
    "EContact2-[" + name + ", " + email + ", " + zip + "]"
  }
}

class EContact3(val name: String, val email: String, val zip: String = "10001") {
  override def toString() = {
    "EContact3-[" + name + ", " + email + ", " + zip + "]"
  }
}

object Sample08 {
  def main(args: Array[String]) = {
    val c1: EContact1 = new EContact1
    c1.setName("Alice")
    c1.setEmail("alice@earth.com")
    c1.setZip("10002")
    printf("c1 = %s\n", c1)
    
    val c2: EContact1 = new EContact1("Bob", "bob@mars.com", "10003")
    printf("c2 = %s\n", c2)
    
    val c3: EContact2 = new EContact2
    c3.name = "Charlie"
    c3.email = "charlie@space.com"
    c3.zip = "10004"
    printf("c3 = %s\n", c3)
    
    val c4: EContact3 = new EContact3("Eve", "eve@mars.com", "10005")
    printf("c4 = %s\n", c4)
    
    val c5: EContact3 = new EContact3("Frank", "frank@northpole.com")
    printf("c5 = %s\n", c5)
  }
}

Executing the program Sample08.scala results in the output:

Output (Sample08.scala)

c1 = EContact1-[Alice, alice@earth.com, 10002]
c2 = EContact1-[Bob, bob@mars.com, 10003]
c3 = EContact2-[Charlie, charlie@space.com, 10004]
c4 = EContact3-[Eve, eve@mars.com, 10005]
c5 = EContact3-[Frank, frank@northpole.com, 10001]

The following section explains some of the aspects of the Scala program Sample08.scala:

The following is the Scala program named Sample09.scala:

Sample09.scala
/*
 *
 * Name  : Sample09
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/18/2015
 *  
 */

package com.polarsparc.scala

class Account1(no: String, val name: String, var balance: Double) {
  def credit(amt: Double) = {
    if (amt > 0.0) balance += amt
  }
  
  def debit(amt: Double) = {
    if (amt > balance) throw new IllegalArgumentException("Balance less than the specified amount")
    balance -= amt
  }
  
  override def toString() = {
    "Account-[" + no + ", " + name + ", " + balance + "]"
  }
}

class Account2(no: String, val name: String, private var balance: Double) {
  def credit(amt: Double) = {
    if (amt > 0.0) balance += amt
  }
  
  def debit(amt: Double) = {
    if (amt > balance) throw new IllegalArgumentException("Balance less than the specified amount")
    balance -= amt
  }
  
  override def toString() = {
    "Account-[" + no + ", " + name + ", " + balance + "]"
  }
}

object Sample09 {
  def main(args: Array[String]) = {
    val a1 = new Account1("1000", "Alice", 1000.00d)
    a1.credit(125)
    a1.debit(75)
    printf("[1] a1 = %s\n", a1)
    // printf("a1.no = %s\n", a1.no) - This will cause a compiler error if uncommented
    printf("a1.name = %s\n", a1.name)
    // a1.name = "Eve" - This will cause a compiler error if uncommented
    printf("a1.balance = %4.2f\n", a1.balance)
    a1.balance = 100.00
    printf("[2] a1 = %s\n", a1)
    
    val a2 = new Account2("1001", "Bob", 1000.00d)
    a2.credit(75)
    a2.debit(125)
    printf("a2 = %s\n", a2)
    printf("a2.name = %s\n", a2.name)
    // printf("a2.balance = %4.2f\n", a2.balance) - This will cause a compiler error if uncommented
  }
}

Executing the program Sample09.scala results in the output:

Output (Sample09.scala)

[1] a1 = Account-[1000, Alice, 1050.0]
a1.name = Alice
a1.balance = 1050.00
[2] a1 = Account-[1000, Alice, 100.0]
a2 = Account-[1001, Bob, 950.0]
a2.name = Bob

The following section explains some of the aspects of the Scala program Sample09.scala:

The following is the Scala program named Sample10.scala:

Sample10.scala
/*
 *
 * Name  : Sample10
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/18/2015
 *  
 */

package com.polarsparc.scala

object MyMathFun {
  val PI = 3.14159
  
  def fibonacci(num: Int): Int = {
    if (num >= 0) {
      num match {
        case 0 => 0
        case 1 => 1
        case 2 => 1
        case _ => fibonacci(num-1) + fibonacci(num-2)
      }
    } else {
      throw new IllegalArgumentException("Specified number must be positive")
    }
  }
  
  def factorial(num: Int): Int = {
    if (num >= 0) {
      num match {
        case 0 => 1
        case 1 => 1
        case _ => num * factorial(num-1)
      }
    } else {
      throw new IllegalArgumentException("Specified number must be positive")
    }
  }
}

object Sample10 {
  def main(args: Array[String]) = {
    printf("Value of PI = %.2f\n", MyMathFun.PI)
    printf("Area of circle with radius 10 m = %.2f sqm\n", MyMathFun.PI * 10 * 10)
    
    printf("fibonacci(0) = %d\n", MyMathFun.fibonacci(0))
    printf("fibonacci(1) = %d\n", MyMathFun.fibonacci(1))
    printf("fibonacci(2) = %d\n", MyMathFun.fibonacci(2))
    printf("fibonacci(5) = %d\n", MyMathFun.fibonacci(5))
    printf("fibonacci(10) = %d\n", MyMathFun.fibonacci(10))
    
    printf("factorial(0) = %d\n", MyMathFun.factorial(0))
    printf("factorial(1) = %d\n", MyMathFun.factorial(1))
    printf("factorial(2) = %d\n", MyMathFun.factorial(2))
    printf("factorial(5) = %d\n", MyMathFun.factorial(5))
    printf("factorial(10) = %d\n", MyMathFun.factorial(10))
  }
}

Executing the program Sample10.scala results in the output:

Output (Sample10.scala)

Value of PI = 3.14
Area of circle with radius 10 m = 314.16 sqm
fibonacci(0) = 0
fibonacci(1) = 1
fibonacci(2) = 1
fibonacci(5) = 5
fibonacci(10) = 55
factorial(0) = 1
factorial(1) = 1
factorial(2) = 2
factorial(5) = 120
factorial(10) = 3628800

The following section explains some of the aspects of the Scala program Sample10.scala:

The following is the Scala program named Sample11.scala:

Sample11.scala
/*
 *
 * Name  : Sample11
 * 
 * Author: Bhaskar S
 *  
 * Date  : 01/18/2015
 *  
 */

package com.polarsparc.scala

class Font1(name: String) {
  override def toString() = {
    "Font1-[" + name + "]"
  }
}

object Font1 {
  def getFont1(name: String): Font1 = {
    new Font1(name)
  }
}

class Font2 private (name: String) {
  override def toString() = {
    "Font2-[" + name + "]"
  }
}

object Font2 {
  def getFont2(name: String) = {
    new Font2(name)
  }
}

object Sample11 {
  def main(args: Array[String]) = {
    val f1 = Font1.getFont1("Arial")
    printf("f1 = %s\n", f1)
    val f2 = new Font1("Courier")
    printf("f2 = %s\n", f2)
    
    val f3 = Font2.getFont2("Sans")
    printf("f3 = %s\n", f3)
    // val f4 = new Font2("Serif") - This will cause a compiler error if uncommented
  }
}

Executing the program Sample11.scala results in the output:

Output (Sample11.scala)

f1 = Font1-[Arial]
f2 = Font1-[Courier]
f3 = Font2-[Sans]

The following section explains some of the aspects of the Scala program Sample11.scala:

References

Scala Quick Notes :: Part - 1

Scala Quick Notes :: Part - 2