1711 Recitation Laboratory 6:
The Song and CDImage classes - working with
Use an array of objects.
Search an array of objects based on a composite key.
Practice with the has-a relationship.
Use an interface to write general code (e.g., sorting of Comparable)
Hand-in Requirements (Deliverables):
All projects and laboratories will be submitted electronically through webCT.
Zip up your entire project directory for submission.
(Right click on the project folder and follow the SentTo link.)
The project source directory should include the following:
- Source code for Song.java and SongTester.java (10 pts)
- Source code for CDImage.java and CDImageTester.java
RealAudiophonic Inc. produces software to manage the playing of music CDs
on computers. You have been hired to produce two of the classes needed
for the RealAudiophonic CD Manager.
In Parts I and II you will implement a
Song class that represents individual pieces of music.
The Song class implements Comparable
and contains the following information about the song:
the length of the song in seconds,
the artist name, and the song name.
The compareTo method uses song names to determine order. If
the song names are the same, then the artist names are checked.
In Part III
you will implement a
CDImage class that represents a music CD. The CDImage
class contains the title of the CD, the label (company that produced the CD),
and the number of tracks. The CDImage class also provides access
to Song objects representing the individual songs on the CD.
Part I: Project setup
- Create a lab06 project.
All classes in this project with be in the songs package.
Create SongTester and
CDImageTester classes containing main methods.
- Add a statement to SongTester and to CDImageTester
to output the message CS 1713 Lab 6: written by YOURNAME".
Compile and run.
- Create Song and CDImage classes with no main
- Add the attributes, constructor and toString to Song.
- Add statements to the main method of SongTester to
create an array of Song objects called hits.
Make the array have length at least 4, fill it with songs and output each entry.
Choose songs so that you can apprioriately test Part II.
Part II: Implementing and testing the Song class
- Now make Song be a Comparable. (Add a compareTo
method and the implements Comparable to the class header.)
- Test Song by outputting an table of values giving each song in
hits compared to each song in hits. If hits
has length 4, output a 4 by 4 table of integers.
- Implement an equals method that returns true only
if compareTo returns 0. The parameter of equals is a
Song while the parameter of compareTo is an object.
According to the definition of the Comparable interface,
when the parameter is null,
compareTo should throw a NullPointerException while
equals should return false. Test this by having SongTester
output a table of boolean values as above.
- (On your own) implement and test the usual accessor methods for Song.
- (On your own) implement a getTime method that returns a string
the running time of the song in minutes:seconds format. For example,
a song with a running time of 130 seconds will return a getTime
string of "2:10".
Use the DecimalFormat class to make sure that the seconds are always
displayed with 2 digits. (See Listing 3.5 of the textbook for an example.)
Part III: Implementing and testing the CDImage class
Design a CDImage class to represent a music CD. The constructor
takes three parameters: the title of the CD, the recording label,
and the number of tracks on the
CD. (Each track contains one song.) The CDImage class uses the
Song class developed above and should contain methods to accomplish
Implement the CDImage class.
Design a client program CDImageTester that tests all of the methods
in the CDImage class. During the laboratory, the constructor,
the attributes, and the toString method of CDImage
will be implemented. The remaining methods should be implemented on your own.
Create a CDImage with at least 5 songs on it for testing.
For testing you may use your own lists of songs, or you can use the following list
of Elvis Presley songs:
- Return a song given a track (1 <= track <= number
of tracks) or null if the track was empty or out of range.
- Print the CD in a readable form.
- Insert a song in a specified track. (Note: the parameters
to this method should be the track number, song name, artist name, and
the length of the song in seconds.) If the track is already occupied or
the track is out of range, do nothing.
- A second version of the insertion where the parameters are the
track number and a Song object.
- Remove a song from a specified track given the song name andartist name.
- Return the track number given the artist name and song name.
If that particular song is not on the CD, return -1.
(Use the equals method of Song for this.)
- Return a string that represents the total time of the CD in minutes and
- The CDImage class should also have accessor methods to return
the title, the recording label, and the number of tracks.
My Baby Left Me 121
We're Gonna Move 150
Poor Boy 133
When My Blue Moon Turns to Gold Again 141
Tell Me Why 120
These songs appear on an album entitled "Elvis: The Other Sides" put out by RCA.
The following list of George Strait songs appear on an album
entitled "If You Ain't Lovin' (You Ain't Livin')" put out by MCA Records:
Under These Conditions 208
Don't Mind If I Do 198
It's Too Late Now 149