- Activity 1
-
Download the following files:
Examine the code
and run the program
(SortSongs)
to see how it works.
It should print out a list of Songs:
1. "One Moment in Time" by Whitney Houston (5:27)
2. "Time After Time" by Cyndi Lauper (4:57)
3. "Love me Two Times" by The Doors (3:17)
4. "Time is on my Side" by The Rolling Stones (3:00)
5. "Does Anybody Really Know What Time it is?" by Chicago (4:35)
6. "Time of my Life" by David Cook (3:38)
7. "Any Time at all" by The Beatles (2:12)
8. "Too Much Time on my Hands" by Styx (4:33)
9. "Big Time" by Peter Gabriel (4:32)
10. "One Time" by Justin Bieber (4:04)
Revise the class Song
so that it implements the Comparable<Song> interface.
Songs should be sorted by title.
Acivate the code in SortSongs for activity 1.
Run the program again
and check that the second presentation of the list is sorted by title.
1. "Any Time at all" by The Beatles (2:12)
2. "Big Time" by Peter Gabriel (4:32)
3. "Does Anybody Really Know What Time it is?" by Chicago (4:35)
4. "Love me Two Times" by The Doors (3:17)
5. "One Moment in Time" by Whitney Houston (5:27)
6. "One Time" by Justin Bieber (4:04)
7. "Time After Time" by Cyndi Lauper (4:57)
8. "Time is on my Side" by The Rolling Stones (3:00)
9. "Time of my Life" by David Cook (3:38)
10. "Too Much Time on my Hands" by Styx (4:33)
- Activity 2
-
In the class Song,
add a Comparator<Song>
called BY_ARTIST.
It sorts the Songs by artist
(by alphabetical order,
including any "The" that might be in the name --
so "The Beatles" come after "Cyndi Lauper").
Don't worry about what happens when there's more than one song
by the same artist.
There are ways to make it sort by title within artist,
but I don't want you to bother about it.
Activate the code in SortSongs for activity 2
and run the program again.
The third list should be sorted by artist:
1. "Does Anybody Really Know What Time it is?" by Chicago (4:35)
2. "Time After Time" by Cyndi Lauper (4:57)
3. "Time of my Life" by David Cook (3:38)
4. "One Time" by Justin Bieber (4:04)
5. "Big Time" by Peter Gabriel (4:32)
6. "Too Much Time on my Hands" by Styx (4:33)
7. "Any Time at all" by The Beatles (2:12)
8. "Love me Two Times" by The Doors (3:17)
9. "Time is on my Side" by The Rolling Stones (3:00)
10. "One Moment in Time" by Whitney Houston (5:27)
- Activity 3
-
Add another Comparator<Song>
to Song,
this one called BY_LENGTH.
It will be used to sort the songs from shortest to longest.
You will need to use the total length in seconds to do this sort.
Activate the two sections of code for activity 3
and run the program again.
This third list should be sorted from shortest to longest,
1. "Any Time at all" by The Beatles (2:12)
2. "Time is on my Side" by The Rolling Stones (3:00)
3. "Love me Two Times" by The Doors (3:17)
4. "Time of my Life" by David Cook (3:38)
5. "One Time" by Justin Bieber (4:04)
6. "Big Time" by Peter Gabriel (4:32)
7. "Too Much Time on my Hands" by Styx (4:33)
8. "Does Anybody Really Know What Time it is?" by Chicago (4:35)
9. "Time After Time" by Cyndi Lauper (4:57)
10. "One Moment in Time" by Whitney Houston (5:27)
and the last from longest to shortest.
1. "One Moment in Time" by Whitney Houston (5:27)
2. "Time After Time" by Cyndi Lauper (4:57)
3. "Does Anybody Really Know What Time it is?" by Chicago (4:35)
4. "Too Much Time on my Hands" by Styx (4:33)
5. "Big Time" by Peter Gabriel (4:32)
6. "One Time" by Justin Bieber (4:04)
7. "Time of my Life" by David Cook (3:38)
8. "Love me Two Times" by The Doors (3:17)
9. "Time is on my Side" by The Rolling Stones (3:00)
10. "Any Time at all" by The Beatles (2:12)
Note that you didn't need to do another comparator
in order to get the list sorted from longest to shortest.
That's because every Comparator object has a reversed method,
that returns another Comparator
that sorts in the opposite order.
Does that seem odd to you?
It should.
What I've told you about interfaces
suggests that interfaces shouldn't be able to inherit anything
from the interface.
It turns out that since Java 8,
there's more to interfaces than what I've told you.
We'll talk more about that in CSCI 2341 (Data Structures).