Skip to content

Latest commit

 

History

History
102 lines (84 loc) · 3.87 KB

File metadata and controls

102 lines (84 loc) · 3.87 KB

Exercises: A Simple Calculator

In this first exercise you will put altogether you have learned so far. Instead of worksheets you will put your code into classes and objects. You will also define a Scala singleton object with a main method allowing you to start it as a Scala application.

In the code skeletons you will find an object definition with a main method. In your IDE just do a right click > Run As > Scala Application

Example: main() method for starting as a Scala Application

object ScalaApp {
  def main(args: Array[String]) {
    val instance = new ScalaFun
    println("> " + instance + ": " + ScalaFun.dateFormat.format(instance.constructionTime))
  }
}

Singleton Objects

Now let's test your skill so far and free your limitations as a Java developer and introduce object classes ;)

Example: Definition and Construction

  object ScalaFun1
  object ScalaFun2 {}
  object ScalaFun3 { val message = "Hello, Scala" }
  ScalaFun3.message                               //> res0: String = Hello, Scala
  //compile error, since other constructor definitions are not allowed
  object ScalaFun4 {
    var message = "Hello, Scala"
    def this(anotherMessage: String) {
      this()
      this.message = anotherMessage
    }
  }

Calculator

Let's get back to elementary school. Remember you always like to cheat and use your calculator instead of your useless brain? Now is your chance... Implement a calculator with the operators add, minus, multiply and divide.

Example: Skeleton from the exercise

object Calculator {
  def add(a: Double, b: Double) = ???

  def subtract(a: Int, b: Double) = ???

  def multiply(a: Double, b: Double) = ???

  def divide(a: Double, b: Double) = ???
  
}

Interlude - Implicit conversions

Look at the source code of the second exercise:

object Calculator {
  def add(a: Double, b: Double) = ???
  ...
}

object CalcApplication {
  ...
  def main(args: Array[String]) {
    val a = 6
    val b = 3
    ...
    println(s"$a + $b = ${add(a,b)}")
    ...
  }
}

Both values a and b are of type Int and the methods add, subtract, multiply, and divide require Double as input parameters.

How does Scala deal with this? If you look into Int's ScalaDoc you find this:

implicit def int2double(x: Int): Double = x.toDouble

Implicit conversions are one of the most powerful features in Scala. It allows in Scala (as a scalable language) to extend existing classes - and not only new ones. In this example the Scala compiler converts these methods calls into the following:

  1. 'add(a,b)' converts to 'add(int2double(a),int2double(b))'
  2. 'subtract(a,b)' compiles to 'subtract(int2double(a),int2double(b))'
  3. 'multiply(a,b)' compiles to 'multiply(int2double(a),int2double(b))'
  4. 'divide(a,b)' compiles to 'divide(int2double(a),int2double(b))'

You can read more on implicits here and here.

Resources

Implicit conversions