-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathComparableMovie.java
52 lines (44 loc) · 1.16 KB
/
ComparableMovie.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package ObjectOrdering;
import java.util.Arrays;
class Movie implements Comparable<Movie> {
private double rating;
private String name;
private int year;
public Movie(String n, double rt, int y) {
this.name = n;
this.rating = rt;
this.year = y;
}
// compare on the basis of rating
/*
* public int compareTo(Movie arg) { return this.year - arg.year; }
*/
// compare on the basis of name
public int compareTo(Movie m) {
return this.name.compareTo(m.name); // this will call compareTo method of String Class
}
public double getRating() {
return rating;
}
public String getName() {
return name;
}
public int getYear() {
return year;
}
}
public class ComparableMovie {
public static void main(String[] args) {
Movie[] mov=new Movie[4];
mov[0]=new Movie("Inception",8.9,2010);
mov[1]=new Movie("The Dark Knight",9.0,2008);
mov[2]=new Movie("Intersteller",8.7,2014);
mov[3]=new Movie("The Prestige",8.2,2005);
Arrays.sort(mov);
System.out.println("Movie list in sorted order: ");
for (Movie mv : mov) {
System.out.println(
"Name: " + mv.getName() + "\t\t[Rating: " + mv.getRating() + "\tYear: " + mv.getYear() + "]");
}
}
}