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:

No comments:
Post a Comment