0% found this document useful (0 votes)
8 views

C# Lab01

Lạp trình hướng đối tượng

Uploaded by

tranminhdangyorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

C# Lab01

Lạp trình hướng đối tượng

Uploaded by

tranminhdangyorn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier

Programming in C#
Lab 1

Objectives:
At the end of this session, you will able to understand:
 Data type in C#
 Input and output data in C#
 Number and datetime format specifier

Part I: Workshop – 15 minutes


Students open workshop in CD ROM, then View, Run, Think about it.

Part II: Step by step – 45 minutes


Exercise 1: Display a message

Step 1: Open Visual Studio 2005


Step 2: Select the menu File->New->Project to create console based project named ‘First_prg’ and
Solution named Session01 as shown in Figure 1

Figure 1: New Project Dialog Box


© 2009 FPT Aptech 2
C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
Step 3: Rename the class file ‘program.cs‘ to ‘First_prg.cs’
Step 4: Replace code in ‘First_prg.cs’ with given code
using System;
class Example
{
static void Main(string[] args)
{
Console.WriteLine("This is the my first program using C#”);
Console.ReadLine();
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build First_prg option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of the program as following

Exercise 2: Using datatype

Step 1: Add a console based project ‘DataType’ to the solution

© 2009 FPT Aptech 3


C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier

Step 2: Right click on project DataTypes -> set as Startup project


Step 3: Rename the class file ‘Program.cs’ to ‘DataTypes.cs’
Step 4: Replace the code in ‘DataTypes.cs’ with the given code

using System;
class Example
{
static void Main(string[] args)
{
int intVal;
double dblVal;
string strVal;
intVal = 10;
dblVal = 3.142;
strVal = "Fpt Aptech";
Console.WriteLine("{0} is an integer value", intVal);
Console.WriteLine("{0} is an double value", dblVal);
Console.WriteLine("{0} is an string", strVal);
Console.ReadLine();
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ‘DataTypes’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 3: value type

Step 1: Add a console based project ‘ValueType’ to the solution


Step 2: Right click on project ValueType-> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘ValueType.cs’
Step 4: Replace the code in ‘ValueType.cs’ with the given code

using System;
using System.Text;

class Example1
© 2009 FPT Aptech 4
C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
{
static void Main(string[] args)
{
int valueVal = 5;
Test(valueVal);
Console.WriteLine("The value of the variable is {0}", valueVal);
Console.ReadLine();
}
static void Test(int valueVal)
{
int temp = 5;
valueVal = temp * 2;
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ‘ValueType’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 3: Reference Type

Step 1: Add a console based project ‘ReferenceType’ to the solution


Step 2: Right click on project ReferenceType -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘ReferenceType.cs’
Step 4: Replace the code in ‘ReferenceType.cs’ with the given code

using System;
class ReferenceType
{
public int valueVal;
}
class TestReference
{
static void Main(string[] args)
{
ReferenceType refer = new ReferenceType();
© 2009 FPT Aptech 5
C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
refer.valueVal = 5;
Test(refer);
Console.WriteLine("The value of the variable is {0}",
refer.valueVal);
Console.ReadLine();
}
static void Test(ReferenceType refer)
{
int temp = 5;
refer.valueVal = temp * 2;
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ReferenceType option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 4: output

Step 1: Add a console based project ‘Output’ to the solution


Step 2: Right click on project Output -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘Output.cs’
Step 4: Replace the code in ‘Output.cs’ with the given code

/* This program demonstrates the output operators in C#*/


using System;
class Student
{
static void Main(string[] args)
{
//Declaring and intialising variables to store student details
int id = 1;
string name = "David George";
byte age = 18;
char gender = 'M';
float percent = 75.50F;

© 2009 FPT Aptech 6


C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
//Displaying the student details
Console.WriteLine("Student ID : {0}", id);
Console.WriteLine("Student Name : {0}", name);
Console.WriteLine("Age : " + age);
Console.WriteLine("Gender : " + gender);
Console.WriteLine("Percentage : {0:F2}", percent);
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build Output option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 5: Input

Step 1: Add a console based project ‘Input’ to the solution


Step 2: Right click on project Input -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘Input.cs’
Step 4: Replace the code in ‘Input.cs’ with the given code

using System;
/* The program demonstrates the input and output operations.*/
class Student
{
static void Main(string[] args)
{
//Declaring integer constant to store value 100
const int percentConst = 100;
//Declaring variable to store the student name
string studentName;

//Declaring variables to store the student marks


int english, maths, science;
//Declaring and initialising variable to store the percentage

© 2009 FPT Aptech 7


C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
float percent = 0.0F;
//Accepting the details of the student
Console.Write("Enter name of the student : ");
studentName = Console.ReadLine();

Console.Write("Enter marks for english : ");


english = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter marks for maths : ");


maths = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter marks for science : ");


science = Convert.ToInt32(Console.ReadLine());

//Calculating the percentage of the student


percent = ((english + maths + science) * percentConst) / 300;

//Displaying the details of the student


Console.WriteLine("Student Name : " + studentName);

Console.WriteLine("Marks obtained in English : {0}", english);


Console.WriteLine("Marks obtained in Maths : {0}", maths);
Console.WriteLine("Marks obtained in Science : {0}", science);
Console.WriteLine("Percent : {0:F2}", percent);
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ‘Input’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 5: Number format specifier

Step 1: Add a console based project ‘NumberFormat’ to the solution


© 2009 FPT Aptech 8
C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
Step 2: Right click on project NumberFormat -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘NumberFormat.cs’
Step 4: Replace the code in ‘NumberFormat.cs’ with the given code

/*This program demonstrates the the numeric formatting in C#*/


using System;
class NumberFormat
{
static void Main(string[] args)
{
Console.WriteLine("Currency formatting - {0:C} {1:C4}", 88.8, 888.8);
Console.WriteLine("Integer formatting - {0:D5}", 88);
Console.WriteLine("Exponential formatting - {0:E}", 888.8);
Console.WriteLine("Fixed-point formatting - {0:F3}", 888.8888);
Console.WriteLine("General formatting - {0:G}", 888.8888);
Console.WriteLine("Number formatting - {0:N}", 8888888.8);
Console.WriteLine("Hexadecimal formatting - {0:X4}", 88);
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ‘NumberFormat’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

Exercise 5: Datetime format specifier

Step 1: Add a console based project ‘DateTimeFormat’ to the solution


Step 2: Right click on project DateTimeFormat -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘DateTimeFormat.cs’
Step 4: Replace the code in ‘DateTimeFormat.cs’ with the given code

© 2009 FPT Aptech 9


C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier
using System;
class MainClass
{
public static void Main()
{
DateTime dt = DateTime.Now; // obtain current time

Console.WriteLine("d format: {0:d}", dt);


Console.WriteLine("D format: {0:D}", dt);

Console.WriteLine("t format: {0:t}", dt);


Console.WriteLine("T format: {0:T}", dt);

Console.WriteLine("f format: {0:f}", dt);


Console.WriteLine("F format: {0:F}", dt);

Console.WriteLine("g format: {0:g}", dt);


Console.WriteLine("G format: {0:G}", dt);

Console.WriteLine("m format: {0:m}", dt);


Console.WriteLine("M format: {0:M}", dt);

Console.WriteLine("r format: {0:r}", dt);


Console.WriteLine("R format: {0:R}", dt);

Console.WriteLine("s format: {0:s}", dt);

Console.WriteLine("u format: {0:u}", dt);


Console.WriteLine("U format: {0:U}", dt);

Console.WriteLine("y format: {0:y}", dt);


Console.WriteLine("Y format: {0:Y}", dt);
}
}

Step 5: Select menu File -> Save to save the file


Step 6: Select Build -> Build ‘DateTimeFormat’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following

© 2009 FPT Aptech 10


C# - Lab 1 – Data Type, Input and Output, Conversion, format specifier

Part III: Do it yourself – 60 minutes


Exercise 1: Write a program to enter: name, address, phone and display these information.
Exercise 2: Write a program to accept three integer number and find maximun number from three integer.
Exercise 3: Write a program that accepts a number between 1 and 7 from the user and return the
corresponding day of the week(1- Monday, 2- Tuesday and so on).
Exercise 4: Write a program to display the first 9 multiples of an integer. N entered from user

Exercise 5: Write a program to print the factorials of the integers from 1 to 20

Part IV: Homework


Exercise 1: Do assignment of module 1 in CD ROM
Exercise 2: Do assignment of module 2 in CD ROM
Exercise 3: Do assignment of module 3 in CD ROM

References
1) CD ROM C# Programming, Aptech Education
2) http://www.java2s.com/Tutorial/CSharp/CatalogCSharp.htm
3) MSDN Document

© 2009 FPT Aptech 11

You might also like