Wednesday, July 29, 2009

C# UML Class Diagram




Click to enlarge...


Shape Class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapeInheritance
{
public abstract class Shape //enforce that abstract MUST BE OVERRIDEN
{
//fields
protected double xPos; //'protect' modifier used because we want all
protected double yPos; //subclasses will generally inherit those fields

//constructors
public Shape(double _xPos, double _yPos)
{
this.xPos = _xPos;
this.yPos = _yPos;
}

//methods
public abstract double Area(); //enforce that Area HAS TO BE OVERRIDEN BY SUB-CLASSES

public override string ToString()
{
return "Position : (" + xPos.ToString() + ", " + yPos.ToString() + ")\n";
}
}
}


Circle class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapeInheritance
{
public class Circle : Shape
{
//fields
private double radius;

//properties
public double Radius
{
get { return radius; }
set { radius = value; }
}

//constructors
public Circle() : base(0,0)
{
}

public Circle(double _xPos, double _yPos) : base(_xPos , _yPos)
{
}

public Circle(double _xPos, double _yPos, double _radius) : base(_xPos, _yPos)
{
this.radius = _radius;
}

//methods
public override double Area()
{
return Math.PI * Math.Pow(Radius, 2);
}

public override string ToString()
{
return base.ToString() + "\nradius : " + radius + "\nArea : " + Area().ToString("N2");
}
}
}


Cylinder class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShapeInheritance
{
public class Cylinder : Circle
{
//fields
private double height;

//properties
public double Height
{
get { return height; }
set { height = value; }
}

//constructors
public Cylinder() : base(0,0)
{
}

public Cylinder(double _xPos, double _yPos, double _radius, double _height) : base(_xPos, _yPos, _radius)
{
height = _height;
}

//methods
public override double Area()
{
return (2 * base.Area()) + (2 * Math.PI * Radius * height);
}

public double Volume()
{
return Math.PI * Math.Pow(Radius,2) * height;
}

public override string ToString()
{
return base.ToString() + "\nHeight : " + height + "\nVolume : " + Volume().ToString("N2");
}

}
}


Form coding:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShapeInheritance
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCircle_Click(object sender, EventArgs e)
{
Circle circle = new Circle(0, 0, 10);
lblText.Text = circle.ToString();
}

private void btnCylinder_Click(object sender, EventArgs e)
{
Cylinder cylinder = new Cylinder(0, 0, 10,10);
lblText.Text = cylinder.ToString();
}
}
}


Note:


1. Chain constructors are not used. Sub-constructors are calling the base constructor only.


2. Rectangle class not implemented.

Tuesday, July 28, 2009

Demonstrating Inheritance in C# - The Animal Class

The Animal class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceAnimals
{
public class Animal
{
//fields
private bool legs;
private bool wings;

//properties
public bool Legs
{
get { return legs; }
set { legs = value; }
}
public bool Wings
{
get { return wings; }
set { wings = value; }
}

//constructors

public Animal(bool _legs, bool _wings)
{
this.legs = _legs;
this.wings = _wings;
}

//methods
public virtual string eat()
{
return " unknown";
}

public virtual string hair()
{
return " unknown";
}

public virtual string sound()
{
return " unknown";
}

public virtual string movement()
{
return " unknown";
}

}
}


The Bat class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceAnimals
{
public class Bat : Animal
{
//fields

//properties

//constructors
public Bat():base(true,true)
{
}


public override string eat()
{
return "fruit and insects";
}

public override string hair()
{
return "feathers";
}


public override string movement()
{
return "unknown";
}

public override string sound()
{
return "unknown";
}

public string take_off()
{
return "launches from the tree";
}

public string land()
{
return "hangs on a rafter";
}


}
}


The Hawk class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceAnimals
{
public class Hawk : Animal
{
//fields

//properties

//constructors
public Hawk():base(true,true)
{

}

public override string eat()
{
return "small animals";
}

public override string hair()
{
return "feathers";
}

public override string sound()
{
return "Screeches";
}

public override string movement()
{
return "unknown";
}

public string take_off()
{
return "glides";
}

public string land()
{
return "perches on a tree top";
}


}
}


The Monkey class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceAnimals
{
public class Monkey : Animal
{
//fields

//properties

//constructors
public Monkey() : base(true, false)
{

}

public override string eat()
{
return "fruit";
}

public override string hair()
{
return "fur";
}

public override string sound()
{
return "Chatters";
}

public override string movement()
{
return "jumps";
}
}
}


The Snake class:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InheritanceAnimals
{
public class Snake : Animal
{
//fields

//properties

//constructors
public Snake(): base(false,false)
{
}

public override string eat()
{
return "rats";
}

public override string hair()
{
return "none";
}

public override string sound()
{
return "Hisses";
}

public override string movement()
{
return "slithers";
}

}
}


And finally, the GUI form sourcecode:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InheritanceAnimals
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnShow_Click(object sender, EventArgs e)
{
Snake snake = new Snake();
Monkey monkey = new Monkey();
Bat bat = new Bat();
Hawk hawk = new Hawk();

string myresults = "";

myresults = "Snake (" + snake.Legs + " " + snake.Wings + ") eats " + snake.eat() + ", hair = " + snake.hair() + ", sound = " + snake.sound() + ", moves = " + snake.movement() +"\n\n";
myresults += "Monkey (" + monkey.Legs + " " + monkey.Wings + ") eats " + monkey.eat() + ", hair = " + monkey.hair() + ", sound = " + monkey.sound() + ", moves = " + monkey.movement() + "\n\n";
myresults += "Bat (" + bat.Legs + " " + bat.Wings + ") eats " + bat.eat() + ", hair = " + bat.hair() + ", sound = " + bat.sound() + ", moves = " + bat.movement() + ", takes off = " + bat.take_off() + ", lands = " + bat.land() + "\n\n";
myresults += "Hawk (" + hawk.Legs + " " + hawk.Wings + ") eats " + hawk.eat() + ", hair = " + hawk.hair() + ", sound = " + hawk.sound() + ", moves = " + hawk.movement() + ", takes off = " + hawk.take_off() + ", lands = " + hawk.land() + "\n\n";
lblResult.Text = myresults;
}

}
}

Wednesday, July 22, 2009

Understanding the concept of 1 to 1 relationship, and applying it to C#

Given the constraint that one object (Player) cannot exist without another object (Play Game), a one-to-one relationship representation in C# would mean that only one game per player, and without any game, we cannot have any player (makes perfect sense).

Tutorial to set up the One-to-One relationship
1) Set up the one-to-one relationship
Create your two independent forms (Form1 and Form2 by default) by adding them to your project in Visual Studio.
Now, you need to create a dependency between those two forms. I am using forms Game (Form1) and Player (Form2).

In Player (Form2) form where you would usually declare your fields,


//fields
//create the dependency to Game Form
private Game myGameForm;


Apply the same logic to the Form1 class, or the Game class.


//fields
//create the dependency to Play Form
private Play myPlayForm;


2) Create the dependency that Form2 cannot exist without Form1.

We want the Play object to exist only if we have a Game.


//fields
//create the dependency to Play Form
private Play myPlayForm;

public Game()
{
myPlayForm = new Play(this);//create a Play form if the game exists
InitializeComponent();
}



Obviously, we cannot have a player if we do not have the game. To enforce this constraint, we make use of the constructor for the Form2, so that Form2 (or Game), will only be created if a Form1 object (Game) is not passed as an argument to the constructor.


//fields
//create the dependency to Game Form
private Game myGameForm;

public Play(Game gameform)
{
InitializeComponent();
myGameForm = gameform;//assign the object myGameForm to the argument gameform
}


So what did we exactly do? We said that if a Game exists, then the player also exists.

3) Show the second Form2 (Player) when the button on Form1 (Game) is clicked.


private void btnPlay_Click(object sender, EventArgs e)
{
myPlayForm.Show();
}


Run your code. On clicking the button from Form1 (Game), you will see the new form Player appear. You're on the right track. That's all we need to know to assign 1-to-1 dependency with constraints!

End of Tutorial


This was the first part of the question. The rest is very easy and will require the use of Random. I've included the full code of the application below.

The Game Class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OneToOne
{
public partial class Game : Form
{
//fields
//create the dependency to Play Form
private Play myPlayForm;

//constructor
public Game()
{
myPlayForm = new Play(this);//create a Play form if the game exists
InitializeComponent();
}

//events
private void btnPlay_Click(object sender, EventArgs e)
{
myPlayForm.Show();
}

//methods
public void showValue(int maxval)
{
lblMax.Text = "Max Number is " + maxval.ToString()+ ".\n\nRestart game to play again";
}
}
}


The Play Class


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OneToOne
{
public partial class Play : Form
{
//fields
//create the dependency to Game Form
private Game myGameForm;
private int myNum = 0;
private int iCounter = 0;

//properties
public int MyNum
{
get { return myNum; }
set { myNum = value; }
}

public int ICounter
{
get { return iCounter; }
set { iCounter = value; }
}

//constructor
public Play(Game gameform)
{
InitializeComponent();
myGameForm = gameform; //assign the object myGameForm to the argument gameform
}

//events
private void btnClick_Click(object sender, EventArgs e)
{
showValue();
}

//methods
public void showValue()
{
Random myRan = new Random(); //create a myRan variable of type Random (Class)
int iRan = myRan.Next(100); //gets a random number from 0 - 100
iCounter++;

if (iCounter <= 3) //can click only 3 times
{
lblNum.Text = iRan.ToString(); //display random value on form
if (iRan >= myNum)
{
myNum = iRan;
}
}
else
{
MessageBox.Show("Cannot click more than 3 times. Turn has ended");
//prevent user from clicking more than 3 times
btnClick.Enabled = false;

//send the max value to Form1 or GameForm
myGameForm.showValue(myNum);
//hide this form
this.Hide();

//show the previous form
myGameForm.Show();

}
}
}
}


I hope that helps!

Tuesday, July 21, 2009

Designing a web page - how to set up your template with HTML and CSS

Getting web projects might sound like a long and hard journey, coding your way through HTML and CSS (including Javascript, Flash, etc) while trying to keep an optimal standard for XHTML Strict 1.0 and CSS 2.1.

However, this is not as complicated as it seems. My personal approach to design a webpage for clients is to follow a set of simple guidelines, that can help in planning and achieving the desired results.

1)
Draw/Sketch your index.html page (main page). You should know where you are going to place the main sections of your page. No coding at this stage is involved.
e.g. Conceptualise your webpage by sections and categories. You might want a head section (logo, website title), the links section (underneath the head or at the left-hand side of the page), a content section and a footer for your copyrighting and disclaimer texts or a sitemap of your website.

2) Create your HTML and CSS page.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Tutorial - Getting Started!</title>
<link rel="stylesheet" type="text/css" href="sample.css" />
</head>
<body>

</body>
</html>


Note that for this tutorial, the doctype is Transitional. If you feel confident in your web coding skills, go for Strict. Do not forget to validate your HTML and CSS pages if you want to abide by the futureproof standards of the W3C!

3)
Next stop: transfer those sections into HTML code. Piece of cake if you want my opinion. To maintain a consistent page and to separate content and presentation (and for a neat code!), use divs. NO TABLES PLEASE!
Why so you might ask? Well, if you need to re-organise your sections (e.g. you want your links section to be on the left-hand side of the webpage instead of the top section, you just have to change the CSS (discussed below), without changing the HTML page. This will help you achieve a tidy and efficient code, and it takes a few seconds to do it).



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Tutorial - Getting Started!</title>
<link rel="stylesheet" type="text/css" href="sample.css" />
</head>
<body>
<div id="wrapper">
<div id="top">
<h1>Getting Started!</h1></div>
<div id="links"></div>
<div id="main"></div>
<div id="bottom"></div>
</div><!-- end wrapper -->

</body>
</html>


Note:
I like my page to be centered. Have you noticed that a website looks kinda amateurish and poorly designed when the page is glued to the left? I am sure you don't want that.

4) Now, the interesting part: CSS!
I usually use a body width of 850px that will contain the whole page. Why? Because you have to think about users with low screen resolution. I would recommend 800-850px for the page width (screen excluding borders), but you can change the width depending on your requirements. To achieve this and to contain all the contents in a 850px-wide div, I use the 'wrapper' section.



#wrapper
{
width:850px;
margin:0 auto;
}

#top
{
width:850px;
position:relative;
float:left;
}

#links
{
width:200px;
position:relative;
float:left;
}

#main
{
width:650px;
position:relative;
float:left;
}

#bottom
{
width:850px;
position:relative;
float:left;
}


5) You might also want to specify the font types to be used and the sizes. It is recommended to place your text styles into your 'body' section in the CSS so that everything that you enclose within your body or content section reflects the type changes.



body
{
background-color:black;
font-family:helvetica;
color:#ffffff;
font-size:14px;
}

/***** Text Formatting *****/
h1
{
font-size:20px;
}

h2
{
font-size:18px;
}



Here, it takes less than 20 minutes to do this. This will provide for the structure of your website and everything can be modified from there (width, font, section/div arrangements). Now all you need is to use your creativity and design skills to make your website attractive! The use of pictures for the background, sections and so on is highly recommended!

Saturday, July 18, 2009

I'm a TAFE celebrity! :D

Hi guys,

There's an article about me on Tafe School of IT Website!

http://emmie.surgero.com

Paul (Ponpolpon) and I volunteered for a complete re-design of a website including HTML, CSS and Javascript, for Emmie/Emeralda, Poland.
Time taken: 2 hours.


Check it out!
http://emmie.surgero.com

Friday, July 10, 2009

Enabling audio on your webpage on page load

Hi Sara
There are many ways to play music on page load, including flash (easiest) or playlists.
You can load a m3u playlist to play your songs in order.
However, what you want to achieve (play one clip only once, and sequentially play another one, within a loop) is very complex, but not impossible.

First of all, playing mp3 files are not advised, given the huge size (~3mb or more).

My first guess to solve your problem is as follows:

Initiate both clips at the same time on page load (e.g. t=0).
Liberi Fatali should have a 3-5 seconds initial blank so that your first clip can be heard without interference.

To do this, place the following in the HEAD section of your page:


<embed src="http://emmie.surgero.com/HerGroovyTrick.mp3" width="310" height="45" autostart="true" autoplay="true" loop="false" volume="85%">
<embed src="http://emmie.surgero.com/LiberiFatali.mp3" width="310" height="45" autostart="true" autoplay="true" loop="true" volume="85%">


Remember to add 3-5 seconds blank in your Liberi Fatali clip and that should do the trick!

Denis