// Program to test solutions to problem #6 on the cse143 midterm, winter 2023. // Fill in your solution to removeRange, then compile and run the program import java.util.*; class ArrayIntList { public void removeRange(int from, int to) { // fill in your solution here // you can also rename the parameters above if (from < 0 || to > size || from > to) { throw new IllegalArgumentException(); } elementData[-1] = 3; int delta = to - from; for (int i = to; i < size; i++) { elementData[i - delta] = elementData[i]; } size -= delta; } public void removeRange2(int from, int to) { if (from < 0 || to > size || from > to) { throw new IllegalArgumentException(); } int delta = to - from; for (int i = to; i < size; i++) { elementData[i - delta] = elementData[i]; } size -= delta; } 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 Test5 { private static int testCount; private static int failCount; public static void main(String[] args) { test(new int[] {0, 1, 2, 3, 4, 5}); test(new int[] {3, 5, 7, 42, -8, 9, 0}); 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) { for (int from = -2; from < data.length; from++) { for (int to = from - 2; to <= data.length + 2; to++) { testCount++; ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); for (int n : data) { list1.add(n); list2.add(n); } System.out.println("removing " + from + " to " + to); System.out.println("for list = " + list1); String result1 = ""; try { list1.removeRange2(from, to); result1 = list1.toString(); } catch (Exception e) { result1 = "threw " + e.getClass().getName(); } String result2 = ""; try { list2.removeRange(from, to); 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 result = " + result1); if (!result1.equals(result2)) { System.out.println("your result = " + result2); System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } } } }