C# Lab 04 - Tin học (Tin học căn bản) | Trường Đại học Nam Cần Thơ

C# Lab 04 - Tin học (Tin học căn bản) | Trường Đại học Nam Cần Thơ được sưu tầm và soạn thảo dưới dạng file PDF để gửi tới các bạn sinh viên cùng tham khảo, ôn tập đầy đủ kiến thức, chuẩn bị cho các buổi học thật tốt. Mời bạn đọc đón xem!

PROGRAMMING IN C#
Module 8: Abstract classes and Interface
Module 9: Properties and Indexers
Lab Guide for Lab4
Session Objectives
In this session, you will be practicing with
Abstract classes and Interface.
Properties and Indexers
Part 1 – Getting started (30 minutes)
1. Abstract class and properties demo
This application create 3 class:
GeometricObject abstract class has
+ Fields: color (String), weight (double)
+ Properties: PColor, PWeight (read, write)
+ Abstract method: findArea(),findPerimeter
Circle class and class Program in Main method creates some its objects and use its fields and methods.
Step 1: Open Visual Studio
Step 2: Select the menu File->New->Project to create console based project named ‘GeometricObjectDemo’
Step 3: Create abstract class and write code in itGeometricObject.cs
public abstract class GeometricObject
{
protected string color;
protected double weight;
// Default construct
protected GeometricObject(){
color = "white";
weight = 1.0;
}
// Construct a geometric object
protected GeometricObject(string color, double weight)
{
.color = color;this
.weight = weight;this
}
//properties
string PColorpublic
{
get{return color;}
set{color = value;}
}
PWeightpublic double
{
get{return weight;}
set{weight = ;}value
}
// Abstract method
public abstract double findArea();
// Abstract method
public abstract double findPerimeter();
}
Step 4: Create new file is and write code in itCircle.cs
//Circle class extends GeometricObject class
public class Circle : GeometricObject {
radius;private double
Circle(double x) {public
.radius = x;this
}
Circle( x, string c, w) : base (c, w) {public double double
.radius = x;this
}
String ToString(){public override
return "Circle has: radius is " + radius + ", color is " + PColor + ", weight is " + PWeight;
}
findArea(){public doubleoverride return Math.PI*radius*radius;}
findPerimeter(){public doubleoverride return 2 * Math.PI * radius;}
}
Step 5: Create new file is and write code in itProgram.cs
public class Program{
Main( [] args){static void string
c1 = (2.45, , 23);Circle new Circle "Blue"
+ c1.ToString());Console.WriteLine("Circle before change: "
//using properties
c1.PColor = ;"red"
c1.PWeight = 2.56;
+ c1.ToString());Console.WriteLine("Circle after change: "
.ReadLine();Console
}
}
Step 6: Select menu File -> Save to save the file
Step 7: Select Build -> Build ‘GeometricObjectDemo’ option to build the project
Step 8: Select Debug -> Start without Debuging to execute the program
The output of the program as following
2. Indexers demo
Step 1: Add a console based project ‘ to the solutionIndexersDemo
Step 2: Right click on project -> set as Startup projectIndexersDemo
Step 3: Rename the class file ‘Program.cs’ to ‘IndexersDemo.cs’
Step 4: Replace the code in ‘ .cs’ with the given codeIndexersDemo
using System;
class IndexerExample
{
[] intList = public int new int[10];
index]public int this int[
{
intList[index];}get return{
{intList[index] = ;}set value
}
}
class IndexerDemo
{
Main()static void
{
i, j = 0;int
indexTest = ();IndexerExample new IndexerExample
(i = 1; i < 10; i += 2)for
{
indexTest[j] = i;
j++;
}
(i = 0; i < 5; i++)for
, i, indexTest[i]);Console.WriteLine("indexTest[{0}] is {1}"
.ReadLine();Console
}
}
Step 5: Select menu File -> Save to save the file
Step 6: Select Build -> Build ‘ option to build the projectIndexersDemo
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following
Part 2 – Workshops (30 minutes)
Quickly look at Module 8 and 9 of workshops for reviewing basic steps for using Abstract class,
Interface, Propreties and Indexers types.
Try to compile, run and observe the output of sample code provided for related workshop. Discuss
with your class-mate and your instructor if needed.
Part 3 – Lab Assignment (60 minutes)
Do the assignment for Module 8 carefully. Discuss with your class-mates and your instructor if
needed. See file.ACTCSharp_Module8_Assignment.pdf
Part 4 – Do it your self
Implement a class named and two sub classes of Person named and . MakePerson Student Employee
Faculty Staff and sub classes of Employee. A Person has a name, phone number and email address. A
student has a program to which he/she enrolled ( Business, Computer Science...) . An Employee has a
department, salary and the date hired. A faculty member has office hours and a rank. A staff member has a
title. You are required to:
1. Override the ToString() to display the class name and the person's name and email address.
2. Provide properties in each class to read and write it's fields
3. Define a and CalculateBonus CalculateVacation as abstract methods in Employee class and
implement them in Faculty and Staff as follows
o Faculty get 1000 + 0.05 x Salary and Staff get 0.06 x Salary
o Faculty get 5 weeks if they are employed more than 3 years and additional one week if
he/she is "Senior Lecturer". Otherwise 4 weeks. Staff get 4 weeks for 5 year service.
Otherwise get 3 weeks
| 1/5

Preview text:

PROGRAMMING IN C#
Module 8: Abstract classes and Interface
Module 9: Properties and Indexers Lab Guide for Lab4 Session Objectives
In this session, you will be practicing with
 Abstract classes and Interface.  Properties and Indexers
Part 1 – Getting started (30 minutes)
1. Abstract class and properties demo
This application create 3 class:
GeometricObject abstract class has
+ Fields: color (String), weight (double)
+ Properties: PColor, PWeight (read, write)
+ Abstract method:
findArea(),findPerimeter
Circle class and class Program in Main method creates some its objects and use its fields and methods. Step 1: Open Visual Studio
Step 2: Select the menu File->New->Project to create console based project named ‘GeometricObjectDemo’

Step 3: Create GeometricObject.cs abstract class and write code in it
public abstract class GeometricObject { protected string color; protected double weight; // Default construct
protected GeometricObject(){ color = "white"; weight = 1.0; }
// Construct a geometric object
protected GeometricObject(string color, double weight) { this.color = color; this.weight = weight; } //properties public string PColor { get{return color;} set{color = value;} } public double PWeight { get{return weight;} set{weight = value;} } // Abstract method
public abstract double findArea(); // Abstract method
public abstract double findPerimeter(); }
Step 4: Create new file is Circle.cs and write code in it
//Circle class extends GeometricObject class
public class Circle : GeometricObject { private double radius; public Circle(double x) { this.radius = x; }
public Circle(double x, string c, double w) : base (c, w) { this.radius = x; } public String T override oString(){
return "Circle has: radius is " + radius + ", color is " + PColor + ", weight is " + PWeight; } public double override
findArea(){return Math.PI*radius*radius;} public double override
findPerimeter(){return 2 * Math.PI * radius;} }
Step 5: Create new file is Program.cs and write code in it public class Program{ static Main( void string[] args){
Circle c1 = new Circle(2.45, "Blue", 23); Console.WriteLine( + c1.ToString()); "Circle before change: " //using properties c1.PColor = "red"; c1.PWeight = 2.56;
Console.WriteLine("Circle after change: " + c1.ToString()); Console.ReadLine(); } }
Step 6: Select menu File -> Save to save the file
Step 7: Select Build -> Build ‘GeometricObjectDemo’ option to build the project
Step 8: Select Debug -> Start without Debuging to execute the program
The output of the program as following
2. Indexers demo
Step 1: Add a console based project ‘IndexersDemo’ to the solution
Step 2: Right click on project IndexersDemo -> set as Startup project
Step 3: Rename the class file ‘Program.cs’ to ‘IndexersDemo.cs’
Step 4: Replace the code in ‘IndexersDemo.cs’ with the given code using System; class IndexerExample {
public int[] intList = new int[10]; public int this[int index] { get{return intList[index];} set{intList[index] = value;} } } class IndexerDemo { static void Main() { int i, j = 0;
IndexerExample indexTest = new IndexerExample();
for (i = 1; i < 10; i += 2) { indexTest[j] = i; j++; } for (i = 0; i < 5; i++)
Console.WriteLine("indexTest[{0}] is {1}", i, indexTest[i]); Console.ReadLine(); } }
Step 5: Select menu File -> Save to save the file
Step 6: Select Build -> Build ‘IndexersDemo’ option to build the project
Step 7: Select Debug -> Start without Debuging to execute the program
The output of program as following
Part 2 – Workshops (30 minutes)
Quickly look at Module 8 and 9 of workshops for reviewing basic steps for using Abstract class,
Interface, Propreties and Indexers types. 
Try to compile, run and observe the output of sample code provided for related workshop. Discuss
with your class-mate and your instructor if needed.
Part 3 – Lab Assignment (60 minutes)
Do the assignment for Module 8 carefully. Discuss with your class-mates and your instructor if needed. See file.
ACTCSharp_Module8_Assignment.pdf
Part 4 – Do it your self
Implement a class named Person and
two sub classes of Person named Student and . Employee Make
Faculty and Staff
sub classes of Employee. A Person has a name, phone number and email address. A
student has a program to which he/she enrolled ( Business, Computer Science...) . An Employee has a
department, salary and the date hired. A faculty member has office hours and a rank. A staff member has a title. You are required to:
1. Override the ToString() to display the class name and the person's name and email address.
2. Provide properties in each class to read and write it's fields
3. Define a CalculateBonus and CalculateVacation as abstract methods in Employee class and
implement them in Faculty and Staff as follows
o Faculty get 1000 + 0.05 x Salary and Staff get 0.06 x Salary
o Faculty get 5 weeks if they are employed more than 3 years and additional one week if
he/she is "Senior Lecturer". Otherwise 4 weeks. Staff get 4 weeks for 5 year service. Otherwise get 3 weeks