Friday

STL - Vector

The sample code below shows how to use vector
(remember to add #include &lt vector &gt )

vector SS;
stringstream str1;
string str2;

cout << "setting String " << endl;

for (int j=0; j< 10; j++)
{
str1 << j;
cout << " str1 = " << str1.str() << endl;
str2 = "Hello ";
str2.append(str1.str());
SS.push_back(str2);

str2.clear();
str1.str("");
cout << " >> " << str2 << endl;
}

// to print out the string in order
vector::const_iterator cii;
for(cii=SS.begin(); cii!=SS.end(); cii++)
{
cout << *cii << endl;
}

cout << endl;

// to print out the string in reverse order
vector::reverse_iterator rii;
for (rii=SS.rbegin(); rii != SS.rend(); ++rii)
{
cout << *rii << endl;
}

Monday

Creating a Web Service

This mini tutorial, i will show how to create a web service using VS2008. At the end of the tutorial, a simple calculator will be created. User just need to key in the value of Principle, Interest and Rate, Interest will be returned.

1. From VS 2008, choose “File” menu, select “New-Web Site”. Select “ASP.NET Web Service”.

2. On the "New WebSite" Dialog Box, choose a Location as “File System”. Click "Browse" button to specify the folder where your solution will be stored.

3. Select C# as the Language. Click "Ok" when done.

4. Once ready, on the left pane (Solution Explorer), find and rename “Service.cs” to “FinancialWebSvc.cs” and “Service.asmx” to “FinancialWebSvc.asmx”.

5. Next open the FinancialWebSvc.asmx, change
CodeBehind= “~/App_code/Service.cs” to
CodeBehind= “~/App_code/FinancialWebSvc.cs”

Class= “Service” to
Class= “FinancialWebSvc.”



6. Select “Build” menu and click on “build Solution” menu under it.
Right click on the FinancialWebS project on the left pane.


Add the following code to your FinancialWebSvc.cs file

[WebMethod(Description = "Return interest")]
public int ComputeInterest(int P, int time, int rate)
{
int interest;

interest = (P * time * rate) / 100;

return interest;
}

5. From the "Build" menu, select "Build Solution".

7. Now you are ready to test out your newly created web service. From "Debug" menu, click on "Start Debugging"

Saturday

Android spelling application


Create this Android spelling application to let my nephew has fun learning his spelling.

Looking at the diagram, a child will enter his spelling into the text box. He will then need to press Enter. If the spelling he entered is correct, a Applause sound will be played.
If the spelling is incorrect, a Baby crying sound will be played.

Thursday

Delegate and Event

namespace Delegate_Event_Test
{
class Figure
{
private float m_xPos =0;
private float m_yPos = 0;
private float m_zPos = 0;
private float m_wPos = 0;

public Figure(float a, float b, float c, float d)
{
m_xPos = a;
m_yPos = b;
m_zPos = c;
m_wPos = d;
}

public void InvertX()
{
Console.WriteLine("X Before : {0}", m_xPos);
m_xPos = -m_xPos;
//Inverted("inverted by z-axis");
Console.WriteLine("X After : {0}", m_xPos);
}

public void InvertY()
{
Console.WriteLine("Y Before : {0}", m_yPos);
m_yPos = -m_yPos;
Inverted("inverted by y-axis");
Console.WriteLine("Y After : {0}", m_yPos);
}

public void InvertZ()
{
Console.WriteLine("Z Before : {0}", m_zPos);
m_zPos = -m_zPos;
Inverted("inverted by z-axis");
Console.WriteLine("Z After : {0}", m_zPos);
}

public void InvertW()
{
Console.WriteLine("W Before : {0}", m_zPos);
m_wPos = -m_wPos;
Inverted("inverted by w-axis");
Console.WriteLine("W After : {0}", m_zPos);
}

public delegate void FigureHandler(String str);
public static event FigureHandler Inverted;
}

class TestClass
{
public delegate void FigureDelegate();

static void Main(string[] args)
{
Figure figure = new Figure(10, 20, 30, 40);
FigureDelegate AxisChange_Delg;
AxisChange_Delg = new FigureDelegate(figure.InvertZ);

// Declare 3 delegates of FigureDelegate type and attach to these
// elements our three methods from Figure class.
// Now, every delegate keeps the address of the attached function.
Console.WriteLine("Choose a axis to change (x, y, z): ");
String ans = Console.ReadLine();

switch (Convert.ToChar(ans))
{
case 'x':
AxisChange_Delg = new FigureDelegate(figure.InvertX);
break;
case 'y':
AxisChange_Delg = new FigureDelegate(figure.InvertY);

break;
case 'z':
AxisChange_Delg = new FigureDelegate(figure.InvertZ);
break;
}

AxisChange_Delg();


Console.WriteLine("\n");
Figure.Inverted += new Delegate_Event_Test.Figure.FigureHandler(OnFigureInverted);

figure.InvertW();

Console.ReadLine();
}

private static void OnFigureInverted(string msg)
{
Console.WriteLine("Figure was {0}", msg);
}
}
}



The above shows how to use delegate in C#. Delegate is similar to function pointer in C language.
In the case above, AxisChange_Delg is assigned a function() depending on the selection made by user.
AxisChange_Delg is then called.

The last part of the program shows how to use event.

Tuesday

Placing array of Controls onto TableLayout


The figure shows control arrays of labels place onto TableLayout using C#. It isn't really difficult to create array of controls in C#. Also using TableLayout helps me to place these controls in a orderly manner quite easily.

A Typing Tutor


Typing Tutor.
Found this exercise in a Deitel book on Java. I used JTextArea, JButton for the controls and used GridBagLayout manger for the layout.
There are still work to be done before the requirements specified in the exercise are met. In mean time, there already idea in mind on other programming project to be done.

Sunday

Simple Java calculator


A simple java calculator program