// Program to test solutions to problem #5 on the cse143 midterm, winter 2020. // Fill in your solution to sublist, then compile and run the program. import java.util.*; class ArrayIntList { public ArrayIntList sublist(int start, int stop) { // fill in your solution here // you can also rename the parameters above } public ArrayIntList sublist2(int start, int stop) { ArrayIntList result = new ArrayIntList(); for (int i = start; i < stop; i++) { result.elementData[i - start] = elementData[i]; } result.size = stop - start; return result; } private int[] elementData; // list of integers private int size; // current number of elements in the list public static final int DEFAULT_CAPACITY = 11; // 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, failCount; public static void main(String[] args) { int[] data = {5, 4, 3, 7, 18, 24, 0, -4, 12, 15, 9}; test(data, 3, 8); test(data, 0, 10); test(data, 0, 11); test(data, 2, 8); test(data, 5, 5); test(data, 0, 0); test(data, 7, 7); test(data, 6, 9); test(data, 8, 11); test(data, 9, 11); test(data, 10, 10); test(data, 10, 11); test(data, 11, 11); data = new int[] {}; test(data, 0, 0); data = new int[] {42}; test(data, 0, 1); data = new int[] {2, 4}; test(data, 0, 0); test(data, 0, 1); test(data, 0, 2); test(data, 1, 1); test(data, 1, 2); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data, int start, int stop) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); for (int n : data) { list1.add(n); list2.add(n); } System.out.println("original list = " + list1); ArrayIntList list3 = list1.sublist2(start, stop); boolean fail = false; ArrayIntList list4 = null; try { list4 = list2.sublist(start, stop); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail) { if (!list3.toString().equals(list4.toString())) { System.out.println("expected sublist = " + list3); System.out.println("their sublist = " + list4); fail = true; } else { System.out.println("extracted lists match"); } if (!list1.toString().equals(list2.toString())) { System.out.println("expected list after = " + list1); System.out.println("your list after = " + list2); fail = true; } } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }