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

Monday

WPF 1


This is a simple WPF GUI project. It uses some common layout containers.

Sunday

Java - Invalid Number Exception

public class JInvalidNumberException extends Exception {
String mistake;

public JInvalidNumberException()
{
super();
mistake = "Unknown error.";
}

public JInvalidNumberException(String reason)
{
super(reason);
mistake = reason;
}
}

Above is customized exception that is created to be thrown when a invalid postal code enter by a user.

Monday

MySQL note

MySQL does not have "SELECT INTO" statement. A similar statement in MySQL is "INSERT INTO"

INSERT INTO copy_table SELECT * FROM data_table;

The statement copies data from data_table into copy_table

Java data generator

// create child element
child_NumGp_SNO = doc.createElement("SNo");
child_NumGp.appendChild(child_NumGp_SNO);
String sno = String.valueOf(i+1);
Text txt = doc.createTextNode(sno);
// append child element
child_NumGp_SNO.appendChild(txt);

randint = r.nextInt(101);
System.out.print(randint + "\n");

child_NumGp_RAND = doc.createElement("Rand");
child_NumGp.appendChild(child_NumGp_RAND);
String randomint = String.valueOf(randint);
Text txt2 = doc.createTextNode(randomint);
child_NumGp_RAND.appendChild(txt2);


Above is part of Java code that will generate an output XML file that wil be feed by Serial Comm or Network Port to my earlier Line Graph program.

Thursday

Line Graph project

Fig 2

Fig 1

This program is created using C# with graph plotting control from Gigasoft

Not completed yet, but a least the graph is running

[12-May-2010]
Now my program is able to receive data from serial port. The data is displayed on the chart. (See Fig 2)

Wednesday

Stock analysis program using Perl and MySQL


Attach showing part of the perl code that write stock information to MySQL database. The completed code should able to read input list of stock to monitor from a text file, analyse SGX daily stock price in HTML file and write price and other information into a MySQL database.

At present, the program is able to analyse sgx HTML file and write data into MySQL. But the column alignment is a bit out.

Software used/installed
Perl - version 5.10
DBI
DBD-mysql

Heidi GUI for MySQL

Future work
Create a java program that reads from DB and display previous stock price record.

Tuesday

course software project II - program analysis


Sample output of VB.net program

course software project I program analysis



A program created using VB.net

Program challenge
To analyse the text file and create multiple output graphs

Software used/installed
WinGraphz - provides API for graph creation

Monday

Perl - random number generator


The program is written in perl. The motivation to write this script is to generate a random list of numbers that will serve as input to another of my on-going software project.

Objective of this program
1. To create an output which contains random 2-decimal places numbers.

Perl command used:
rand
open
print
sprintf


Requirement
To accept 3 user command inputs (count, filename, step)

Output
a file (filename) with number of lines specified by count

random number with 2 decimal places is generated assigned to y
step determines increment of x.