Please help with this The Movie class will now implement Com
Please help with this
The Movie class will now implement Comparable. In your answer below, show the new class header for Movie and then implement the compareTo method as follows:
movie objects should be ordered based on title and then year
Solution
Hi Friend, Please find my implementation.
Please let me know in case of any issue.
public class Movie implements Comparable<Movie> {
private String title;
private int year;
// others instance members
// implementation of compareTo method
@Override
public int compareTo(Movie o) {
if(title.compareTo(o.title) < 0)
return -1;
else if(title.compareTo(o.title) > 0)
return 1;
else{ // title is equal so sort based on year
if(year < o.year)
return -1;
else if(year > o.year)
return 1;
else
return 0;
}
}
}
