Java Reference
In-Depth Information
a while loop that divides n by all numbers between 2 and (n-1) and tests whether the divi-
sion yields a whole number. You can write this test by using the modulo operator ( % ) to check
whether the integer division leaves a remainder of 0 (see the discussion of the modulo operator
in Section 3.8.3).
Exercise 4.34 In the findFirst method, the loop's condition repeatedly asks the files
collection how many files it is storing. Does the value returned by size vary from one check to
the next? If you think the answer is no, then rewrite the method so that the number of files is
determined only once and stored in a local variable prior to execution of the loop. Then use the
local variable in the loop's condition rather than the call to size . Check that this version gives
the same results. If you have problems completing this exercise, try using the debugger to see
where things are going wrong.
Improving structure—the Track class
We have seen in a couple of places that using strings to store all of the track details is not en-
tirely satisfactory and gives our music player a rather cheap feel. Any commercial player would
allow us to search for tracks by artist, title, album, genre, etc., and would likely include further
details, such as track playing time and track number. One of the powers of object orientation
is that it allows us to design classes that closely model the inherent structure and behaviors of
the real-world entities we are often trying to represent. This is achieved through writing classes
whose fields and methods match those of the attributes. We know enough already about how
to write basic classes with fields, constructors, and accessor and mutator methods that we can
easily design a Track class that has fields for storing separate artist and title information, for
instance. In this way, we will be able to interact with the objects in the music organizer in a way
that feels more natural.
So it is time to move away from storing the track details as strings, because having a separate
Track class is the most appropriate way to represent the main data items—music tracks—that
we are using in the program. However, we will not be too ambitious. One of the obvious hur-
dles to overcome is how to obtain the separate pieces of information we wish to store in each
Track object. One way would be to ask the user to input the artist, title, genre, etc., each time
they add a music file to the organizer. However, that would be fairly slow and laborious, so, for
this project, we have chosen a set of music files that have the artist and title as part of the file
name, and we have written a helper class for our application (called TrackReader ) that will
look for any music files in a particular folder and use their file names to fill in parts of the cor-
responding Track objects. We won't worry about the details of how this is done at this stage.
(Later in this topic, we will discuss the techniques and library classes used in the TrackReader
class.) An implementation of this design is in music-organizer-v5 .
Here are some of the key points to look for in this version:
The main thing to review is the changes we have made to the MusicOrganizer class, in
going from storing String objects in the ArrayList to storing Track objects (Code 4.7).
This has affected most of the methods we developed previously.
Search WWH ::

Custom Search