INFO0062 - Object-Oriented Programming Exercise session #5 - Inheritance

Jean-François Grailet

University of Liège Faculty of Applied Sciences

Academic Year 2017 - 2018 Context Context Exercises Additional exercise

Sorting your audio library

Media players allow to sort in several manners, most notably

• Artist

• Genre

• Year of release

1 / 8 Context Exercises Additional exercise

Today’s program

Re-using your code from the previous session

• We will add new classes to allow sorting

• We will use inheritance to implement the different policies

• We will also re-organize list classes in the process

If you don’t have a solution to the exercices of the previous session, don’t worry

• You can use the suggested solution1

1available on http://www.run.montefiore.ulg.ac.be/~grailet/INFO0062.php 2 / 8 Exercises Context Exercises Additional exercise

Exercise 1

We want to expand our program from last week to produce two sorted lists of the albums: one list where the albums are sorted by their respective artist, and another list where the albums are sorted by their year of release (with the earliest works first). Calling toString() on each list should print their content with the details about the artist/year of release when relevant.

Since you already have a list class to build the albums from Audio.db2, and since we will maintain all lists3, we will have three lists with several similarities.

Your first task is design a superclass AlbumList. Consider these questions:

What are the elements that all lists will have in common ?

How does the existing list class relate to this superclass ?

Should this superclass be instantiable ?

Implement AlbumList and adapt your previous list class in consequence, if necessary.

2to simplify things a bit, additional exercise from previous session is not taken into account. 3the point is not to apply sorting algorithms on the first list; see courses like INFO0902 for this specific topic. 3 / 8 Context Exercises Additional exercise

Exercise 2

Our sorted lists will only be created after building the initial list/database. They can therefore be created by reading the entire initial list to insert each album, one by one. The sorting can therefore occur at the insertion of an album. This consist in visiting the list being built to see where the new album should be placed to respect the ordering.

Considering the statement above, ask yourself the following questions:

How can you instantiate your ordered lists from the initial database ?

What operations are likely to be identical for building each list ?

Can you translate this into your class hierarchy ?

If yes, how can you use the abstract keyword ?

If necessary, implement the additional class(es) to apply your answers.

4 / 8 Context Exercises Additional exercise

Exercise 3

Using what you implemented during previous exercises, implement the class(es) that can produce the sorted lists of albums and update your main() method to display both. Can you still use find() on your new lists ?

Your toString() method(s) should produce the following output (sorting by artist):

- AC/DC - The Razors Edge (1990) Highway To Hell (1979) - Daft Punk - Discovery (2001) Homework (1997) - David Bowie - Blackstar (2016) - Fatboy Slim - Palookaville (2004) Better Living Through Chemistry (1996) ...

5 / 8 Context Exercises Additional exercise

Exercise 3 (II)

As for the sorting by year, you should obtain something like this:

- 1976 - Oxygène by Jean Michel Jarre - 1979 - Highway To Hell by AC/DC - 1990 - The Razors Edge by AC/DC - 1996 - Travelling Without Moving by Better Living Through Chemistry by Fatboy Slim - 1997 - Oxygène 7-13 by Jean Michel Jarre Homework by Daft Punk ...

6 / 8 Additional exercise Context Exercises Additional exercise

Exercise 4

We implemented a solution to sort our album database by year or by artist. However, albums from a same year or artist are not sorted between themselves; i.e., albums from a same artist could be ordered by year/title, and albums from a same year could be sorted by title as well.

Modify your solution such that sequences of albums from a same year or artist are also sorted by title. Ideally, you should only modify existing child classes of your current solution to achieve this goal.

7 / 8 Context Exercises Additional exercise

Exercise 4 (II)

For instance, this is the display you should have for albums sorted by artist:

- AC/DC - Highway To Hell (1979) The Razors Edge (1990) - Daft Punk - Discovery (2001) Homework (1997) - David Bowie - Blackstar (2016) - Fatboy Slim - Better Living Through Chemistry (1996) Palookaville (2004) - Jamiroquai - Automaton (2017) Rock Dust Light Star (2010) Travelling Without Moving (1996) ...

8 / 8