Tạo bởi Trần Văn Điêp|
C Sharp

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 SkillsRead/Write

  • DateTime DateOfBirthRead-only

  • int AgeRead-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 parameter int id, assign this value to the employee identity.
    The employee name is assigned a default value: "No name".

  • Second constructor:
    Declare with two parameters int id, string name, then assign these values to the employee’s identity and name.

3. Properties – public

  • int IDRead-only property that returns the employee ID.

  • string NameRead/Write property of the employee name.

    • Must contain statements to throw a new Exception if 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 assign skills to _skills and dob to _DOB.

3. Properties – public

  • string Skills — implement from IPerson, Read/Write.
    Must include statement to throw new Exception if input value’s length < 0.

  • DateTime DateOfBirth — implement from IPerson, Read-only.
    Returns the value of _DOB.

  • int Age — implement from IPerson, Read-only.
    Returns the real age of the programmer calculated from DateOfBirth.

4. Method – public

  • void ShowInfo() — display all programmer information in the following format:

    Id: 1 | Name: Nguyen Van A | Skills: C#, SQL | DOB: 20/5/1990 | Age: 34

Question 2

I. Create a class named HiredProgrammers that performs the following tasks:

1. Field – private

  • Declare a generic List<Programmer> named HPGM.

2. Constructor method

  • Declare a constructor with one parameter int capacity to initialize the HPGM list with the given capacity.

3. Methods – public

  • void AddNew(Programmer prog)
    Add a programmer object into the list HPGM.
    Must throw a new Exception when the list is out of capacity.

  • int ShowFilterInfo(int underage)
    Display all information of programmers whose Ageunderage.
    Return the number of those programmers.


II. Create a class to test

  1. Define an instance of HiredProgrammers class named myemployee, initialized with 3 as input capacity.

  2. Write statements to input 3 programmers and add them into myemployee.
    Use try-catch blocks to handle exceptions during input.

  3. In the console:

    • Input a value underage.

    • Invoke ShowFilterInfo(underage) with myemployee to display which programmers have an age less than or equal to that input value.


End of Question

Phản hồi từ học viên

5

Tổng 0 đánh giá

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó