Bài tập interface trong java có lời giải

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 Phonechứa những phương thức trừu tượng sau đây:

- abstract void insertPhone[String name, String phone]

- abstractvoid removePhone[String name]

- abstractvoid updatePhone[String name, String newphone]

- abstractvoid searchPhone[String name]

- abstractvoid sort[]

2.Tạo lớp có tênPhoneBook thừa kế lớpPhone:

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

- Phương thứcinsertPhone[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ứcremovePhone[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ứcupdatePhone[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ứcmain[] để 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ênINewsbao gồm phương thứcvoid Display[].

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

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]. Tạo các phương thứcsetter và gettercho từng thuộc tính, riêngAverageRate thì chỉ có getter.

b. Thực thi giao diệnINews.

c. Phương thức Display[] sẽ in raTitle, PublishDate, Author, Content vàAverageRate của tin tức raconsole.

d. Khai báo một mảng có tênRateListkiểuintgồm 3 phần tử.

e. Tạo một phương thức có tênCalculate[] để thiết đặt thuộc tínhAverageRateở ýalà trung bình cộng của 3 phần tử của mảngRateListở ý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ọn1từ bàn phím thì:

a. Tạo một thể hiện của lớpNews và nhập giá trị cho các thuộc tínhTitle, 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ớpNews.

5.Nếu người dùng chọn2từ bàn phím thì lấy từng thể hiện trong ArrayList ra và thực thi phương thứcDisplay[]tương ứng củatừng thể hiện.

6.Nếu người dùng chọn3từ 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ứcDisplay[]thì cần thực thi phương thứcCalculate[]để tính đánh giá trung bình.

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

BÀI TẬP 3: BOOKS MANAGEMENT SYSTEM

In this exam, youll have to create a Books Management System. The system allows input, list, search books.

1.Create an interface nameIBookcontains thismethod:

·void display[]

2.Create a class nameBook:

a. Properties

·id [int]

·name [String]

·publishDate [String]

·author [String]

·language [String]

·averagePrice [float] Read only property

b. Implements theIBookinterface in step 1

c. Thedisplaymethodwill print all name, publishDate, author, language and averagePrice of the book

d. Declare an array namePriceListtypeinthas size of5elements

f. Create a method named calculateto setAveragePrice= average of 5 int elements inPriceListarray.

3.Display a tasks menu to choose:

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

4.If user type1from keyboard then:

a. Create a new Bookinstance and input name, publishDate, author, language and then ask user to enter 5 prices.

- The id property is auto increament [id++]

b. Create an ArrayList to keep the Book instance in step a

5.If user type2from keyboard then:

Loop from all book instances in the ArrayList then executes thedisplaymethod fromIBookinterface.

6.If user type3from keyboard then:

Loop from all book instances in the ArrayList then executes thecalculatemethod and then execute thedisplaymethod.

7.While user not chooses Exit [type4from 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 nameIStudentMark contains these properties and methods:

Methods:

void Display[]

2.Create a class nameStudentMark:

a. Properties:

- FullName [String]

- ID [int]

- Class [String]

- Semester [int]

- AverageMark [float] Read only property

b. Implements theIStudentMarkinterface 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.Displaya tasks menu to choose:

4.If user type1from the keyboard then:

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

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

5.If user type2from the keyboard then:

Loopfrom all studentmark instances in the ArrayList then executes theDisplay[]method fromIStudentMarkinterface.

6.If user type3from the keyboard then:

Loopfrom all studentmark instances in the ArrayList then executes theAveCal[]method and then executes theDisplay[]method.

7.While user not chooses Exit [type4from 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, youll have to create a Students Management system. The system allows input, list, search students.

1.Create an interface nameIStudentcontains these properties and methods:

Methods:

void Display[]

2.Create a class nameStudent:

a. Properties:

- FullName [String]

- ID [int]

- DateofBirth [String]

- Native [String]

- Class [String]

- PhoneNo [String]

- Mobile [int]

b. Implements theIStudentinterface 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 type1from 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 type2from the keyboard then:

Loopfrom all Student instances in the ArrayList then executes theDisplay[]method fromIStudentinterface.

6.If users type3from the keyboard then:

Loopfrom all instances ofStudent classin the ArrayList then search students of class that is inputed from the keyboardand then executes theDisplay[]method.

7.While user does not choose Exit [type4from the keyboard] then go back to the menu step 3 to ask user chooses an option.

Video liên quan

Chủ Đề