Java Reference
In-Depth Information
Exercise 4.17 Create a MusicOrganizer object in the second version of our project.
Experiment with adding some files to it and playing them.
If you want to use the files provided in the audio folder within the project, you must include
the folder name in the filename parameter, as well as the file name and suffix. For example, to
use the file BlindBlake-EarlyMorningBlues.mp3 from the audio folder, you must pass the string
"audio/BlindBlake-EarlyMorningBlues.mp3" to the addFile method.
You can use your own mp3 files by placing them into the audio folder. Remember to use the
folder name as part of the file name.
Also experiment with file names that do not exist. What happens when you use those?
Summary of the music organizer
We have made good progress with the basics of organizing our music collection. We can store
the names of any number of music files and even play them. We have done this with relatively
little coding effort, because we have been able to piggyback on the functionality provided by
library classes: ArrayList from the standard Java library and a music player that uses a third-
party class library. We have also been able to do this with relatively little knowledge of the
internal workings of these library classes; it was sufficient to know the names, parameter types,
and return types of the key methods.
However, there is still some key functionality missing if we want a genuinely useful program—
most obvious is the lack of any way to list the whole collection, for instance. This will be the
topic of the next section, as we introduce the first of several Java loop-control structures.
Processing a whole collection
At the end of the last section, we said that it would be useful to have a method in the music
organizer that lists all of the file names stored in the collection. Knowing that each file name
in the collection has a unique index number, one way to express what we want would be to say
that we wish to show the file name stored at consecutive, increasing index numbers starting
from zero. Before reading further, try the following exercises to see whether we can easily write
such a method with the Java that we already know.
Exercise 4.18 What might the header of a listAllFiles method in the MusicOrganizer
class look like? What sort of return type should it have? Does it need to take any parameters?
Exercise 4.19 We know that the first file name is stored at index zero in the ArrayList
and the list stores the file names as strings, so could we write the body of listAllFiles
along the following lines?
Search WWH ::

Custom Search