Inf1 OP : Lab Sheet Week 6 Q2 - Arrays and Reference Types
Overview
Warning
Pair Programming:
This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.

This exercise looks briefly at building arrays of objects with reference types that we have built ourselves.

So far, we have only looked at arrays like int[], double[] and String[], which use built-in types for the elements. But once we start creating our own data types, we can use those in arrays too. As an example, let’s build a very simple class MusicTrack. The only thing we’ll store is two strings, namely the artist and the track title. Here is the API:

public MusicTrack(String artist, String title)
Class constructor
public String getArtist()
Return the track’s artist.
public String getTitle()
Return the track’s title
public String toString()
Return a string of the form title + " | " + artist

Implement the class MusicTrack.

An automated test has been created for this exercise: MusicTrackTest.java.

Next, we are going to build a class Favourites that stores a list of ‘favourite tracks’. For simplicity, we’ll limit the list to five items, and hold these in an array of type MusicTrack[]. Here is the API for Favourites:

public void addTrack(String artist, String title)
Add a new track to the list of favourites, passing the arguments artist and title to the constructor for MusicTrack. If we already have five favourites stored, don’t change the data, but print a warning message to the terminal.
public void showFavourites()
Print out the items in the list of favourites.

If we create and initialize an array of type MusicTrack[], then prior to adding any tracks via addTrack(), we’ll have an array of null objects. Since we don’t want to print these out in the showFavourites() method, we’ll have to test for this. For example, if mt is an element of the list, then we need to check mt != null.

Here is some example client code for calling these methods.

Favourites favourites = new Favourites();
favourites.addTrack("Fun", "Some Nights");
favourites.addTrack("Oliver Tank", "Help You Breathe");
favourites.addTrack("Horse Feathers", "Fit Against the Country");
favourites.addTrack("Emile Sande", "Country House");
favourites.addTrack("Fun", "Walking the Dog");
favourites.addTrack("Porcelain Raft", "Put Me To Sleep");
favourites.showFavourites();

And here is some sample output:

Sorry, can't add: Put Me To Sleep | Porcelain Raft

Some Nights | Fun
Help You Breathe | Oliver Tank
Fit Against the Country | Horse Feathers
Country House | Emile Sande
Walking the Dog | Fun

Implement Favourites so that it behaves in a similar manner.

Make sure showFavourites() doesn’t generate an error even if no tracks are added, or if fewer than 5 tracks are added.

An automated test has been created for this exercise: FavouritesTest.java.

Can you see why storing lists of items in arrays causes difficulties when the quantity of items to be stored is unknown? Take a look at the Java ArrayList if you’d like to see how to overcome these difficulties. We will look at this in more detail next week.