Tuesday, May 15, 2007

assignment 4 - Telephone number word generator - file stream code




///////////////////////////////////////////////////////////////////////////////////
// telephone-number word Generator
// coded by: philip hariyanto
//
// this program is a GUI program that, given a seven-digit number, and it will generate
// every possibe seven-letter word combination corresponding to that number. there will be
// 2,187 such combinations.
// Classes: 1. Form1.cs: to handle all GUI element and input validation ( input must have 7 numbers, must be all numbers, must not be 0 and 1)
// 2. Generator.cs: to handle all the algorithm to generate and write them to file
//
//
//////////////////////////////////////////////////////////////////////////////////


download code here:
assignment4.zip

download executable file here:
assignment4.exe

assignment 3 - Converter Fahrenheit to Celcius GUI program




///////////////////////////////////////////////////////////////////////////////////
// Converter Fahrenheit to Celcius program
// coded by: philip hariyanto
//
// this program using program that converts from Fahrenheit to Celsius.
// The Fahrenheit temperature should be entered from the keyboard (via a TextBox).
// A Label should be used to display the converted temperature.
// Use the following formula for the conversion:
// Celsius = ( 5 / 9 ) x ( Fahrenheit - 32 )
//additional feature: display 12 conversion history and format error handling
//////////////////////////////////////////////////////////////////////////////////


download code here:
assignment3.zip

download executable file here:
assignment3.exe

assignment 2 - error handling

this program have 2 classes:
- Class1: main class.this class will create an object by calling a constructor from CDivided class
- CDivided: this class has a default constructor and another constructor with 2 arguements
there are 3 privates variable: a as a nominator and b as a denominator, and result of a/b

this constructor from CDivided will pass information about constructor failure

download the code here:
assignment2.zip

assignment 1 - Employee hierarchy test application - polymorphism

// PayrollSystemTest.cs
// Employee hierarchy test application.
// this test will create each type of employees to the system
// declare an employee matrix with size of 4
// assign each element to one of the employee and
// generically process each element in array employees ( polymorphism )


the program will process using polymorphism technic by creating an array of their base class,
and assign each element to the specific object.

methodology:
I added 2 private variable in Employee class to store date class information about each employee's birthday and hire date
- private Date birthDate;
- private Date hireDate;
and 1 protected decimal bonus. I am using protected variable in order to let derived classes use bonus variable to add bonus to the earning.

thus, the constructor of base class employee need to add 2 more arguement
by adding dateOfBirth and dateOfHire varialbes of Date type to function declaration.
This also influences derieved class constructors that used base class constructor which their constructor needs to add
": base( first, last, ssn, dateOfBirth, dateOfHire )"


adding bonus feature:
because we need to check if an employee birthday month,
we need to create a public assessor of private Date birthDate. The bonus will be added if there is a match and
this feature will be handled in earning method in each type of employees.

advantance test program:
this program is using PayrollManager class to manage adding, display monthly report, and list of employees. Some employees
from PayrollSystemTest.cs is initialize in the class constructor.
because this is a mini program. this program will not tolarate some invalid input, such as: inputing empty character in menu.
Please follow format example if it is available.


download sample code here:
assignment1.zip

Wednesday, April 25, 2007

foreach statement

C# introduce the foreach statement, which iterates through the elements of an entire array or collection.

foreach ( type identifier in arrayName )
statement



simple declaration and output syntax

Declaring and Creating Arrays

int[] c = new int[ 12 ];

other ways:
int[] c; // declare the array variable
c = new int[ 12 ]; // create the array; assign to array variable

single lines for some variables:
string[] b = new string[ 100 ], x=newstring[ 27 ];




it's suggested that :

For readability, declare only one variable per declaration. Keep each declaration on a separate line and include a comment describing the variable being declared.

common output for printing array elements:
- using for loop
// output each array element's value \
for ( int counter = 0; counter <>
Console.WriteLine( "{0,5}{1,8}", counter, array[ counter ] );

output:

Friday, April 20, 2007

Getting user input

In c#, all input from user will be taken as string. we need to convert user input to decimal if we want to use it as a number. C# provide a nice class called Convert to deal with this.

example code:

string temp;
decimal amount;

Console.Write("account 1 deposit: "); // print question
temp=Console.ReadLine(); // get user input as string

// convert to decimal
amount = Convert.ToDecimal(temp);

// print user input with string currency specifier
Console.WriteLine("total deposit:{0:C}", amount);


output:
account 1 deposit: [500] => my input
total deposit:$500.00

formatting your text in c#

some common escape sequence / special character

Escape sequence

Description

\n

Newline. Positions the screen cursor at the beginning of the next line.

\t

Horizontal tab. Moves the screen cursor to the next tab stop.

\r

Carriage return. Positions the screen cursor at the beginning of the current linedoes not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.

\\

Backslash. Used to place a backslash character in a string.

\"

Double quote. Used to place a double-quote character (") in a string. For example,

Console.Write( "\"in quotes\"" );

displays

"in quotes"



And,
C# has added some nice default features that we can use to format our text/string in console.

string format specifiers.

Format Specifier

Description

C or c

Formats the string as currency. Precedes the number with an appropriate currency symbol ($ in the US). Separates digits with an appropriate separator character (comma in the US) and sets the number of decimal places to two by default.

D or d

Formats the string as a decimal. Displays number as an integer.

N or n

Formats the string with commas and a default of two decimal places.

E or e

Formats the number using scientific notation with a default of six decimal places.

F or f

Formats the string with a fixed number of decimal places (two by default).

G or g

General. Formats the number normally with decimal places or using scientific notation, depending on context. If a format item does not contain a format specifier, format G is assumed implicitly.

X or x

Formats the string as hexadecimal.



example code 1:
Console.Write( "Welcome\nto\nC#\nProgramming!" );

output:
welcome
to
C#
Programming!

example code 2, we will use default string formating ( without string specifier):

Console.WriteLine( "{0}\n{1}", "Welcome to", "C# Programming!" );


output:
Welcome to
C# Programming!

example code 3, this program will show u how to create time format of hh:mm:ss

int hour = 4;
int min = 30;
int sec =23;
Console.WriteLine("time: {0:D2}:{1:D2}:{2:D2}",hour,min,sec);

output:
time: 04:30:23

example code 4, this code will use the string fromat specifier C for currency

Console.WriteLine( "account1 balance: {0:C}", 1000 );

output:
account1 balance: $1,000.00

it's cool, isn't it?

hello world!

1st ex code: of course it’s hello world!


1 // Welcome1.cs
2 // Text-printing application.
3 using System;
4
5 public class Welcome1
6 {
7 // Main method begins execution of C# application
8 public static void Main( string[] args )
9 {
10 Console.WriteLine( “Hello world!” );
11 } // end method Main
12 } // end class Welcome1