Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 29

Tạ Trung Hiếu – 20204970

Object-Oriented Programming
Lab 04: Inheritance and Polymorphism

Contents
1. Creating the Book class...........................................................................................................................1
2. Creating the abstract Media class............................................................................................................3
3. Creating the CompactDisc class..............................................................................................................6
6.1. Create the Disc class extending the Media class.............................................................................6
6.2. Create the Track class.......................................................................................................................8
6.3. Open the CompactDisc class.........................................................................................................10
4. Create the Playable interface.................................................................................................................10
5. Update the Cart class to work with Media............................................................................................12
6. Update the Store class to work with Media...........................................................................................17
7. Constructors of whole classes and parent classes.................................................................................19
8. Unique item in a list..............................................................................................................................19
9. Polymorphism with toString() method..................................................................................................20
10. Sort media in the cart.........................................................................................................................21
11. Create a complete console application in the Aims class..................................................................22

1. Creating the Book class


Add fields to the Book class

Generate Getter and Setter

Page 1 of 29
Tạ Trung Hiếu – 20204970

addAuthor(String authorName)

removeAuthor(String authorName)

Page 2 of 29
Tạ Trung Hiếu – 20204970

2. Creating the abstract Media class

Page 3 of 29
Tạ Trung Hiếu – 20204970

Page 4 of 29
Tạ Trung Hiếu – 20204970

Page 5 of 29
Tạ Trung Hiếu – 20204970

3. Creating the CompactDisc class


6.1. Create the Disc class extending the Media class

Page 6 of 29
Tạ Trung Hiếu – 20204970

CompactDisc extending the Disc class

Page 7 of 29
Tạ Trung Hiếu – 20204970

6.2. Create the Track class

Page 8 of 29
Tạ Trung Hiếu – 20204970

Page 9 of 29
Tạ Trung Hiếu – 20204970

6.3. Open the CompactDisc class


addTrack()

removeTrack()

getLength() method

4. Create the Playable interface

- Implement play() for DigitalVideoDisc and Track

Page 10 of 29
Tạ Trung Hiếu – 20204970

- Implement play() for CompactDisc

Page 11 of 29
Tạ Trung Hiếu – 20204970

5. Update the Cart class to work with Media

Page 12 of 29
Tạ Trung Hiếu – 20204970

Page 13 of 29
Tạ Trung Hiếu – 20204970

Page 14 of 29
Tạ Trung Hiếu – 20204970

Page 15 of 29
Tạ Trung Hiếu – 20204970

Page 16 of 29
Tạ Trung Hiếu – 20204970

6. Update the Store class to work with Media

Page 17 of 29
Tạ Trung Hiếu – 20204970

Page 18 of 29
Tạ Trung Hiếu – 20204970

7. Constructors of whole classes and parent classes

8. Unique item in a list


+ For the Media class: the title is equal

Page 19 of 29
Tạ Trung Hiếu – 20204970

+ For the Track class: the title and the length are equal

9.
Poly

morphism with toString() method

Page 20 of 29
Tạ Trung Hiếu – 20204970

10. Sort media in the cart

Page 21 of 29
Tạ Trung Hiếu – 20204970

11. Create a complete console application in the Aims class

Page 22 of 29
Tạ Trung Hiếu – 20204970

Page 23 of 29
Tạ Trung Hiếu – 20204970

Page 24 of 29
Tạ Trung Hiếu – 20204970

Page 25 of 29
Tạ Trung Hiếu – 20204970

Page 26 of 29
Tạ Trung Hiếu – 20204970

Page 27 of 29
Tạ Trung Hiếu – 20204970

Kết quả chạy: Các chức năng hoạt động bình thường (ngoại trừ chức năng sắp xếp chưa hoàn thiện)

Page 28 of 29
Tạ Trung Hiếu – 20204970

Page 29 of 29

You might also like