// Hw9SortableArrays.java // // Do not modify the Hw9SortableArrays main class. Make this code work // by adding the appropriate classes with the appropriate methods as // described by the test methods called by the main method. // Note that you do not have to add any code to the test cases, though // you do have to solve them with general-purpose solutions (and not just // hard-code the example test cases!). // Hint: to solve this incrementally, you may wish to comment out // parts of the test code so the parts you have implemented will compile // and can be tested as you go. import java.util.Arrays; public class Hw9SortableArrays { public static void main(String[] args) throws Exception { testSortableArrayClass(); } private static void testSortableArrayClass() { System.out.print("Running SortableArray class tests... "); // Constructor -- uses a variable-length parameter list! SortableArray a1 = new SortableArray(2, 5, 1); assert(a1.toString().equals("[2, 5, 1]")); // hint: just use Arrays.toString! SortableArray a2 = new SortableArray(2, 5); assert(a2.toString().equals("[2, 5]")); SortableArray a3 = new SortableArray(1, 2, 3, 4, 5, 6, 7, 8, 9); assert(a3.toString().equals("[1, 2, 3, 4, 5, 6, 7, 8, 9]")); // compareTo: longer arrays come first assert(a1.compareTo(a2) < 0); // because a1 has 3 elements, a2 has 2, so // a1 comes first (that is, "a1 < a2") assert(a2.compareTo(a1) > 0); // ditto assert(a1.compareTo(a3) > 0); // a3's array is longer // compareTo, continued: for same-length arrays, the one // with the smaller average value comes first. Note that the // averages are computed as doubles and not with integer division. // Also, be careful when converting doubles into return values // for compareTo. For example, what if one value is greater than // the other by just 0.1? If you simply cast this to an int, what value // would you return? SortableArray a11 = new SortableArray(2, 5, 6); // higher average than a1 SortableArray a12 = new SortableArray(2, 5, 0); // smaller average than a1 SortableArray a13 = new SortableArray(2, 1, 5); // same average as a1 assert(a1.compareTo(a11) < 0); assert(a1.compareTo(a12) > 0); assert(a1.compareTo(a13) == 0); // compareTo, still continued: what to do with null? // Answer: nulls always come first! SortableArray nullA1 = new SortableArray(null); SortableArray nullA2 = new SortableArray(null); assert(nullA1.toString().equals("null")); assert(a1.compareTo(nullA1) > 0); assert(nullA1.compareTo(a1) < 0); assert(nullA2.compareTo(nullA1) == 0); // Finally, let's create an Array of SortableArrays, and then // sort them to prove this works SortableArray[] list = { new SortableArray(2, 5, 1), new SortableArray(null), new SortableArray(0, 2, 5), new SortableArray(6, 0), new SortableArray(null), new SortableArray(3, 3) }; assert(Arrays.toString(list).equals( "[[2, 5, 1], null, [0, 2, 5], [6, 0], null, [3, 3]]")); Arrays.sort(list); assert(Arrays.toString(list).equals( "[null, null, [0, 2, 5], [2, 5, 1], [6, 0], [3, 3]]")); System.out.println("Passed all tests!!!"); } } ////////////////////////////////// // SortableArray class ////////////////////////////////// class SortableArray { // ADD CODE HERE!!! // (You may also have to modify the SortableArray class declaration...) }