C# - C Sharp: Bài tập phần Abstract Class & Interface

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

Bài tập 1: HỆ THỐNG QUẢN LÝ SỐ ĐIỆN THOẠI

1. Tạo một lớp có tên Phone chứa những phương thức trừu tượng sau đây:

- abstract void InsertPhone(string name, string  phone)

- abstract void RemovePhone(string name)

- abstract void UpdatePhone(string name, string newphone)

- abstract void SearchPhone(string name)

- abstract void Sort()

2. Tạo lớp có tên PhoneBook thừa kế lớp Phone:

- Tạo một ArrayList tên PhoneList để lưu dữ liệu.

- Phương thức InsertPhone(string name, string  phone):

   Nếu tên người dùng (name) chưa có sẵn trong PhoneList thì thêm người dùng cùng số điện thoại (phone) tương ứng vào

   Nếu tên người dùng đã có sẵn thì kiểm tra xem số điện thoại (phone) có khác so với số đã có không, nếu khác thì thêm vào sau số đã có theo dạng như ví dụ sau:

   "0912333333 : 0902345671"

- Phương thức RemovePhone(string name):

   Xóa người dùng cùng các số điện thoại của chủ sở hữu có tên (name) tương ứng khỏi PhoneList.

- Phương thức UpdatePhone(string name, string newphone):

   Thay số điện thoại cũ bằng số điện thoại (newphone) mới.

- Phương thức SearchPhone(string name):

   Tìm kiếm số điện thoại theo tên người dùng.

- Phương thức Sort():

   Sắp xếp các phần tử trong PhoneList theo tên người dùng.

3. Tạo lớp ManagePhoneBook chứa phương thức Main() để quản lý chương trình theo dạng Menu như sau:

   PHONEBOOK MANAGEMENT SYSTEM

   1. Insert Phone

   2. Remove Phone

   3. Update Phone

   4. Search Phone

   5. Sort

   6. Exit

Bài tập 2:

1. Tạo một giao diện (interface) có tên INews bao gồm phương thức void Display().

2. Tạo một lớp có tên News:

a. Khai báo các thuộc tính bao gồm: ID (int), Title (string), PublishDate (string), Author (string), Content (string) và AverageRate (float). Riêng AverageRate thì chỉ có get.

b. Thực thi giao diện INews.

c. Phương thức Display() sẽ in ra Title, PublishDate, Author, Content và AverageRate của tin tức ra console.

d. Khai báo một mảng có tên RateList kiểu int gồm 3 phần tử.

e. Tạo một phương thức có tên Calculate() để thiết đặt thuộc tính AverageRate ở ý a là trung bình cộng của 3 phần tử của mảng RateList ở ý d.

3. Tạo một menu lựa chọn gồm các mục sau:

   1. Insert news
   2. View list news
   3. Average rate
   4. Exit

4. Nếu người dùng chọn 1 từ bàn phím thì:

a. Tạo một thể hiện của lớp News và nhập giá trị cho các thuộc tính Title, PublishDate, Author, Content sau đó yêu cầu người dùng nhập vào 3 đánh giá để lưu vào RateList.

b. Tạo một ArrayList (chỉ tạo duy nhất một ArrayList) để lưu thể hiện của lớp News.

5. Nếu người dùng chọn 2 từ bàn phím thì lấy từng thể hiện trong ArrayList ra và thực thi phương thức Display() tương ứng của từng thể hiện.

6. Nếu người dùng chọn 3 từ bàn phím thì thực hiện tương tự như mục 5 ở trên nhưng trước khi thực thi phương thức Display() thì cần thực thi phương thức Calculate() để tính đánh giá trung bình.

7. Trường hợp người dùng chọn 4 thì sẽ thoát khỏi chương trình.

BÀI TẬP 3: BOOKS MANAGEMENT SYSTEM

In this exam, you’ll have to create a Books Management System. The system allows input, list, search … books.

1. Create an interface name IBook contains this method:

   · void Display()

2. Create a class name Book:

a. Properties

   · Id (int)

   · Name (string)

   · PublishDate (string)

   · Author (string)

   · Language (string)

   · AveragePrice (float) – Read only property

b. Implements the IBook interface in step 1

c. The display method will print all Name, PublishDate, Author, Language and AveragePrice of the book.

d. Declare an array name PriceList type int has size of 5 elements

f. Create a method named Calculate to set AveragePrice = average of 5 int elements in PriceList array.

3. Display  a tasks menu to choose:

   1. Insert new book
   2. View list of books
   3. Average Price
   4. Exit

4. If user type from keyboard then:

a. Create a new Book instance and input Name, PublishDate, Author, Language and then ask user to enter 5 prices.

   - The Id property is auto increament (Id++)

b. Create a List<Book> to keep the Book instance in step a

5. If user type 2 from keyboard then:

Loop from all book instances in the List<Book> then executes the Display method from IBook interface.

6. If user type 3 from keyboard then:

Loop from all book instances in the List<Book>  then executes the Calculate method and then execute the Display method.

7. While user not chooses Exit (type 4 from keyboard) then go back to the menu step 3 to ask user chooses an option.

BÀI TẬP 4: MARKS MANAGEMENT SYSTEM

1. Create an interface name IStudentMark  contains these properties and methods:

Methods:

void Display()

2. Create a class name StudentMark:

a. Properties:

- FullName (string)

- ID (int)

- Class (string)

- Semester (int)

- AverageMark (float) – Read only property

b. Implements the IStudentMark interface in step 1.

c. The method Display() will print all FullName, ID, Class, Semester, AverageMark.

d. Declare an array name SubjectMarkList type int has size of 5 elements.

e. Create a method named AveCal to set AverageMark = average of 5 int elements in SubjectMarkList array.

3. Display a tasks menu to choose:

menu-marks-management-system

4. If user type from the keyboard then:

a. Create a new StudentMark  instance and input FullName, Class, Semester and then ask users to enter 5 subjectmarks. The ID is auto increament. Note: Use try-catch-finally to ensure data is correct.

insert-marks-management-system

b. Create a ArrayList (create only one ArrayList) to keep the StudentMark instance in step 4a.

5. If user type 2 from the keyboard then:

Loop from all studentmark instances in the ArrayList then executes the Display() method from IStudentMark interface.

6. If user type 3 from the keyboard then:

Loop from all studentmark instances in the ArrayList then executes the AveCal() method and then executes the Display() method.

7. While user not chooses Exit (type 4 from the keyboard) then go back to the menu step 3 to ask user chooses an option.

BÀI TẬP 5: STUDENTS MANAGEMENT SYSTEM

In this exam, you’ll have to create a Students Management system. The system allows input, list, search students.

1. Create an interface name IStudent contains these properties and methods:

Methods:

void Display()

2. Create a class name Student:

a. Properties:

- FullName (string)

- ID (int)

- DateofBirth (string)

- Native (string)

- Class (string)

- PhoneNo (string)

- Mobile (int)

b. Implements the IStudent interface in step 1.

c. The method Display() will print all FullName, ID, DateofBirth, Native, Class, PhoneNo and Mobile of the student to the console.

3. Display  a tasks menu to choose:

   1. Insert new Student

   2. View list of Students

   3. Search Students

   4. Exit

4. If users type 1 from the keyboard then:

a. Create a new Student instance and inputs FullName, DateofBirth, Native, Class, PhoneNo, and Mobile. The ID is auto increament (++ID ).

b. Create a ArrayList (create only one ArrayList only) to keep the Student instance in step 4a.

5. If users type 2 from the keyboard then:

Loop from all Student instances in the ArrayList then executes the Display() method from IStudent interface.

6. If users type 3 from the keyboard then:

Loop from all instances of Student class in the ArrayList then search students of class that is inputed from the keyboard and then executes the Display() method.

7. While user does not choose Exit (type 4 from the keyboard) then go back to the menu step 3 to ask user chooses an option.

» Tiếp: Bài tập phần Windows Forms
« Trước: Bài tập phần Thừa kế & Đa hình
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!