// Program to test solutions to problem #5 on the cse143x midterm, autumn 2025. // Fill in your solution to reverseCopy, then compile and run the program import java.util.*; class ArrayIntList { public ArrayIntList reverseCopy() { // fill in your solution here } // this is the sample solution public ArrayIntList reverseCopy2() { ArrayIntList result = new ArrayIntList(elementData.length); for (int i = 0; i < size; i++) { result.elementData[i] = elementData[size - 1 - i]; } result.size = size; return result; } 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(int 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, failCount; public static void main(String[] args) { test(new int[] {}); test(new int[] {3, 8}); test(new int[] {4, 2, 5, 4}); test(new int[] {17, 42, 3, 8, 9, 12}); 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(50); ArrayIntList list2 = new ArrayIntList(50); for (int n : data) { list1.add(n); list2.add(n); } System.out.println("original list = " + list1); ArrayIntList list3 = list1.reverseCopy2(); System.out.println("reversed list = " + list3); boolean fail = false; ArrayIntList list4 = null; try { list4 = list2.reverseCopy(); System.out.println("yours = " + list4); if (!list3.toString().equals(list4.toString())) { fail = true; } } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!list1.toString().equals(list2.toString())) { System.out.println("original list was changed"); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }