// Zorah Fung, CSE 143 // Demonstrates behavior that requires classes to implement the Comparable interface. import java.util.*; public class CollectionsOrdering { public static void main(String[] args) { testStringOrdering(); testPointOrdering(); testCatOrdering(); } // Builds a List of Strings and calls several methods that require Strings // to have a natural ordering. public static void testStringOrdering() { String[] tempNames = { "Rebecca", "Rasika", "Amanda", "Shannon", "John" }; List names = Arrays.asList(tempNames); System.out.println(names); Collections.sort(names); System.out.println(names); System.out.println(Collections.binarySearch(names, "Shannon")); } // Builds a List of Points and calls several methods that require Points // to have a natural ordering. public static void testPointOrdering() { Point[] tempPoints = { new Point(8, 12), new Point(3, 17), new Point(3, 16), new Point(7, 5), new Point(-7, -1) }; List points = Arrays.asList(tempPoints); System.out.println(points); Collections.sort(points); System.out.println(points); System.out.println(Collections.binarySearch(points, new Point(8, 6))); } public static void testCatOrdering() { List cats = new ArrayList(); cats.add(new Cat("Grumpy", 0, 14.5)); cats.add(new Cat("Glenn", 9, 13)); cats.add(new Cat("Nick", 6, 50.3)); cats.add(new Cat("Ganda", 3, 10)); cats.add(new Cat("Ryan", 5, 16.2)); cats.add(new Cat("Duncan", 6, 20.0)); cats.add(new Cat("Radu", 8, 18.0)); cats.add(new Cat("Jeffrey", 7, 12)); cats.add(new Cat("Ayaz", 7, 15.5)); cats.add(new Cat("Grumpy", 6, 20.0)); cats.add(new Cat("Yen", 4, 11.11)); System.out.println(cats); Collections.sort(cats); System.out.println(cats); System.out.println(Collections.binarySearch(cats, new Cat("Grumpy", 6, 20.0))); } }