// Program to test solutions to problem #6 on the cse143 midterm, spring 2011. // Fill in your solution to removeFront, then compile and run the program // note: this program doesn't test the exception that should be thrown import java.util.*; class ArrayIntList { public void removeFront(int n) { // fill in your solution here // you can also rename the parameter above } public void removeFront2(int n) { if (n < 0 || n > size) throw new IllegalArgumentException(); for (int i = n; i < size; i++) elementData[i - n] = elementData[i]; size -= n; } 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() { elementData = new int[DEFAULT_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 Test6 { public static void main(String[] args) { for (int i = 0; i <= 10; i++) test(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i); test(new int[] {8, 17, 9, 24, 42, 3, 8}, 4); } public static void test(int[] data, int num) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); for (int n : data) { list1.add(n); list2.add(n); } System.out.println("removing " + num); System.out.println("list = " + list1); list1.removeFront2(num); System.out.println("result = " + list1); boolean fail = false; try { list2.removeFront(num); System.out.println("yours = " + list2); if (!list1.toString().equals(list2.toString())) { fail = true; } } catch (RuntimeException e) { System.out.println(" threw " + e); fail = true; } if (fail) System.out.println("failed"); else System.out.println("passed"); System.out.println(); } }