// Program to test solutions to problem #6 on the cse143 midterm, winter 2022. // Fill in your solution to splitStack, then compile and run the program. Note // that it does not test whether the exception is thrown. import java.util.*; public class Test6 { public static void splitStack(Stack s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void splitStack2(Stack s) { Queue q = new LinkedList<>(); while (!s.isEmpty()) { q.add(s.pop()); } int oldSize = q.size(); for (int i = 0; i < oldSize; i++) { int n = q.remove(); if (n % 2 == 0) { q.add(n); } else { s.push(n); } } while (!q.isEmpty()) { s.push(q.remove()); } while (!s.isEmpty()) { q.add(s.pop()); } while (!q.isEmpty()) { s.push(q.remove()); } } private static int testCount; private static int failCount; public static void main(String[] args) { test(new int[] {}); test(new int[] {8}); test(new int[] {13}); test(new int[] {13, 12}); test(new int[] {80, 7}); test(new int[] {5, 7, 22}); test(new int[] {3, 5, 2}); test(new int[] {4, 2, 4}); test(new int[] {2, 4, 3, 2}); test(new int[] {8, 3, 3, 8}); test(new int[] {9, 2, 2, 7}); test(new int[] {18, 7, 7, 22}); test(new int[] {3, 2, 1, 2, 3}); test(new int[] {7, 4, 5, 4, 12}); test(new int[] {8, 12, 9, -3, 8}); test(new int[] {1, 2, 3, 1, 2}); test(new int[] {2, 4, 6, 6, 4, 2}); test(new int[] {1, 2, 3, 4, 2, 1}); test(new int[] {8, 7, 2, 9, 12, 15}); test(new int[] {12, 3, 5, 4, 3, 17}); test(new int[] {5, 8, 1, 7, 1, 5, 8}); test(new int[] {3, 2, 1, 4, 1, 1, 1}); test(new int[] {3, 2, 2, 2, 4, 4, 4}); test(new int[] {0, 3, 3, 4, 2, 3, 2}); test(new int[] {1, 4, 4, 1, 2, 1, 4, 1}); test(new int[] {1, 3, 1, 4, 1, 1, 0, 2}); test(new int[] {3, 3, 1, 2, 3, 3, 3, 4}); test(new int[] {3, 1, 3, 1, 3, 1, 2, 1, 2}); test(new int[] {4, 1, 0, 1, 2, 1, 2, 1, 1}); test(new int[] {3, 4, 3, 1, 2, 4, 3, 0, 4}); test(new int[] {1, 3, 0, 4, 3, 0, 0, 4, 2, 4}); test(new int[] {4, 1, 4, 2, 4, 0, 4, 4, 2, 1}); test(new int[] {4, 2, 2, 0, 2, 2, 4, 3, 2, 0}); test(new int[] {3, 1, 1, 1, 3, 2, 4, 3, 1, 0, 1}); test(new int[] {3, 2, 3, 1, 4, 0, 2, 3, 3, 1, 2}); test(new int[] {2, 4, 2, 3, 0, 3, 4, 0, 1, 1, 3}); test(new int[] {10, 12, 14, 16, 18, 14, 16, 18, 12, 10}); test(new int[] {5, 4, 3, 2, 1, 0, 2, 2, 3, 3, 5}); test(new int[] {19, 0, 1, 3, 2, 7, 4, 4, 9, 14, 3, 2, 0, 2}); test(new int[] {3, 2, 4, 6, 8, 10, 12, 5, 10, 10, 6, 8, 4, 2, 3}); test(new int[] {17, 78, 42, 39, 15, 17, 204, 52, 9, 4, 3, 2}); test(new int[] {8, 12, 3, 9, 50, 78, 15, 2, 4, 6, 49}); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data) { Stack s1 = new Stack(); Stack s2 = new Stack(); for (int n : data) { s1.push(n); s2.push(n); } System.out.println("initial stack = " + s1); splitStack2(s1); System.out.println("expected stack = " + s1); boolean fail = false; try { splitStack(s2); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail && !s1.equals(s2)) { System.out.println("your stack = " + s2); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } String text = s2.toString(); System.out.println(); } }