Vous êtes sur la page 1sur 4

Programming in C#

V Hng Khanh

Lab 04
Classes and Methods
& Inheritance and Polymorphism
A.

OBJECTIVES

B.

Create a class with constructors.


Declare fields and methods for class.
Know about inheritance.
Know about overload.
Know about override.
STEP BY STEP EXERCISES
Create a class named Circle that is base of Cylinder. The Circle provides two methods to calculate
area and perimeter of the circle. The Cylinder provides two methods to calculate area and volume
of the cylinder.

Lab 04

- 1/4 -

Programming in C#

Lab 04

V Hng Khanh

- 2/4 -

Programming in C#
C.

V Hng Khanh

GUIDED EXERCISES
Classes and objects
Create a class called studentInfo to descript general information (include student code, full name
and average mark) of a student. A student who passed an examination will have average mark
equal or greater than 4 which is from 0 to 10. Use this class to store details of 10 students. After
that, display the student list and then list all students who passed.

D.

EXTRA EXERCISES
Methods and polymorphism
You must create a class named Fraction to calculate some fraction operators of two fractions:
1. This class have three constructors
a. The first constructor doesnt have any parameter. The numerator will be 0 and
denominator will be 1.
b. The second constructor has one integer parameter so the numerator will equal with
the parameter and denominator will be 1.
c. The third constructor has two integer parameters so the numerator will equal with
the first parameter and denominator will equal with the second parameter.
2. Writing set method and get method for numerator and denominator and validate the
value that user set for denominator when they use set method.
3. Writing a ToString method to display this fraction.
4. This class have two methods called add
a. The first function has one parameter that typed Fraction. This function will
calculate the sum of current fraction and the fraction provided by the parameter.
b. The second function has one integer parameter. This function will calculate the sum
of current fraction and the integer parameter.
5. This class have two methods called subtract
a. The first function has one parameter that typed Fraction. This function will
calculate the subtraction of current fraction and the fraction provided by the
parameter.
b. The second function has one integer parameter. This function will calculate the
subtraction of current fraction and the integer parameter.
6. This class have two methods called multiply
a. The first function has one parameter that typed Fraction. This function will

Lab 04

- 3/4 -

Programming in C#

V Hng Khanh
calculate the multiplication of current fraction and the fraction provided by the
parameter.

b. The second function has one integer parameter. This function will calculate the
multiplication of current fraction and the integer parameter.
7. This class have two methods called divide
a. The first function has one parameter that typed Fraction. This function will
calculate the division of current fraction and the fraction provided by the parameter.
b. The second function has one integer parameter. This function will calculate the
division of current fraction and the integer parameter.

Lab 04

- 4/4 -

Vous aimerez peut-être aussi