Monday, October 18, 2010

Scala - Basics by Example



Update 1: Porting code examples to Scala 2.10.1 - support for String Interpolation.

Continue with the Basics by Example; today's version of the post written in Scala Enjoy!

I was thinking in changing the name of this post to "Scala - OO Basics by Example" because I guess its confusing the fact that Scala is more a Functional Programming language than an Imperative one, even if both paradigms are well supported by the language. At the end I decided to leave it like that, I just want to make clear that you will not find any "Functional Basics" on this post ;) ... As I did with my previous F# post.

You can copy and paste the code below in your favorite IDE/Editor and start playing and learning with it. This little "working" program will teach you the basics of the Programming Language.

There are some "comments" on the code added just to tell you what are or how are some features called. In case you want to review the theory, you can read my previous post, where I give a definition of each of the concepts mentioned on the code. You can find it here: http://carlosqt.blogspot.com/2010/08/new-series-languages-basics-by-example.html 



Greetings Program - Verbose
// Scala Basics  
package com.series  
import scala._    
import java.util.Calendar  
  
// Constructor
// Fields or Attributes
class Greet(private var _message:String = "", 
            private var _name:String = "", 
            private var _loopMessage:Int = 0) {
    //Properties  
    def Message = _message  
    def Message_= (value:String):Unit = _message = Capitalize(value)  
    def Name = _name   
    def Name_= (value:String):Unit = _name = Capitalize(value)  
    def LoopMessage = _loopMessage  
    def LoopMessage_= (value:Int):Unit = _loopMessage = value  
    // Overloaded Constructor  
    def this() = { this("","",0) }   
    // Method 1  
    private def Capitalize(value:String):String = {  
        if (value.length >= 1)  
            value.capitalize  
        else  
            ""  
     }  
    // Method 2  
    def Salute() = {  
        // "for" statement  
        for (i <- 1 to _loopMessage) {              
            println(s"${_message} ${_name}!")
        }  
    }  
    // Overloaded Method 2.1  
    def Salute(message:String, name:String, loopMessage:Int) = {  
        // "while" statement  
        var i:Int = 0  
        while (i < loopMessage) {                      
            println(s"${Capitalize(message)} ${Capitalize(name)}!")
            i += 1  
        }  
    }  
    // Overloaded Method 2.2  
    def Salute(name:String) = {  
        // "switch/case" statement is not supported    
        // using match statement instead  
        val dtNow:Calendar = Calendar.getInstance()  
        val t:Int = dtNow.get(Calendar.HOUR_OF_DAY)      
        t match {  
            case 6|7|8|9|10|11 => _message = "good morning,"    
            case 12|13|14|15|16|17 => _message = "good afternoon,"    
            case 18|19|20|21|22 => _message = "good evening,"    
            case 23|0|1|2|3|4|5 => _message = "good night,"    
            case _ => _message = "huh?"    
            }  
        println(s"${Capitalize(_message)} ${Capitalize(name)}!")  
    }  
}    
   
// Console Program  
object Program {    
    def main(args: Array[String]): Unit = {    
        // Define object of type Greet and Instantiate Greet. Call Constructor   
        val g:Greet = new Greet  
        // Call Set Properties   
        g.Message = "hello"  
        g.Name = "world"  
        g.LoopMessage = 5  
        // Call Method 2  
        g.Salute()  
        // Call Overloaded Method 2.1 and Get Properties  
        g.Salute(g.Message, "scala", g.LoopMessage)  
        // Call Overloaded Method 2.2   
        g.Salute("carlos")  
    }    
}

Greetings Program - Minimal
import java.util.Calendar 
    
// Constructor
// Fields or Attributes
class Greet(private var _message:String = "", 
            private var _name:String = "", 
            private var _loopMessage:Int = 0) {
    //Properties  
    def Message = _message  
    def Message_= (value:String):Unit = _message = Capitalize(value)  
    def Name = _name   
    def Name_= (value:String):Unit = _name = Capitalize(value)  
    def LoopMessage = _loopMessage  
    def LoopMessage_= (value:Int):Unit = _loopMessage = value  
    // Overloaded Constructor  
    def this() = this("","",0)
    // Method 1  
    private def Capitalize(value:String) = {  
        if (value.length >= 1)  
            value.capitalize  
        else  
            ""  
     }  
    // Method 2  
    def Salute() = {  
        // "for" statement  
        for (i <- 1 to _loopMessage) {              
            println(s"${_message} ${_name}!")
        }  
    }  
    // Overloaded Method 2.1  
    def Salute(message:String, name:String, loopMessage:Int) = {  
        // "while" statement  
        var i = 0  
        while (i < loopMessage) {                      
            println(s"${Capitalize(message)} ${Capitalize(name)}!")
            i += 1  
        }  
    }  
    // Overloaded Method 2.2  
    def Salute(name:String) = {  
        // "switch/case" statement is not supported    
        // using match statement instead  
        val dtNow = Calendar.getInstance  
        val t = dtNow.get(Calendar.HOUR_OF_DAY)      
        t match {  
            case 6|7|8|9|10|11 => _message = "good morning,"    
            case 12|13|14|15|16|17 => _message = "good afternoon,"    
            case 18|19|20|21|22 => _message = "good evening,"    
            case 23|0|1|2|3|4|5 => _message = "good night,"    
            case _ => _message = "huh?"    
            }  
        println(s"${Capitalize(_message)} ${Capitalize(name)}!")  
    }  
}    
   
// Console Program  
object Program {    
    def main(args: Array[String]) = {    
        // Define object of type Greet and Instantiate Greet. Call Constructor   
        val g = new Greet  
        // Call Set Properties   
        g.Message = "hello"  
        g.Name = "world"  
        g.LoopMessage = 5  
        // Call Method 2  
        g.Salute()  
        // Call Overloaded Method 2.1 and Get Properties  
        g.Salute(g.Message, "scala", g.LoopMessage)  
        // Call Overloaded Method 2.2   
        g.Salute("carlos")  
    }    
}


And the Output is:


No comments:

Post a Comment