Scala is a language combining both the features of Object-Oriented Programming and Functional Programming. Since Scala runs on Java Virtual Machine, you can reuse all existing Java Class. In this lab, we will teach you how to write simple Scala programs and explore the object-oriented and functional features of Scala.
Choose "Help" -> "Install New Software..." in the menu bar.
Choose "Scala IDE for Eclipse"
Click the "Next" button.
Accept the licence and then click the "Finish" button.
First, download the material used in this lab here. Then, unzip the file. It will be used through out this lab.
Then, choose "File" -> "Import..." in the menu bar.
Choose "General" -> "Existing Project into Workspace", and then click the "Next" button.
Click the "Browse" button, and then find the directory of the unzipped lab material.
Click the "Finish" button.
Please open the file "HelloWorld.scala" under package "first.example"
Please open the file "FrenchDate.scala" under package "interaction.withjava"
Please open the file "Bicycle.scala" under package "object_oriented.programming"
Please open the file "FunctionVsNonFunc.scala" under package "functional.programming"
Scala also provides functional programming features. The basic properties of functional programming are
Immutable is one of the key concept of functional programming. In Scala, a variable can be declared as immutable and mutable, here is an example:
object VariableExample{ def main(args:Array[String]) = { //mutable variable var mutable_var = 1 mutable_var = 2 //immutable variable val immutable_var = 1 immutable_var = 2 //Error !! } }
Please open the file "Sum.scala" under package "functional.programming"
Some advanced features in functional programming:
Please open the file "ScalaCollections.scala" under package "collections.inScala"
Collections (e.g. List, Map, Array, etc.) are very useful classes in Scala. They will ease your job when you are doing analytical tasks.It is difficult to teach everything in single lab. Here are some useful materials for leaning Scala:
Now we need to define a function to calculate the square root of a float number:
def sqrt(x: Double): Double = ...
We achieve this by successive approximations using Newton's method. Specifically, to compute sqrt(x):
For example, to compute sqrt(2), the iterations are as follows:
Estimation (y) | x/y | Mean |
1 | 2/1=2 | 1.5 |
1.5 | 2/1.5=1.333 | 1.4167 |
1.4167 | 2/1.4167=1.4118 | 1.4142 |
1.4142 | ... | ... |
Please fill in the blanks in the following code:
object NewtonSqrt { //Functions like sqrtIter,improve, and isGoodEnough are ONLY used as the auxiliary functions for sqrt, so we put them inside sqrt. def sqrt(target: Double): Double = { //Test whether the guess is good enough //I.e., test whether Math.abs(guess * guess - target) / target is smaller than a threshold (e.g., 0.001) def isGoodEnough(guess: Double): Boolean = //snippet 1: PUT YOUR CODE HERE //Take the mean of guess and target/guess to improve the guess def improve(guess: Double): Double = //snippet 2: PUT YOUR CODE HERE def sqrtIter(guess: Double): Double = if (isGoodEnough(guess)) guess else sqrtIter(improve(guess)) //It looks like a if-else in Java, but is used for expressions, not statements //The return expression of the function sqrt(target: Double). Here we set the initial guess (i.e., estimate) as 1 sqrtIter(1.0) } def main(args: Array[String]) { println(sqrt(2.0)); } } |
Reference Solution: Snippet 1: if (Math.abs(guess * guess - target) / target < 0.001) true else false |
Snippet 2: (guess + target / guess) / 2.0 |