Friday, August 27, 2010

OO Hello World - JavaFX



The Hello World version of the program in JavaFX!  

Better late than never!!! JavaFX was already included in the list of most active JVM and CLR languages around as well as its keywords in the keywords page of this blog. Now I'm adding here below the OO Hello World program that I will also add to the how many keywords and lines comparison posts :)

And of course I will include it in the newest series "Basics by Example" but later on :) 


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;
    init {
        name = "{name.substring(0,1).toUpperCase()}{name.substring(1, name.length())}";
    }
    function Salute(){
        println("Hello {name}!")
    }
}

// Greet the world!
var g = Greet{name: "world"};
g.Salute();

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

package greetprogram;
import javafx.*;

public class Greet {
    var name: String;
    init {
        this.name = "{name.substring(0,1).toUpperCase()}{name.substring(1, name.length())}";
    }
    public function Salute(){
        println("Hello {this.name}!")
    }
}

// Greet the world!
public function run() {
    var g = Greet{name: "world"};
    g.Salute();
}

The Program Output:









JavaFX Info:
“JavaFX apps developers use a statically typed, declarative language called JavaFX Script; Java code can be integrated into JavaFX programs. JavaFX is compiled to Java bytecode, so JavaFX applications run on any desktop and browser that runs the Java Runtime Environment (JRE) and on top of mobile phones running Java ME.” Taken from: (http://en.wikipedia.org/wiki/JavaFX)

Appeared:
2008
Current Version:
Developed by:
Sun Microsystems
Creator:
Chris Oliver
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:
“.fx”
Keywords:
70
Case Sensitive:
Yes
Free Version Available:
Yes
Open Source:
No
Standard:
?
Latest IDE Support:
NetBeans
Eclipse
IntelliJ IDEA
Language Reference:
Extra Info:



Wednesday, August 25, 2010

JScript.NET - Basics by Example



continue with the Basics by Example; today's version of the post written in JScript.NET 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
// JScript.NET Basics  
import System;

package JsGreetProgram {    
    // Greeting Class  
    public class Greet {    
        // Fields or Attributes  
        private var message:String;
        private var name:String;  
        private var loopMessage:int;  
        // Properties  
        public function get Message():String {  
            return this.message; 
        }  
        public function set Message(value:String) {
            this.message = this.Capitalize(value);
        }
        public function get Name():String {  
            return this.name; 
        }  
        public function set Name(value:String) {
            this.name = this.Capitalize(value);
        }  
        public function get LoopMessage():int {  
            return this.loopMessage; 
        }  
        public function set LoopMessage(value:int) {
            this.loopMessage = value;
        }  
        // Constructor  
        public function Greet() {  
            this.message = "";  
            this.name = "";  
            this.loopMessage = 0;  
        }  
        // Overloaded Constructor  
        public function Greet(message:String, name:String, loopMessage:int) {  
            this.message = this.Capitalize(message);  
            this.name = this.Capitalize(name);  
            this.loopMessage = loopMessage;  
        }   
        // Method 1  
        private function Capitalize(val:String):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 function Salute():void {  
            // "for" statement  
            for (var i:int = 0; i < this.loopMessage; i++) {  
            print(this.message + " " + this.name + "!");                
            }  
        }  
        // Overloaded Method 2.1  
        public function Salute(message:String, name:String, loopMessage:int):void {  
            // "while" statement  
            var i:int = 0;  
            while(i < loopMessage) {  
            print(this.Capitalize(message) + " " 
            + this.Capitalize(name) + "!");                
                i++;  
            }  
        }  
        // Overloaded Method 2.2  
        public function Salute(name:String):void {  
            // "switch/case" statement  
            var dtNow:DateTime = DateTime.Now;  
            switch (dtNow.Hour)
            {  
                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?";  
                    break;  
            }  
            print(this.Capitalize(this.message) + " " 
                + this.Capitalize(name) + "!");  
        }  
    }  
}
    
// Console Program  
function Main() {
 // Define object of type Greet   
 var g:JsGreetProgram.Greet;  
 // Instantiate Greet. Call Constructor  
 g = new JsGreetProgram.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, "jScript.NET", g.LoopMessage);  
 // Call Overloaded Method 2.2  
 g.Salute("carlos");  
   
 // Stop and exit  
 print("Press any key to exit...");  
 Console.Read();
};

Main();

Greetings Program - Minimal
// JScript.NET Basics  
import System;
// Greeting Class  
class Greet {    
 // Fields or Attributes  
 var message, 
  name, 
  loopMessage;  
 // Properties  
 function get Message() {  
  return message; 
 }  
 function set Message(value) {
  message = Capitalize(value);
 }
 function get Name() {  
  return name; 
 }  
 function set Name(value) {
  name = Capitalize(value);
 }
 function get LoopMessage() {  
  return loopMessage; 
 }  
 function set LoopMessage(value) {
  loopMessage = value;
 }        
 // Constructor  
 function Greet() {  
  message = "";  
  name = "";  
  loopMessage = 0;  
 }  
 // Overloaded Constructor  
 function Greet(message, name, loopMessage) {  
  this.message = Capitalize(message);  
  this.name = Capitalize(name);  
  this.loopMessage = loopMessage;  
 }   
 // Method 1  
 private function Capitalize(val) {  
  // "if-then-else" statement  
  if (val.Length >= 1) {  
   return val[0].ToString().ToUpper() 
     + val.Substring(1, val.Length - 1);      
  }  
  else  {  
   return "";  
  }  
 }  
 // Method 2  
 function Salute() {  
  // "for" statement  
  for (var i = 0; i < loopMessage; i++) {  
   print(message + " " + name + "!");                
  }  
 }  
 // Overloaded Method 2.1  
 function Salute(message, name, loopMessage) {  
  // "while" statement  
  var i = 0;  
  while(i < loopMessage) {  
   print(Capitalize(message) + " " 
    + Capitalize(name) + "!");                
   i++;  
  }  
 }  
 // Overloaded Method 2.2  
 function Salute(name) {  
  // "switch/case" statement  
  var dtNow = DateTime.Now;  
  switch (dtNow.Hour)
  {  
   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?";  
    break;  
  }  
  print(Capitalize(message) + " " 
    + Capitalize(name) + "!");  
 }  
}
   
// Console Program  
// Define object of type Greet and Instantiate Greet. Call Constructor  
var 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, "jScript.NET", g.LoopMessage);  
// Call Overloaded Method 2.2  
g.Salute("carlos");
  
// Stop and exit  
print("Press any key to exit...");  
Console.Read();


And the Output is:

Monday, August 23, 2010

C++/CLI - Basics by Example



continue with the Basics by Example; today's version of the post written in C++/CLI 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
// CppCli Basics
#include "stdafx.h"  
using namespace System;  
  
namespace CppCliGreetProgram {  

 private ref class Greet { 
  // Fields or Attributes
  private: String ^message;
  private: String ^name;
  private: int loopMessage; 
  // Properties
  public: property String^ Message {
   public: String^ get() { return this->message; }
   public: void set(String^ val) { this->message = val; }
  }
  public: property String^ Name {
   public: String^ get() { return this->name; }
   public: void set(String^ val) { this->name = val; }
  }
  public: property int LoopMessage {
   public: int get() { return this->loopMessage; }
   public: void set(int val) { this->loopMessage = val; }
  }
  // Constructor
  public: Greet() {
   this->message = "";
   this->name = "";
   this->loopMessage = 0;
  }
  // Overloaded Constructor
  public: Greet(String^ message, String^ name, int loopMessage) {
   this->message = this->Capitalize(message);
   this->name = this->Capitalize(name);
   this->loopMessage = loopMessage;
  }    
  // Method 1
  private: String^ Capitalize(String^ val) {
   // "if-then-else" statement
   if (val->Length >= 1) {
    return val[0].ToString()->ToUpper() + val->Substring(1, val->Length - 1);
   }
   else {
    return "";
   }
  }
 
  // Method 2
  public: void Salute() {
   // "for" statement
   for (int i = 0; i < this->loopMessage; i++) {
    Console::WriteLine("{0} {1}!", this->message, this->name);
   }
  }
  // Overloaded Method 2.1
  public: void Salute(String^ message, String^ name, int loopMessage) {
   // "while" statement
   int i = 0;
   while(i < loopMessage) {
    Console::WriteLine("{0} {1}!", this->Capitalize(message), this->Capitalize(name));
    i++;
   }
  }
  // Overloaded Method 2.2
  public: void Salute(String^ name) {
   // "switch/case" statement
   DateTime^ dtNow = DateTime::Now;
   switch (dtNow->Hour) {
    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?";
     break;
   }
   Console::WriteLine("{0} {1}!", this->Capitalize(this->message), this->Capitalize(name));
  }
 };
}

// Console Program
int main(array<System::String ^> ^args) {
 // Define object of type Greet 
    CppCliGreetProgram::Greet ^g;
    // Instantiate Greet. Call Constructor
    g = gcnew CppCliGreetProgram::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, "c++/CLI", g->LoopMessage);
    // Call Overloaded Method 2.2
    g->Salute("carlos");
            
    // Stop and exit
    Console::WriteLine(L"Press any key to exit...");
    Console::Read();
 return 0;
}

Greetings Program - Minimal
// CppCli Basics
#include "stdafx.h"  
using namespace System;  

ref class Greet {
 // Fields or Attributes
 String ^message;
 String ^name;
 int loopMessage; 
 // Properties
public: 
 property String^ Message {
  String^ get() { return message; }
  void set(String^ val) { message = val; }
 }
 property String^ Name {
  String^ get() { return name; }
  void set(String^ val) { name = val; }
 }
 property int LoopMessage {
  int get() { return loopMessage; }
  void set(int val) { loopMessage = val; }
 }
 // Constructor
 Greet() {
  message = "";
  name = "";
  loopMessage = 0;
 }
 // Overloaded Constructor
 Greet(String^ message, String^ name, int loopMessage) {
  this->message = Capitalize(message);
  this->name = Capitalize(name);
  this->loopMessage = loopMessage;
 }    
 // Method 1
private: 
 String^ Capitalize(String^ val) {
  // "if-then-else" statement
  if (val->Length >= 1) {
   return val[0].ToString()->ToUpper() + val->Substring(1, val->Length - 1);
  }
  else {
   return "";
  }
 }
 
 // Method 2
public: 
 void Salute() {
  // "for" statement
  for (int i = 0; i < loopMessage; i++) {
   Console::WriteLine("{0} {1}!", message, name);
  }
 }
 // Overloaded Method 2.1
 void Salute(String^ message, String^ name, int loopMessage) {
  // "while" statement
  int i = 0;
  while(i < loopMessage) {
   Console::WriteLine("{0} {1}!", Capitalize(message), Capitalize(name));
   i++;
  }
 }
 // Overloaded Method 2.2
 void Salute(String^ name) {
  // "switch/case" statement
  DateTime^ dtNow = DateTime::Now;
  switch (dtNow->Hour) {
   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?";
    break;
  }
  Console::WriteLine("{0} {1}!", Capitalize(message), Capitalize(name));
 }
};

// Console Program
int main(array<System::String ^> ^args) {
    // Define and Instantiate object of type Greet. Call Constructor
    Greet ^g = gcnew 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, "c++/CLI", g->LoopMessage);
    // Call Overloaded Method 2.2
    g->Salute("carlos");
            
    // Stop and exit
    Console::WriteLine(L"Press any key to exit...");
    Console::Read();
}

And the Output is:


















Auto-Implemented Properties in C++/CLI

As with C# and VB.NET, C++/CLI also has built in syntax to create auto-implemented properties. 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 compiler will automatically create a private field to store the property variable in addition to creating the associated get and set procedures.

// That means that we can omit the Fields/Attributes declaration
   //and go directly to the properties
 
private:
 // Fields or Attributes
 /*String ^message;
 String ^name;
 int loopMessage;*/ 
public: 
 // Auto-Implemented Properties
 property String^ Message;
 property String^ Name;
 property int LoopMessage;  
 
// then, when ever you want to use them you get and set their value using the  properties only (just like with C#. The attributes are marked as: 
// ".field private string " (via reflector) and you cannot access it directly) 
// Let's see an example in our constructor
        // Constructor
 public: Greet() {
  this->Message = "";
  this->Name = "";
  this->LoopMessage = 0;
 }



Initializer Lists in Constructors in C++/CLI

A constructor initializer list is a syntax construct that allows the user to initialize attributes/fields of a class, as well as parent class fields, of a class without using assignments. Let's see an example:

ref class Greet {
 // Fields or Attributes
 String ^message;
 String ^name;
 int loopMessage; 
public: 
 // then, instead of the using assignments within the Constructor
 // public: Greet() {
 //  this->Message = "";
 //  this->Name = "";
 //  this->LoopMessage = 0;
 // }
 // we use 
 // Constructor with Initialization Lists
 Greet() : message(""), name(""), loopMessage(0) { 
 }
 // Overloaded Constructor with Initialization Lists
 // you can also call other methods during the initialization (ex: Capitalize)
 Greet(String^ message, String^ name, int loopMessage) : message(Capitalize(message)), name(Capitalize(name)), loopMessage(loopMessage) {
 }
 // Method 1
private: 
 String^ Capitalize(String^ val) {}
}; 

 // However, if you decide to use Auto-Implemented Properties, then you do not have attribute fields anymore (at least, not that you can directly access), so you might think that you can initialize the property. Right?
// Well, not really. If you do that as shown here below, you will get a compiler error.

ref class Greet {
 // Fields or Attributes
 String ^message;
 String ^name;
 int loopMessage; 
 // Properties
public: 
 property int AutoImplementedPro;
 // Constructor with Initialization Lists
 Greet() : message(""), name(""), loopMessage(0), AutoImplementedPro(1) {}

// You will get:
// Error 1 error C3137: 'AutoImplementedPro' : a property cannot be initialized I:\Dev\PRJ\CppCLI_Basics_Features\CppCLI_Basics_Features\CppCLI_Basics_Features.cpp 28 1 CppCLI_Basics_Features

Saturday, August 21, 2010

VB.NET - Basics by Example



continue with the Basics by Example; today's version of the post written in VB.NET 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
' VB.NET Basics
Imports System
Namespace VBGreetProgram
    Friend Class Greet
        ' Fields or Attributes
        Private _message As String
        Private _name As String
        Private _loopMessage As Integer
        ' Properties
        Public Property Message() As String
            Get
                Return Me._message
            End Get
            Set(ByVal value As String)
                Me._message = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return Me._name
            End Get
            Set(ByVal value As String)
                Me._name = value
            End Set
        End Property
        Public Property LoopMessage() As String
            Get
                Return Me._loopMessage
            End Get
            Set(ByVal value As String)
                Me._loopMessage = value
            End Set
        End Property
        ' Constructors
        Public Sub New()
            Me._message = ""
            Me._name = ""
            Me._loopMessage = 0
        End Sub
        ' Overloaded Constructor
        Public Sub New(ByVal message As Integer, ByVal name As String, ByVal loopMessage As Integer)
            Me._message = Me.Capitalize(message)
            Me._name = Me.Capitalize(name)
            Me._loopMessage = loopMessage
        End Sub
        ' Method 1
        Private Function Capitalize(ByVal val As String) As String
            ' "if-then-else" statement
            If Len(val) >= 1 Then
                Return UCase(val(0)) & val.Substring(1, Len(val) - 1)
            Else
                Return ""
            End If
        End Function
        ' Method 2
        Public Sub Salute()
            ' "for" statement
            For i As Integer = 0 To Me._loopMessage
                Console.WriteLine("{0} {1}!", Me._message, Me._name)
            Next
        End Sub
        ' Overloaded Method 2.1
        Public Sub Salute(ByVal message As String, ByVal name As String, ByVal loopMessage As Integer)
            ' "while" statement
            Dim i As Integer = 0
            While i < loopMessage
                Console.WriteLine("{0} {1}!", Me.Capitalize(message), Me.Capitalize(name))
                i = i + 1
            End While
        End Sub
        ' Overloaded Method 2.2
        Public Sub Salute(ByVal name As String)
            ' "switch/case" statement
            Dim dtNow As DateTime = DateTime.Now()
            Select Case dtNow.Hour
                Case 6 To 11
                    Me._message = "good morning,"
                Case 12 To 17
                    Me._message = "good afternoon,"
                Case 18 To 22
                    Me._message = "good evening,"
                Case 23, 0 To 5
                    Me._message = "good night,"
                Case Else
                    Me._message = "huh?"
            End Select
            Console.WriteLine("{0} {1}!", Me.Capitalize(Me._message), Me.Capitalize(name))
        End Sub
    End Class

    ' Console Program
    Friend Module Program
        Public Sub Main()
            ' Define object of type Greet 
            Dim g As Greet
            ' 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, "VB.net", g.LoopMessage)
            ' Call Overloaded Method 2.2
            g.Salute("carlos")

            ' Stop and exit
            Console.WriteLine("Press any key to exit...")
            Console.Read()
        End Sub
    End Module
End Namespace

Greetings Program - Minimal
' VB.NET Basics
Imports System

Friend Class Greet
    ' Fields or Attributes
    Private _message As String, _name As String
    Private _loopMessage As Integer
    ' Properties
    Property Message() As String
        Get
            Return _message
        End Get
        Set(ByVal value As String)
            _message = value
        End Set
    End Property
    Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property
    Property LoopMessage() As String
        Get
            Return _loopMessage
        End Get
        Set(ByVal value As String)
            _loopMessage = value
        End Set
    End Property
    ' Constructors
    Sub New()
        _message = ""
        _name = ""
        _loopMessage = 0
    End Sub
    ' Overloaded Constructor
    Sub New(ByVal message As Integer, ByVal name As String, ByVal loopMessage As Integer)
        _message = Capitalize(message)
        _name = Capitalize(name)
        _loopMessage = loopMessage
    End Sub
    ' Method 1
    Private Function Capitalize(ByVal val As String) As String
        ' "if-then-else" statement
        If Len(val) >= 1 Then
            Return UCase(val(0)) & val.Substring(1, Len(val) - 1)
        Else
            Return ""
        End If
    End Function
    ' Method 2
    Sub Salute()
        ' "for" statement
        For i As Integer = 0 To _loopMessage
            Console.WriteLine("{0} {1}!", _message, _name)
        Next
    End Sub
    ' Overloaded Method 2.1
    Sub Salute(ByVal message As String, ByVal name As String, ByVal loopMessage As Integer)
        ' "while" statement
        Dim i As Integer = 0
        While i < loopMessage
            Console.WriteLine("{0} {1}!", Capitalize(message), Capitalize(name))
            i = i + 1
        End While
    End Sub
    ' Overloaded Method 2.2
    Sub Salute(ByVal name As String)
        ' "switch/case" statement
        Dim dtNow As DateTime = DateTime.Now()
        Select Case dtNow.Hour
            Case 6 To 11
                _message = "good morning,"
            Case 12 To 17
                _message = "good afternoon,"
            Case 18 To 22
                _message = "good evening,"
            Case 23, 0 To 5
                _message = "good night,"
            Case Else
                _message = "huh?"
        End Select
        Console.WriteLine("{0} {1}!", Capitalize(_message), Capitalize(name))
    End Sub
End Class

' Console Program
Friend Module Program
    Sub Main()
        ' Define object of type Greet and Instantiate Greet. Call Constructor        
        Dim g As 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, "VB.net", g.LoopMessage)
        ' Call Overloaded Method 2.2
        g.Salute("carlos")

        ' Stop and exit
        Console.WriteLine("Press any key to exit...")
        Console.Read()
    End Sub
End Module

And the Output is:


Auto-Implemented Properties in VB.NET

Auto-implemented properties enable you to quickly specify a property of a class without having to write code to Get and Set the property. When you write code for an auto-implemented property, the Visual Basic compiler automatically creates a private field to store the property variable in addition to creating the associated Get and Set procedures. Taken from: http://msdn.microsoft.com/en-us/library/dd293589.aspx


' That means that we can omit the Fields/Attributes declaration
   'and go directly to the properties
    ' Fields or Attributes
    ' Private _message As String
    ' Private _name As String
    ' Private _loopMessage As Integer
    ' Auto-Implemented Properties
    Property Message() As String
    Property Name() As String
    Property LoopMessage() As String

' 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 constructor
        ' Constructor
        Sub New()
        ' accessing through the Property
        Message = ""
        Name = ""
        LoopMessage = 0
        ' or through the auto created fields/attributes (even if you cannot see them using "Me.")        
        _Message = ""
        _Name = ""
        _LoopMessage = 0
    End Sub  

Wednesday, August 4, 2010

Zonnon - Basics by Example



The second of a series of basics statements and constructs of .NET/JVM Programming Language is here! This time the language chosen was Zonnon.
If you want to know more details about this interesting Programming Language you can find it here: http://carlosqt.blogspot.com/2010/06/oo-hello-world-zonnon.html

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 do not know the theory part, then 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
(* Zonnon Basics *)
module Main;  
import System;

(* Greeting Class and Contructor(No Args) *)
type {ref, public} Greet = object()
 (* Fields or Attributes *)
 var {private}
  message: string;
  name: string;
  loopMessage: integer;

 (* Getters and Setters - No "Working" Properties Support in version 1.2.7? *)
 procedure {public} GetMessage: string;
 begin  
  return self.message;
 end GetMessage;

 procedure {public} SetMessage(val: string);
 begin
  self.message := val;
 end SetMessage;

 procedure {public} GetName: string;
 begin  
  return self.name;
 end GetName;

 procedure {public} SetName(val: string);
 begin
  self.name := val;
 end SetName;

 procedure {public} GetLoopMessage: integer;
 begin  
  return self.loopMessage;
 end GetLoopMessage;

 procedure {public} SetLoopMessage(val: integer);
 begin
  self.loopMessage := val;
 end SetLoopMessage;

 (* Overloaded Constructor *)
 (* No Overloaded Constructors Support in version 1.2.7 *)

 (* Method 1 *)
 procedure {private} Capitalize(val: string): string;  
 begin
  if val.Length >= 1 then 
   return val.Substring(0, 1).ToUpper() + val.Substring(1, val.Length - 1);
  else
   return "";
  end;
 end Capitalize;  

 (* Method 2 *)
 procedure {public} Salute();  
 var i: integer;
 begin
  (* "for" statement *)
  for i := 0 to self.loopMessage do;
   writeln(self.Capitalize(self.message) + " " + self.Capitalize(self.name) + "!");
  end;   
 end Salute; 

 (* Overloaded Methods *)  
 (* No Overloaded Methods Support in version 1.2.7 *)

 (* Method 2.1 *)
 procedure {public} Salute21(message: string; name: string; loopMessage: integer);
 var i: integer;
 begin
  i := 0;
  (* "while" statement *)
  while i < loopMessage do;
   writeln(self.Capitalize(message) + " " + self.Capitalize(name) + "!");
   i := i + 1;
  end;
 end Salute21;

 (* Method 2.2 *)
 procedure {public} Salute22(name: string);
 var 
  dtNow: System.DateTime;
  time: integer;
 begin
  dtNow := System.DateTime.Now;
  time := dtNow.Hour;
  (* "switch/case" statement *)
  case time of  
      6..11 : 
    self.message := "good morning,"; 
   | 12..17 : 
    self.message := "good afternoon,"; 
   | 18..22 :
    self.message := "good evening,"; 
   (*| 23, 0..5 : this doesnt work in 1.2.8 use the following instead*)
   | 23,0,1,2,3,4,5 :
    self.message := "good night,"; 
   else   
          self.message := "huh?";
  end;   
  writeln(self.Capitalize(self.message) + " " + self.Capitalize(name) + "!");
 end Salute22;

(* Contructor | Initializer *)
begin  
 self.message := "";
 self.name := "";
 self.loopMessage := 0;
end Greet;  

(* Console Program *)
var   
 (* Define object of type Greet *)
 g: Greet;
begin   
 (* Instantiate Greet. Call Constructor *)
  g := new Greet();   
 (* Call Setters *)
 g.SetMessage("hello");
 g.SetName("world");
 g.SetLoopMessage(5);
 (* Call Method 2 *)
 g.Salute();
 (* Call Method 2.1 and Getters *)
 g.Salute21(g.GetMessage(), "zonnon", g.GetLoopMessage());
 (* Call Method 2.2 *)
 g.Salute22("carlos");
            
 (* Stop and exit *)
 writeln("Press any key to exit...");
 readln();
end Main.

Greetings Program - Minimal
(* Zonnon Basics *)
module Main;  
import System;

(* Greeting Class and Contructor(No Args) *)
type {ref} Greet = object
 (* Fields or Attributes *)
 var 
  message, name: string;
  loopMessage: integer;

 (* Getters and Setters - No "Working" Properties Support in version 1.2.7? *)
 procedure {public} GetMessage: string;
 begin  
  return message;
 end GetMessage;

 procedure {public} SetMessage(val: string);
 begin
  message := val;
 end SetMessage;

 procedure {public} GetName: string;
 begin  
  return name;
 end GetName;

 procedure {public} SetName(val: string);
 begin
  name := val;
 end SetName;

 procedure {public} GetLoopMessage: integer;
 begin  
  return loopMessage;
 end GetLoopMessage;

 procedure {public} SetLoopMessage(val: integer);
 begin
  loopMessage := val;
 end SetLoopMessage;

 (* Overloaded Constructor *)
 (* No Overloaded Constructors Support in version 1.2.7 *)

 (* Method 1 *)
 procedure Capitalize(val: string): string;  
 begin
  if val.Length >= 1 then 
   return val.Substring(0, 1).ToUpper() + val.Substring(1, val.Length - 1);
  else
   return "";
  end;
 end Capitalize;  

 (* Method 2 *)
 procedure {public} Salute;  
 var i: integer;
 begin
  (* "for" statement *)
  for i := 0 to loopMessage do;
   writeln(Capitalize(message) + " " + Capitalize(name) + "!");
  end;   
 end Salute; 

 (* Overloaded Methods *)  
 (* No Overloaded Methods Support in version 1.2.7 *)

 (* Method 2.1 *)
 procedure {public} Salute21(message: string; name: string; loopMessage: integer);
 var i: integer;
 begin
  i := 0;
  (* "while" statement *)
  while i < loopMessage do;
   writeln(Capitalize(message) + " " + Capitalize(name) + "!");
   i := i + 1;
  end;
 end Salute21;

 (* Method 2.2 *)
 procedure {public} Salute22(name: string);
 var 
  dtNow: System.DateTime;
  time: integer;
 begin
  dtNow := System.DateTime.Now;
  time := dtNow.Hour;
  (* "switch/case" statement *)
  case time of  
       6..11 : 
    message := "good morning,"; 
   | 12..17 : 
    message := "good afternoon,"; 
   | 18..22 :
    message := "good evening,"; 
    (*| 23, 0..5 : this doesnt work in 1.2.8 use the following instead*)
   | 23,0,1,2,3,4,5 :
    message := "good night,"; 
   else   
          message := "huh?";
  end;   
  writeln(Capitalize(message) + " " + Capitalize(name) + "!");
 end Salute22;

(* Contructor | Initializer *)
begin  
 message := "";
 name := "";
 loopMessage := 0;
end Greet;  

(* Console Program *)
var   
 (* Define object of type Greet *)
 g: Greet;
begin   
 (* Instantiate Greet. Call Constructor *)
  g := new Greet();   
 (* Call Setters *)
 g.SetMessage("hello");
 g.SetName("world");
 g.SetLoopMessage(5);
 (* Call Method 2 *)
 g.Salute();
 (* Call Method 2.1 and Getters *)
 g.Salute21(g.GetMessage(), "zonnon", g.GetLoopMessage());
 (* Call Method 2.2 *)
 g.Salute22("carlos");
            
 (* Stop and exit *)
 writeln("Press any key to exit...");
 readln();
end Main.

And the Output is:


I decided to take out some extra comments I added to the code to explain to myself the missing features of Zonnon (overloaded constructors, overloaded methods and properties), but added them with the code examples in the following code section:

(* Properties: *)
 (* It looks like there is some Properties support, but then you need to define your attributes outside 
  the type, into a definition object and then inherit from it in the type = object definition.
  Then you can start creating get and set procedures that also implement each of the attributes defined
  in the definition object... which in fact looks like a normal procedure with more typing. Now, the 
  thing is that I was not able to make it work... I'm still doing some research on that, but for the 
  time being I decided to use Getters and Setters, BUT, definitively, I will show you here below the syntax 
  for those = Get and Set Properties:*)

  (* Step 1: Create a Definition *)
  definition GreetAttributes;
  var {private}
   message, name: string;    
   loopMessage: integer;
  end GreetAttributes;
  
  (* Step 2: Define your Type and Implement from GreetDataMembers Definition *)
  type {ref, public} Greet = object() implements GreetDataMembers

  (* Step 3: Do not define Attributes on the Type (we will use Definition's Attributes) *)  
  
  (* Step 4: Create a Get Property for message attribute which implements attribute in definition *)
  procedure {get, public} message(): string implements GreetDataMembers.message;
  begin
   return self.message;
  end message;

  (* Step 5: Create a Set Property for message attribute which implements attribute in definition *)
  procedure {set, public} message(val: string) implements GreetDataMembers.message;
  begin
   self.message := val;
  end message;

(* Overloaded Constructors: *)
(* Overloaded Constructors are not supported in version 1.2.7 *)
 (* If you need parameters in the constructor
    You need to define the Greet Type as follows: *)
  
  type {ref, public} Greet = object(message: string; name: string; loopMessage: integer) 

  (*  And then update the Constructor | Initialize section of the Type to initialize the values: *)

  begin  
   self.message := message;
   self.name := name;
   self.loopMessage := 0;
  end Greet;  

(* Overloaded Methods: *)
(* Overloaded Methods are not supported in version 1.2.7 *)
 (* If you try to declare the same method name even if the signature differs it gives the following error message 

  "[2015] Duplicate declaration with the name 'Salute' in the current scope" BEEP!

  So, I'm calling the next methods Salute21 and Salute22 (cuz they suppose to be overloading Method 2
 *)

Monday, August 2, 2010

C# - Basics by Example



The first of a series of basics statements and constructs of .NET/JVM Programming Language is here! No boring step by step explanations... just code!
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 do not know the theory part, then 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 

Today's version of the Greetings Program is written in C# Enjoy!

Greetings Program - Verbose
// C# Basics
namespace CsGreetProgram
{
    using System;
    // Greeting Class
    internal class Greet  
    {  
        // Fields or Attributes
        private string message;
        private string name;
        private int loopMessage;
        // Properties
        public string Message
        {
            get { return this.message; }
            set { this.message = this.Capitalize(value); }
        }
        public string Name
        {
            get { return this.name; }
            set { this.name = this.Capitalize(value); }
        }
        public int LoopMessage
        {
            get { return this.loopMessage; }
            set { this.loopMessage = value; }
        }
        // Constructor
        public Greet() 
        {
            this.message = "";
            this.name = "";
            this.loopMessage = 0;
        }
        // Overloaded Constructor
        public Greet(string message, string name, int loopMessage)
        {
            this.message = this.Capitalize(message);
            this.name = this.Capitalize(name);
            this.loopMessage = loopMessage;
        } 
        // Method 1
        private string Capitalize(string val)
        {
            // "if-then-else" statement
            if (val.Length >= 1)
            {
                return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1);
            }
            else
            {
                return "";
            }
        }
        // Method 2
        public void Salute()  
        {
            // "for" statement
            for (int i = 0; i < this.loopMessage; i++)
            {
                Console.WriteLine("{0} {1}!", this.message, this.name);
            }
        }
        // Overloaded Method 2.1
        public void Salute(string message, string name, int loopMessage)
        {
            // "while" statement
            int i = 0;
            while(i < loopMessage)
            {
                Console.WriteLine("{0} {1}!", this.Capitalize(message), 
                                 this.Capitalize(name));
                i++;
            }
        }
        // Overloaded Method 2.2
        public void Salute(string name)
        {
            // "switch/case" statement
            DateTime dtNow = DateTime.Now;
            switch (dtNow.Hour)
            {
                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?";
                    break;
            }
            Console.WriteLine("{0} {1}!", this.Capitalize(this.message), 
                             this.Capitalize(name));
        }
    }
  
    // Console Program
    internal class Program  
    {                  
        public static void Main(string[] args)  
        {            
            // Define object of type Greet 
            Greet g;
            // 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, "c#", g.LoopMessage);
            // Call Overloaded Method 2.2
            g.Salute("carlos");
            
            // Stop and exit
            Console.WriteLine("Press any key to exit...");
            Console.Read();
        }          
    }
}

Greetings Program - Minimal
// C# Basics
using System;
// Greeting Class
class Greet
{
    // Fields or Attributes
    string message, name;
    int loopMessage;
    // Properties
    public string Message
    {
        get { return message; }
        set { message = Capitalize(value); }
    }
    public string Name
    {
        get { return name; }
        set { name = Capitalize(value); }
    }
    public int LoopMessage
    {
        get { return loopMessage; }
        set { loopMessage = value; }
    }
    // Constructor
    public Greet()
    {
        message = "";
        name = "";
        loopMessage = 0;
    }
    // Overloaded Constructor
    public Greet(string message, string name, int loopMessage)
    {
        this.message = Capitalize(message);
        this.name = Capitalize(name);
        this.loopMessage = loopMessage;
    }
    // Method 1
    string Capitalize(string val)
    {
        // "if-then-else" statement
        if (val.Length >= 1)        
            return val[0].ToString().ToUpper() + val.Substring(1, val.Length - 1);
        else
            return "";
    }
    // Method 2
    public void Salute()
    {
        // "for" statement
        for (int i = 0; i < loopMessage; i++)
            Console.WriteLine("{0} {1}!", message, name);
    }
    // Overloaded Method 2.1
    public void Salute(string message, string name, int loopMessage)
    {
        // "while" statement
        int i = 0;
        while (i < loopMessage)
        {
            Console.WriteLine("{0} {1}!", Capitalize(message), 
                             Capitalize(name));
            i++;
        }
    }
    // Overloaded Method 2.2
    public void Salute(string name)
    {
        // "switch/case" statement
        DateTime dtNow = DateTime.Now;
        switch (dtNow.Hour)
        {
            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?";
                break;
        }
        Console.WriteLine("{0} {1}!", Capitalize(message), 
                         Capitalize(name));
    }
}

// Console Program
class Program
{
    static void Main(string[] args)
    {
        // Define and Instantiate object of type Greet. Call Constructor
        Greet 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, "c#", g.LoopMessage);
        // Call Overloaded Method 2.2
        g.Salute("carlos");        

        // Stop and exit
        Console.WriteLine("Press any key to exit...");
        Console.Read();
    }
}

And the Output is:

















Auto-Implemented Properties in C#

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. They also enable client code to create objects. When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors. Taken from: http://msdn.microsoft.com/en-us/library/bb384054.aspx


/* That means that we can omit the Fields/Attributes declaration
   and go directly to the properties  */
        // private string message;
        // private string name;
        // private int loopMessage;
        // Auto-Implemented Properties
        public string Message
        {
            get; set;
        }
        public string Name
        {
            get; set;
        }
        public int LoopMessage
        {
            get; set;
        }

/* then, when ever you want to use them you get and set their value using the properties only/ Let's see an example in our constructor */
        // Constructor
        public Greet()
        {
        // error: Greet does not contain a definition for message/name/loopMessage
         // this.message = "";  
         // this.name = "";
         // this.loopMessage = 0;
         // We need to use the properties: 
            this.Message = "";  
            this.Name = "";
            this.LoopMessage = 0;
        }

Sunday, August 1, 2010

New Series - Language's Basics by Example



I'm going to start working on a new posts series today. This time, I will extend some previous code, to show you some basic features of each of the 20 languages I have been using so far. If you haven't been following this blog, then I will remind you which those languages are:

Languages targeting the .NET/CLR runtime:
C#, VB.NET, C++/CLI, F#, Boo, Phalanger, IronPython, IronRuby, Delphi Prism, Zonnon, Nemerle, Cobra, JScript.NET

Languages targeting the Java/JVM runtime:
Groovy, Java, Jython, JRuby, Fantom, Scala, JavaFX

Now, about the content, I will show you an extended version of the Greetings Program implementing the following Language Basics statements and constructs features by using a Console Application. I will be using again 2 versions of the same program: Verbose and Minimal, so we can keep showing some numbers just for the fun.

The Program's Structure will be as follows:

// Language Basics

    // Greet Class
        // Fields or Attributes
        // Properties
        // Constructor
        // Overloaded Constructor
        // Method 1
            // "if-then-else" statement
        // Method 2
            // "for" statement
        // Overloaded Method 2.1
            // "while" statement
        // Overloaded Method 2.2
            // "switch/case" statement

    // Console Program
        // Define object of type Greet 
        // Instantiate Greet. Call Constructor
        // Call Set Properties
        // Call Method 2
        // Call Method 2.1 and Get Properties
        // Call Method 2.2
        // Stop and exit - Read Input From Console

And here below some definitions of each of the statements and constructs used on the upcoming posts:

Class
"In object-oriented programming, a class is a construct that is used as a blueprint (or template) to create objects of that class. This blueprint describes the state and behavior that the objects of the class all share. An object of a given class is called an instance of the class. The class that contains (and was used to create) that instance can be considered as the type of that object, e.g. an object instance of the "Fruit" class would be of the type "Fruit".

It encapsulates state through data placeholders called attributes (or member variables or instance variables); it encapsulates behavior through reusable sections of code called methods." Taken from: http://en.wikipedia.org/wiki/Class_(computer_science)

Constructor
"In object-oriented programming, a constructor (sometimes shortened to ctor) in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created." Taken from: http://en.wikipedia.org/wiki/Constructor_(computer_science)

Attributes
"In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such." Taken from: http://en.wikipedia.org/wiki/Attribute_(computing)

Methods
"In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method). Like a subroutine in procedural programming languages, a method usually consists of a sequence of programming statements to perform an action, a set of input parameters to customize those actions, and possibly an output value (called the return value) of some kind. Methods provide a mechanism for accessing and manipulating the encapsulated data stored in an object." Taken from: http://en.wikipedia.org/wiki/Method_(computer_science)


Method and Constructor Overloading
"Function overloading or method overloading is a feature found in various programming languages such as Ada, C#, C++, D and Java that allows the creation of several methods with the same name which differ from each other in terms of the type of the input and the type of the output of the function." Taken from: http://en.wikipedia.org/wiki/Function_overloading

Conditional Statements (if-else-then)
"In computer science, conditional statements, conditional expressions and conditional constructs are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition." Taken from: http://en.wikipedia.org/wiki/If_statement

Conditional Statements (Case and Switch)
"Switch statements (in some languages, case statements) compare a given value with specified constants and take action according to the first constant to match." Taken from: http://en.wikipedia.org/wiki/If_statement

"Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit), offering the potential of faster execution through compiler optimization." Taken from: http://en.wikipedia.org/wiki/Switch_statement

Control Flow Statement (For loop)
"In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement.

Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable. This allows the body of the for loop (the code that is being repeatedly executed) to know about the sequencing of each iteration. For loops are also typically used when the number of iterations is known before entering the loop." Taken from: http://en.wikipedia.org/wiki/For_statement

Control Flow Statement (While loop)
"In most computer programming languages, a while loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop." Taken from: http://en.wikipedia.org/wiki/While_loop 

Console Application
"A console application is a computer program designed to be used via a text-only computer interface, such as a text terminal" Taken from: http://en.wikipedia.org/wiki/Console_application


So, what do you think? interested? :D
I will start with C# then Zonnon because those are the ones I was using when I got the idea of working on this new series.

stay tuned!
bytes!