public class SquareOrder implements Comparator Shape public int compar

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class SquareOrder implements Comparator<Shape> {
public int compare(Shape o1, Shape o2) {
return (new Double(o1.square())).compareTo(o2.square());
}
}
public class PerimeterOrder implements Comparator<Polygon> {
public int compare(Polygon o1, Polygon o2) {
return (new Double(perimeter(o1))).compareTo(perimeter(o2));
}
}
public static void main(String[] args) {
...
System.out.println("Max square shape: " + Collections.max(shapes, new SquareOrder()));
System.out.println("Max perimeter poly: " + Collections.max(shapes, new PerimeterOrder()));
}