Monday, September 13, 2010

Boo - Basics by Example



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

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
// Boo Basics  
namespace BooGreetProgram  
import System  
  
private class Greet:  
    // Fields or Attributes  
    private message as string  
    private name as string  
    private loopMessage as int  
    // Properties  
    public Message as string:  
        get:      
            return self.message  
        set:  
            self.message = self.Capitalize(value)  
    public Name as string:  
        get:  
            return self.name  
        set:  
            self.name = self.Capitalize(value)  
    public LoopMessage as int:  
        get:  
            return self.loopMessage  
        set:  
            self.loopMessage = value  
    // Constructor  
    public def constructor():  
        self.message = ""  
        self.name = ""  
        self.loopMessage = 0  
    // Overloaded Constructor  
    public def constructor(message as string, name as string, loopMessage as int):  
        self.message = self.Capitalize(message)  
        self.name = self.Capitalize(name)  
        self.loopMessage = loopMessage  
    // Method 1  
    private def Capitalize(val as string) as string:  
        // "if-then-else" statement  
        if val.Length >= 1:  
            return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1)    
        else:  
            return ""  
    // Method 2  
    public def Salute() as void:  
        // "for" statement  
        for i as int in range(0, self.loopMessage):  
            print "${self.message} ${self.name}!"   
    // Overloaded Method 2.1  
    public def Salute(mesage as string, name as string, loopMessage as int) as void:  
        // "while" statement  
        i as int = 0  
        while i < loopMessage:  
            print "${self.Capitalize(message)} ${self.Capitalize(name)}!"   
            i++  
    // Overloaded Method 2.2  
    public def Salute(name as string) as void:  
        // "switch/case" statement is not supported    
        // so I'm using if then else if...    
        dtNow as DateTime = DateTime.Now    
        hh as int = dtNow.Hour  
        if hh==6 or hh==7 or hh==8 or hh==9 or hh==10 or hh==11:  
            self.message = "good morning,"  
        elif hh == 12 or hh == 13 or hh == 14 or hh == 15 or hh == 16 or hh == 17:  
            self.message = "good evening,"  
        elif hh == 23 or hh == 0 or hh == 1 or hh == 2 or hh == 3 or hh == 4 or hh == 5:  
            self.message = "good night,"  
        else:  
            self.message = "huh?"  
        print "${self.Capitalize(self.message)} ${self.Capitalize(name)}!"   

// Console Program  
public def Main(argv as (string)):    
    // Define variable object of type Greet  
    g as Greet  
    // Instantiate Greet. Call Constructor    
    g = 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, "boo", g.LoopMessage)  
    // Call Overloaded Method 2.2  
    g.Salute("carlos")  
    // Stop and exit  
    print "Press any key to exit..."  
    Console.ReadKey()

Greetings Program - Minimal
// Boo Basics  
import System  

class Greet:  
    // Fields or Attributes  
    message as string  
    name as string  
    loopMessage as int  
    // Properties  
    Message as string:  
        get:  
            return message  
        set:  
            message = Capitalize(value)  
    Name as string:  
        get:  
            return name  
        set:  
            name = Capitalize(value)  
    LoopMessage as int:  
        get:  
            return loopMessage  
        set:  
            loopMessage = value  
    // Constructor  
    def constructor():  
        message = ""  
        name = ""  
        loopMessage = 0  
    // Overloaded Constructor  
    def constructor(message as string, name as string, loopMessage as int):  
        self.message = Capitalize(message)  
        self.name = Capitalize(name)  
        self.loopMessage = loopMessage  
    // Method 1  
    private def Capitalize(val as string) as string:  
        // "if-then-else" statement  
        if val.Length >= 1:
            return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1)    
        else:  
            return ""  
    // Method 2  
    def Salute():  
        // "for" statement  
        for i as int in range(0, self.loopMessage):  
            print "${message} ${name}!"   
        // Overloaded Method 2.1  
    def Salute(mesage as string, name as string, loopMessage as int):  
        // "while" statement  
        i = 0  
        while i < loopMessage:  
            print "${Capitalize(message)} ${Capitalize(name)}!"   
            i++  
    // Overloaded Method 2.2  
    def Salute(name as string):  
        // "switch/case" statement is not supported    
        // so I'm using if then else if...    
        dtNow = DateTime.Now    
        hh as int = dtNow.Hour  
        if hh==6 or hh==7 or hh==8 or hh==9 or hh==10 or hh==11:  
            message = "good morning,"  
        elif hh == 12 or hh == 13 or hh == 14 or hh == 15 or hh == 16 or hh == 17:  
            message = "good evening,"  
        elif hh == 23 or hh == 0 or hh == 1 or hh == 2 or hh == 3 or hh == 4 or hh == 5:  
            message = "good night,"  
        else:  
            message = "huh?"  
        print "${Capitalize(message)} ${Capitalize(name)}!"   

// Console Program  
// Define variable object of type Greet and Instantiate Greet. Call Constructor    
g = 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, "boo", g.LoopMessage)  
// Call Overloaded Method 2.2  
g.Salute("carlos")  
// Stop and exit  
print "Press any key to exit..."  
Console.ReadKey()


And the Output is:


















Auto-Implemented Properties in Boo

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. The following code shows how to use them just like with VB.NET, C#, C++/CLI and so on.

//That means that we can omit the Fields/Attributes declaration  
//and go directly to the properties  
class Greet:
 // Fields or Attributes 
 //message as string 
 //name as string
 //loopMessage as int
 // Properties     
 [Property(Message)]
 message as string
 [Property(Name)]
 name as string
 [Property(LoopMessage)]
 loopMessage as int

// then, when ever you want to use them you get and set their value using the  properties or using the auto-generated attributes (different from C# where you need to use the property) 
// Let's see an example in our parameter-less constructor 

 // Constructor
 def constructor():
  self.Message = ""
  self.Name = ""
  self.LoopMessage = 0
        // or
  self.message = ""
  self.name = ""
  self.loopMessage = 0

No comments:

Post a Comment