// Program to test solutions to problem #5 on the cse143 midterm, spring 2019. // Fill in your solution to removeMax, 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 removeMax() { // fill in your solution here } public void removeMax2() { if (size == 0) { throw new IllegalStateException(); } int max = 0; for (int i = 1; i < size; i++) { if (elementData[i] > elementData[max]) { max = i; } } for (int i = max; i < size - 1; i++) { elementData[i] = elementData[i + 1]; } size--; } 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: returns true if list is empty, false otherwise public boolean isEmpty() { return 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, failCount; public static void main(String[] args) { int[] data = {3, 1, 5, 7, 3, 19, 42, 8, 23, 7, 42, 2, -8, 9, 105, -3}; test(data); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); for (int n : data) { list1.add(n); list2.add(n); } while (!list1.isEmpty()) { System.out.println("original list = " + list1); list1.removeMax2(); boolean fail = false; try { list2.removeMax(); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail) { if (!list1.toString().equals(list2.toString())) { System.out.println("expected list after = " + list1); System.out.println("your list after = " + list2); fail = true; } else { System.out.println("lists after match"); } } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } } }