Skip to content

Latest commit

 

History

History
29 lines (29 loc) · 1.49 KB

Exercise16102019.md

File metadata and controls

29 lines (29 loc) · 1.49 KB

In this exercise you will try hands on with the comparators and Comparable implementations

  • Clone this repository and import it as a gradle project in eclipse or any other IDE of your choice
  • Have a look at the file Song.java see that it implements a Comparable interface
  • You can also see that it implements a compareTo method
   public int compareTo(Song s)
    {
        return title.compareTo(s.getTitle());
    }
  • Your task is to change this compareTo implementation to compare using the rating attribute
    • Remeber your compareTo method should return 0 if ratings are equal, a positve number if this.rating is greather than other song's rating and a negative number otherwise
  • Run the sorting algorithm again to see the change in order (run JukeBox.java)
  • After this implement a RatingComparator which sorts according to the rating, if the ratings of two songs are equal then break the tie with the bpm attribute
    • have a look at the ArtistCompare comparator whcih sorts according to the artist name
	class ArtistCompare implements Comparator<Song> {
		public int compare(Song one, Song two) {
			return one.getArtist().compareTo(two.getArtist());
		}
	}
  • Finally, use the TreeSet datastructure to keep the songs sorted by rating and write a method to retrieve the top-k rated songs
public Song[] getTopKSongs(int k){
   //TODO return top-k songs according to their rating
   return null;
}