// Program to test solutions to problem #9 on the cse142 final, winter 2019. // Fill in your solution to surroundWithN, then compile and run the program. import java.util.*; public class FinalTest9 { public static int[] surroundWithN(int[] list1, int[] list2, int n) { // fill in your solution here // you can also rename the parameters above } // this is the sample solution public static int[] surroundWithN2(int[] list1, int[] list2, int n) { int[] result = new int[list1.length + 2 * n]; for (int i = 0; i < n; i++) { result[i] = list2[i]; } for (int i = 0; i < list1.length; i++) { result[i + n] = list1[i]; } for (int i = 0; i < n; i++) { result[list1.length + n + i] = list2[i]; } return result; } private static int testCount, failCount; public static final int ERRORS_MAX = 25; public static void main(String[] args) { failCount = 0; test(new int[] {}, new int[] {}); test(new int[] {}, new int[] {8}); test(new int[] {}, new int[] {3, 8}); test(new int[] {4}, new int[] {2, 7}); test (new int[] {7, 2, 8, 9, 4, 13, 7, 1, 9, 10}, new int[] {1, 2, 3, 4, 5}); if (failCount == 0) { System.out.println("PASSED all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] list1, int[] list2) { for (int i = 0; i <= list2.length; i++) test(list1, list2, i); for (int i = 0; i <= list1.length; i++) test(list2, list1, i); } public static void test(int[] list1, int[] list2, int n) { int[] copy1 = Arrays.copyOf(list1, list1.length); int[] copy2 = Arrays.copyOf(list2, list2.length); System.out.println("testing surroundWithN(" + Arrays.toString(copy1) + ", " + Arrays.toString(copy2) + ", " + n + ")"); testCount++; int[] result2 = surroundWithN2(list1, list2, n); System.out.println("correct: " + Arrays.toString(result2)); int[] result1 = null; boolean fail = false; RuntimeException except = null; try { result1 = surroundWithN(list1, list2, n); } catch (RuntimeException e) { fail = true; except = e; } fail = fail || !Arrays.equals(result1, result2) || !Arrays.equals(list1, copy1) || !Arrays.equals(list2, copy2); if (fail) { if (except != null) { StackTraceElement[] trace = except.getStackTrace(); int line = trace[0].getLineNumber(); System.out.println("Threw exception at line#" + line); } else { if (!Arrays.equals(result1, result2)) { System.out.println("yours : " + Arrays.toString(result1)); } if (!Arrays.equals(list1, copy1)) { System.out.println("list1 was: " + Arrays.toString(copy1)); System.out.println("list1 now: " + Arrays.toString(list1)); } if (!Arrays.equals(list2, copy2)) { System.out.println("list2 was: " + Arrays.toString(copy2)); System.out.println("list2 now: " + Arrays.toString(list2)); } } failCount++; System.out.println("failed"); } else { System.out.println("passed"); } System.out.println(); } }