// Program to test solutions to problem #5 on the cse143 midterm, winter 2017. // Fill in your solution to removeAll, then compile and run the program import java.util.*; class ArrayIntList { public void removeAll(int n) { // fill in your solution here // you can also rename the parameter above } public void removeAll2(int n) { int newSize = 0; for (int i = 0; i < size; i++) { if (elementData[i] != n) { elementData[newSize] = elementData[i]; newSize++; } } size = newSize; } private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 100; // post: constructs an empty list of default capacity public ArrayIntList() { this(DEFAULT_CAPACITY); } // pre : capacity >= 0 (throws IllegalArgumentException if not) // post: constructs an empty list with the given capacity public ArrayIntList(int capacity) { if (capacity < 0) { throw new IllegalArgumentException("capacity: " + capacity); } elementData = new int[capacity]; size = 0; } // post: creates a comma-separated, bracketed version of the list public String toString() { if (size == 0) { return "[]"; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } result += "]"; return result; } } // post: appends the given value to the end of the list public void add(int value) { elementData[size] = value; size++; } } public class Test5 { private static int testCount; private static int failCount; public static void main(String[] args) { test(new int[] {12, 7, 8, 3, 9, 3, 2, 3, 3, 14}, new int[] {3, 12, 4, 14, 9}); test(new int[] {}, new int[] {3}); test(new int[] {4}, new int[] {2, 4, 6}); test(new int[] {8, 3}, new int[] {2, 3, 5, 8, 12}); test(new int[] {7, 7, 7}, new int[] {4, 7, 15}); test(new int[] {8, 8, 8, 8, 8}, new int[] {5, 8, 9}); test(new int[] {3, 3, 5, 5, 4, 4, 9, 9}, new int[] {3, 5, 4, 9, 1, 2}); test(new int[] {1, 3, 0, 2, 0, 0, 5, 0}, new int[] {-1, 2, 1, 0, 8}); if (failCount == 0) { System.out.println("passed all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data, int[] nums) { for (int n : nums) { testCount++; ArrayIntList list1 = new ArrayIntList(data.length); ArrayIntList list2 = new ArrayIntList(data.length); for (int value : data) { list1.add(value); list2.add(value); } System.out.println("removing " + n); System.out.println("original = " + list1); list1.removeAll2(n); String result1 = list1.toString(); String result2 = ""; try { list2.removeAll(n); result2 = list2.toString(); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println("threw " + e + " at line #" + line); result2 = "threw " + e.getClass().getName(); } System.out.println("correct = " + result1); if (!result1.equals(result2)) { System.out.println("yours = " + result2); System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } } }