Bài tập ôn luyện OOP + Interface trong C# - Lập Trình C# - Lập Trình C Sharp
Question 1
I. Create an interface IPerson that has the following properties:
-
string Skills— Read/Write -
DateTime DateOfBirth— Read-only -
int Age— Read-only
II. Create an abstract class named Employee that performs the following tasks:
1. Fields – private
-
int _id— employee identity -
string _name— employee name
2. Constructor methods
-
First constructor:
Declare with one parameterint id, assign this value to the employee identity.
The employee name is assigned a default value:"No name". -
Second constructor:
Declare with two parametersint id, string name, then assign these values to the employee’s identity and name.
3. Properties – public
-
int ID— Read-only property that returns the employee ID. -
string Name— Read/Write property of the employee name.-
Must contain statements to throw a new
Exceptionif the input name’s length is less than 3 characters.
-
4. Abstract method – public
-
void ShowInfo()— without parameters.
III. Declaring class Programmer that inherits from abstract class Employee and implements interface IPerson.
1. Fields – private
-
string _skills— programmer skills -
DateTime _DOB— date of birth -
int _age— age of programmer
2. Constructor methods
-
First constructor with two parameters
(int id, string name)
→ Call base constructor with(id, name)and assign default values:-
_skills = string.Empty -
_DOB = DateTime.Now
-
-
Second constructor with three parameters
(int id, string name, string skills, DateTime dob)
→ Call base constructor(id, name), then assignskillsto_skillsanddobto_DOB.
3. Properties – public
-
string Skills— implement fromIPerson, Read/Write.
Must include statement to throw newExceptionif input value’s length < 0. -
DateTime DateOfBirth— implement fromIPerson, Read-only.
Returns the value of_DOB. -
int Age— implement fromIPerson, Read-only.
Returns the real age of the programmer calculated fromDateOfBirth.
4. Method – public
-
void ShowInfo()— display all programmer information in the following format:
Question 2
I. Create a class named HiredProgrammers that performs the following tasks:
1. Field – private
-
Declare a generic
List<Programmer>namedHPGM.
2. Constructor method
-
Declare a constructor with one parameter
int capacityto initialize theHPGMlist with the given capacity.
3. Methods – public
-
void AddNew(Programmer prog)
Add a programmer object into the listHPGM.
Must throw a newExceptionwhen the list is out of capacity. -
int ShowFilterInfo(int underage)
Display all information of programmers whoseAge≤underage.
Return the number of those programmers.
II. Create a class to test
-
Define an instance of
HiredProgrammersclass namedmyemployee, initialized with 3 as input capacity. -
Write statements to input 3 programmers and add them into
myemployee.
Usetry-catchblocks to handle exceptions during input. -
In the console:
-
Input a value
underage. -
Invoke
ShowFilterInfo(underage)withmyemployeeto display which programmers have an age less than or equal to that input value.
-
End of Question