Saturday, November 13, 2010

Gosu - Basics by Example



Extending my Basics by Example series to a new language :D. today's version of the post written in Gosu 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
// Gosu Basics
classpath "."
//package gsgreetprogram
uses java.util.Calendar
uses java.util.GregorianCalendar
uses java.util.Scanner
uses java.lang.System

public class Greet {
    // Fields or Attributes
    private var _message: String
    private var _name: String
    private var _loopMessage: int
    // Properties
    public property get Message(): String {
        return this._message
    }
    public property set Message(value: String) {
        this._message = this.capitalize(value)
    }
    public property get Name(): String {
        return this._name 
    }
    public property set Name(value: String) {
        this._name = this.capitalize(value)
    }
    public property get LoopMessage(): int {
        return this._loopMessage
    }
    public property set LoopMessage(value: int) {
        this._loopMessage = value
    }
    // Constructor
    public construct() {
        this._message = ""
        this._name = ""
        this._loopMessage = 0
    }
    // Overloaded Constructor
    public construct(pmessage: String, pname: String, ploopMessage: int) {
        this._message = pmessage
        this._name = pname
        this._loopMessage = ploopMessage
    }
    // Method 1
    private function capitalize(val: String): String {
        // "if-then-else" statement
        if(val.length >= 1) {
            return val.capitalize()
        }
        else {
            return ""
        }
    }
    // Method 2
    public function salute() {
        // "for" statement        
        for (i in 0..this._loopMessage) {
            print("${this._message} ${this._name}!")
        }
    }
    // Overloaded Method 2.1
    public function salute(pmessage: String, pname: String, ploopMessage: int) {
        // "while" statement
        var i: int = 0
        while (i < ploopMessage) {
            print("${this.capitalize(pmessage)} ${this.capitalize(pname)}!")
            i = i + 1
        }
    }
    // Overloaded Method 2.2    
 public function salute(pname: String) {    
  // "switch/case" statement    
  var dtNow = new GregorianCalendar()
  switch (dtNow.get(Calendar.HOUR_OF_DAY))
  {    
   case 6: case 7: case 8: case 9: case 10: case 11:    
    this._message = "good morning,"
    break
   case 12: case 13: case 14: case 15: case 16: case 17:    
    this._message = "good afternoon,"
    break
   case 18: case 19: case 20: case 21: case 22:    
    this._message = "good evening,"
    break
   case 23: case 0: case 1: case 2: case 3: case 4: case 5:    
    this._message = "good night,"
    break
   default:    
    this._message = "huh?"
  }    
  print("${this.capitalize(this._message)} ${this.capitalize(pname)}!")
 }    
}

// Console Program
// Define variable object of type Greet and Instantiate. Call Constructor        
var g = new Greet()
// Call Set Properties        
g.Message = "hello"            
g.Name = "world"            
g.LoopMessage = 5
// Call Method 2            
g.salute()        
// Call Method 2.1 and Get Properties            
g.salute(g.Message, "gosu", g.LoopMessage)        
// Call Method 2.2            
g.salute("carlos")            
// Stop and exit            
print("Press any key to exit...")
var sin = new Scanner(System.in)
var line = sin.nextLine()
sin.close()

Greetings Program - Minimal
// Gosu Basics
classpath "."
uses java.util.Calendar
uses java.util.GregorianCalendar
uses java.util.Scanner
uses java.lang.System

class Greet {
    // Fields or Attributes
    var _message: String
    var _name: String
    var _loopMessage: int
    // Properties
    property get Message(): String {
        return _message
    }
    property set Message(value: String) {
        _message = capitalize(value)
    }
    property get Name(): String {
        return _name 
    }
    property set Name(value: String) {
        _name = capitalize(value)
    }
    property get LoopMessage(): int {
        return _loopMessage
    }
    property set LoopMessage(value: int) {
        _loopMessage = value
    }
    // Constructor
    construct() {
        _message = ""
        _name = ""
        _loopMessage = 0
    }
    // Overloaded Constructor
    construct(pmessage: String, pname: String, ploopMessage: int) {
        _message = pmessage
        _name = pname
        _loopMessage = ploopMessage
    }
    // Method 1
    private function capitalize(val: String): String {
        // "if-then-else" statement
        if(val.length >= 1) {
            return val.capitalize()
        }
        else {
            return ""
        }
    }
    // Method 2
    function salute() {
        // "for" statement        
        for (i in 0.._loopMessage) {
            print("${_message} ${_name}!")
        }
    }
    // Overloaded Method 2.1
    function salute(pmessage: String, pname: String, ploopMessage: int) {
        // "while" statement
        var i = 0
        while (i < ploopMessage) {
            print("${capitalize(pmessage)} ${capitalize(pname)}!")
            i = i + 1
        }
    }
    // Overloaded Method 2.2    
    function salute(pname: String) {
        // "switch/case" statement    
        var dtNow = new GregorianCalendar()
        switch (dtNow.get(Calendar.HOUR_OF_DAY))
        {    
            case 6: case 7: case 8: case 9: case 10: case 11:    
                _message = "good morning,"
                break
            case 12: case 13: case 14: case 15: case 16: case 17:
                _message = "good afternoon,"
                break
            case 18: case 19: case 20: case 21: case 22:    
                _message = "good evening,"
                break
            case 23: case 0: case 1: case 2: case 3: case 4: case 5:    
                _message = "good night,"
                break    
            default:    
                _message = "huh?"
        }    
        print("${capitalize(_message)} ${capitalize(pname)}!")
    }    
}

// Console Program
// Define variable object of type Greet and Instantiate. Call Constructor        
var g = new Greet()
// Call Set Properties        
g.Message = "hello"            
g.Name = "world"            
g.LoopMessage = 5
// Call Method 2            
g.salute()        
// Call Method 2.1 and Get Properties            
g.salute(g.Message, "gosu", g.LoopMessage)        
// Call Method 2.2            
g.salute("carlos")            
// Stop and exit            
print("Press any key to exit...")
var sin = new Scanner(System.in)
var line = sin.nextLine()
sin.close()


And the Output is:




Auto-Implemented Properties in Gosu

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.

// Gosu Basics
class AutoImplementedProperties {
    // Fields + Auto-Implemented Properties
    var _message: String as Message
    var _name: String as Name
    var _loopMessage: int as LoopMessage
    // Methods
    function salute() {
        print("${_message.capitalize()} ${_name.capitalize()} ${_loopMessage}!")
    }
}

var g = new AutoImplementedProperties()
// Call Set Properties
g.Message = "hello"
g.Name = "world"
g.LoopMessage = 5
// print them out
g.salute()
// and print them again using Get Properties
print(g.Message + " " + g.Name + " " + g.LoopMessage + "!")

And the output is:

Friday, November 12, 2010

OO Hello World - Gosu



The Hello World version of the program in Gosu!

A new OO programming language targeting the JVM with a Java-like syntax and compatible with java code. This looks like a promising language that deserves to be in the list of the ~20 languages I'm writing on here.

So, let's see how it looks like and how it compares to the other langs in a minimal OO program :D 


By the way, you can see my previous post here: http://carlosqt.blogspot.com/2010/06/oo-hello-world.html
where I give some details on WHY these "OO Hello World series" samples. 


Version 1 (Minimal):
The minimum you need to type to get your program compiled and running.

class Greet {
    var _name: String
    construct(name: String) {
        _name = name.capitalize()  
    }
    function salute() {
        print("Hello ${_name}!")
    }
}

// Greet Program
var g = new Greet("world")
g.salute()

Version 2 (Verbose):
Explicitly adding instructions and keywords that are optional to the compiler.

//classpath "."
//package greetprogram
uses java.util.*

public class Greet {
    private var _name: String
    public construct(name: String) {
        this._name = name.capitalize()  
    }
    public function salute() {
        print("Hello ${this._name}!")
    }
}

// Greet Program
var g = new Greet("world")
g.salute()

The Program Output:









Gosu Info:
“Gosu is an imperative statically-typed object-oriented programming language that is designed to be expressive, easy-to-read, and reasonably fast. Gosu supports several resource types” Taken from: (http://gosu-lang.org/intro.shtml)

Appeared:
2010
Current Version:
Developed by:
Guidewire Software
Creator:

Influenced by:
Java (James Gosling)
Predecessor Language

Predecessor Appeared

Predecessor Creator

Runtime Target:
JVM
Latest Framework Target:
JDK 6
Mono Target:
No
Allows Unmanaged Code:
No
Source Code Extension:
“.gsp”
Keywords:
53
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
Yes
Standard:

Latest IDE Support:
Eclipse
IntelliJ IDEA
Language Reference:
Extra Info:


Monday, November 1, 2010

Oxygene - Basics by Example



Continue with the Basics by Example; today's version of the post written in Oxygene 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
// Delphi Prism Basics
namespace DPGreetProgram;  

interface
uses
    System;   
  
type 
    Greet = public class      
        // Fields of Attributes
        private var fMessage: String;            
        private var fName: String;
        private var fLoopMessage: Integer;
        // Methods Definition
        private method Capitalize(value: String): String;
        private method SetMessage(value: String);
        private method SetName(value: String);    
        // Properties
        public property Message: String read fMessage write SetMessage;
        public property Name: String read fName write SetName;
        public property LoopMessage: Integer read fLoopMessage write fLoopMessage;        
        // Constructors Definition
        public constructor();
        public constructor(message: String; name: String; loopMessage: Integer);  
        // Methods Definition
        public method Salute();
        public method Salute(message: String; name: String; loopMessage: Integer);  
        public method Salute(name: String);
    end;  
  
type  
    GreetProgram = public class      
    public class method Main(args: array of String);  
    end;

implementation
// Property Setters/Getters Methods
method Greet.SetMessage(value: String);
begin
    self.fMessage := self.Capitalize(value);
end;
method Greet.SetName(value: String);
begin
    self.fName := self.Capitalize(value);
end;
// Constructor 
constructor Greet();
begin
    self.fMessage := "";
    self.fName := "";
    self.loopMessage := 0;    
end;  
// Overloaded Constructor 
constructor Greet(message: String; name: String; loopMessage: Integer);
begin
    self.fMessage := message;
    self.fName := name;
    self.loopMessage := loopMessage;
end;
// Method 1
method Greet.Capitalize(value: String): String;
begin
    // "if-then-else" statement 
    if value.Length >= 1 then 
    begin
        result := value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1);
    end
    else 
    begin
        result := "";
    end;
end;
// Method 2
method Greet.Salute();
begin  
    // "for" statement 
    for i: Integer := 1 to self.loopMessage step 1 do
    begin
        Console.WriteLine("{0} {1}!", self.fMessage, self.fName);
    end;
end;  
// Overloaded Method 2.1 
method Greet.Salute(message: String; name: String; loopMessage: Integer);
var
    i: Integer;
begin
    // "while" statement  
    i := 0;
    while i < loopMessage do 
    begin
        Console.WriteLine("{0} {1}!", self.Capitalize(message), self.Capitalize(name));
        i := i + 1;
    end;
end;
// Overloaded Method 2.2  
method Greet.Salute(name: String);
var    
    dtNow: DateTime;
begin
    // "switch/case" statement  
    dtNow := DateTime.Now;
    case dtNow.hour of
        6..11: self.fMessage := "good morning,";
        12..17: self.fMessage := "good afternoon,";
        18..22: self.fMessage := "good evening,";
        23,0..5: self.fMessage := "good night,";
        else self.fMessage := "huh?"; 
    end;
    Console.WriteLine("{0} {1}!", self.Capitalize(self.fMessage), self.Capitalize(name));
end;

// Console Program
class method GreetProgram.Main(args: array of String);  
var
    // Define object of type Greet    
    g: Greet;
begin  
    // Instantiate Greet. Call Constructor 
    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, "delphi Prism", g.LoopMessage);  
    // Call Overloaded Method 2.2  
    g.Salute("carlos");
    
    // Stop and exit  
    Console.WriteLine("Press any key to exit...");  
    Console.Read();  
end;  
end.


Greetings Program - Minimal
// Delphi Prism Basics
namespace;

interface
uses
    System;   
  
type
    Greet = class  
    private
        // Fields of Attributes
        fMessage: String;
        fName: String;
        fLoopMessage: Integer;
        // Methods Definition
        method Capitalize(value: String): String;
        method SetMessage(value: String);
        method SetName(value: String);
    public
        // Properties
        property Message: String read fMessage write SetMessage;
        property Name: String read fName write SetName;
        property LoopMessage: Integer read fLoopMessage write fLoopMessage;        
        // Constructors Definition
        constructor;
        constructor(message: String; name: String; loopMessage: Integer);  
        // Methods Definition
        method Salute;
        method Salute(message: String; name: String; loopMessage: Integer);  
        method Salute(name: String);
    end;  
  
type 
    GreetProgram = class
    public  
        class method Main(args: array of String);  
    end;

implementation
// Property Setters/Getters Methods
method Greet.SetMessage(value: String);
begin
    fMessage := Capitalize(value);
end;
method Greet.SetName(value: String);
begin
    fName := Capitalize(value);
end;
// Constructor 
constructor Greet;
begin
    fMessage := "";
    fName := "";
    loopMessage := 0;    
end;  
// Overloaded Constructor 
constructor Greet(message: String; name: String; loopMessage: Integer);
begin
    fMessage := message;
    fName := name;
    loopMessage := loopMessage;
end;
// Method 1
method Greet.Capitalize(value: String): String;
begin
    // "if-then-else" statement 
    if value.Length >= 1 then 
    begin
        result := value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1);
    end
    else 
    begin
        result := "";
    end;
end;
// Method 2
method Greet.Salute;
begin  
    // "for" statement 
    for i: Integer := 1 to loopMessage step 1 do
    begin
        Console.WriteLine("{0} {1}!", fMessage, fName);
    end;
end;  
// Overloaded Method 2.1 
method Greet.Salute(message: String; name: String; loopMessage: Integer);
var
    i: Integer;
begin
    // "while" statement  
    i := 0;
    while i < loopMessage do 
    begin
        Console.WriteLine("{0} {1}!", Capitalize(message), Capitalize(name));
        i := i + 1;
    end;
end;
// Overloaded Method 2.2  
method Greet.Salute(name: String);
var    
    dtNow: DateTime;
begin
    // "switch/case" statement  
    dtNow := DateTime.Now;
    case dtNow.hour of
        6..11: fMessage := "good morning,";
        12..17: fMessage := "good afternoon,";
        18..22: fMessage := "good evening,";
        23,0..5: fMessage := "good night,";
        else fMessage := "huh?"; 
    end;
    Console.WriteLine("{0} {1}!", Capitalize(fMessage), Capitalize(name));
end;

// Console Program
class method GreetProgram.Main(args: array of String);  
var
    // Define object of type Greet    
    g: Greet;
begin  
    // Instantiate Greet. Call Constructor 
    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, "delphi Prism", g.LoopMessage);  
    // Call Overloaded Method 2.2      
    g.Salute("carlos");    
    // Stop and exit  
    Console.WriteLine("Press any key to exit...");  
    Console.Read();  
end;  
end.

And the Output is:





















Auto-Implemented Properties in Delphi Prism
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.

namespace;
interface
uses System;   

type Greet = class  
    // No explicit private field required
    // private fMessage: String;
    // Auto Implemented Property
    property Message: String;
    constructor();        
    method Salute();        
    method Salute(value: String);
end;  

type GreetProgram = class
    class method Main(args: array of String);  
end;

implementation
// Constructor 
constructor Greet();
begin
    Message := "";
end;  
method Greet.Salute();
begin  
    Console.WriteLine(Message[0].ToString().ToUpper() + Message.SubString(1, Message.Length - 1));
end;  
method Greet.Salute(value: String);
begin  
    Console.WriteLine(value[0].ToString().ToUpper() + value.SubString(1, value.Length - 1));
end; 

class method GreetProgram.Main(args: array of String);  
var g: Greet;
begin  
    g := new Greet();
    // Call Set Auto Implemented Property
    g.Message := "hello";  
    g.Salute;
    g.Message := "bye";
    // Call Get Auto Implemented Property
    g.Salute(g.Message);
end;  
end.


And the Output is: