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:














