// Program to test solutions to problem #7 on the cse143 midterm, summer 2013. // Fill in your solution to takeMax, then compile and run the program. import java.util.*; public class Test7 { static class ArrayIntList { public void takeMax(ArrayIntList other) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public void takeMax2(ArrayIntList other) { if (other.size > size) { ensureCapacity(other.size); } for (int i = 0; i < other.size; i++) { if (elementData[i] < other.elementData[i] || i >= size) { elementData[i] = other.elementData[i]; } } size = Math.max(size, other.size); } private int[] elementData; private int size; // post: ensures that the list has the given capacity; if not, the size is // doubled (or more if given capacity is even larger) public void ensureCapacity(int capacity) { if (capacity > elementData.length) { int newCapacity = elementData.length * 2 + 1; if (capacity > newCapacity) { newCapacity = capacity; } elementData = Arrays.copyOf(elementData, newCapacity); } } public String toString() { String result = ""; if (size > 0) { result += elementData[0]; } for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } return "[" + result + "]"; } public boolean equals(Object other) { if (other.getClass() == getClass()) { ArrayIntList aOther = (ArrayIntList) other; if (aOther.size == size) { for (int i = 0; i < size; i++) { if (elementData[i] != aOther.elementData[i]) { return false; } } return true; } } return false; } } private static int failCount; public static void main(String[] args) { int[][][] data = {{{}, {}}, {{}, {1, 2, -3}}, {{1, 2, -3}, {}}, {{1}, {-1}}, {{-1}, {1}}, {{41, 2, 4, 17, -3, -1, 20}, {6, 13, 4, 29, -4}}, {{6, 13, 4, 29, -4}, {41, 2, 4, 17, -3, -1, 20}}}; for (int[][] d : data) { ArrayIntList list1 = getList(d[0]); ArrayIntList list2 = getList(d[1]); test(list1, list2); } if (failCount == 0) { System.out.println("all tests passed"); } else { System.out.println("failed " + failCount + " tests"); } } public static void test(ArrayIntList list1, ArrayIntList list2) { System.out.println("list1 = " + list1); System.out.println("list2 = " + list2); ArrayIntList copy1 = copy(list1); ArrayIntList copy2 = copy(list2); copy1.takeMax2(copy2); System.out.println("list1 after = " + copy1); boolean fail = false; try { list1.takeMax(copy2); if (!copy1.equals(list1)) { fail = true; System.out.println("your list1 after = " + list1); } if (!copy2.equals(list2)) { fail = true; System.out.println("You changed the second list"); } } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } private static ArrayIntList copy(ArrayIntList a) { ArrayIntList other = new ArrayIntList(); other.elementData = new int[a.elementData.length]; for (int i = 0; i < a.size; i++) { other.elementData[i] = a.elementData[i]; } other.size = a.size; return other; } private static ArrayIntList getList(int[] data) { ArrayIntList list = new ArrayIntList(); list.elementData = data; list.size = data.length; return list; } }