// Program to test solutions to problem #10 on the cse142 final, winter 2012. // Fill in your solution to removeZeros, then compile and run the program. import java.util.*; public class FinalTest10 { public static void removeZeros(int[] list) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void removeZeros2(int[] list) { int numNonZero = 0; for (int i = 0; i < list.length; i++) { if (list[i] != 0) { list[numNonZero] = list[i]; numNonZero++; } } for (int i = numNonZero; i < list.length; i++) { list[i] = 0; } } private static int count; private static int count2; private static Random r; public static void main(String[] args) { count = 0; r = new Random(); for (int i = 0; i <= 15; i++) { test(i); } if (count == 0) { System.out.println("PASSED all " + count2 + " tests"); } } public static void test(int i) { int[] data = new int[i]; test(data, 0); } public static void test(int[] data, int index) { if (index == data.length) { count2++; int[] data1 = Arrays.copyOf(data, data.length); int[] data2 = Arrays.copyOf(data, data.length); boolean fail = false; boolean except = false; try { removeZeros(data1); } catch (RuntimeException e) { fail = true; except = true; } removeZeros2(data2); fail = fail || !Arrays.equals(data1, data2); if (fail) { if (except) { System.out.println("FAIL with exception for " + Arrays.toString(data)); } else { System.out.println("FAIL for " + Arrays.toString(data)); } System.out.println("Correct: " + Arrays.toString(data2)); System.out.println("Theirs: " + Arrays.toString(data1)); System.out.println(); count++; if (count > 25) { System.out.println("exceeded 25 errors"); System.exit(0); } } } else { data[index] = 0; test(data, index + 1); do { data[index] = r.nextInt(); } while (data[index] == 0); test(data, index + 1); } } }