// Program to test solutions to problem #5 on the cse143 midterm, winter 2016. // Fill in your solution to mirror, then compile and run the program. import java.util.*; public class Test5 { public static void mirror(Stack s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void mirror2(Stack s) { Queue q = new LinkedList(); while (!s.isEmpty()) q.add(s.pop()); while (!q.isEmpty()) s.push(q.remove()); while (!s.isEmpty()) q.add(s.pop()); for (int i = 0; i < q.size(); i++) { int n = q.remove(); s.push(n); q.add(n); } 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[] {12, 7, -4, 0, 9, 8, 7, 0, -5, 2, 1, 2}); test(new int[] {1, 2, 3}); if (failCount == 0) { System.out.println("passed all " + testCount + " tests"); } else { System.out.println("failed " + failCount + " of " + testCount + " tests"); } } public static void test(int[] data) { for (int i = 0; i <= data.length; i++) { Stack s = new Stack(); for (int j = 0; j < i; j++) { s.push(data[j]); } test(s); } } public static void test(Stack s) { Stack s2 = new Stack(); s2.addAll(s); System.out.println("original = " + s); mirror2(s); System.out.println("result = " + s); boolean fail = false; try { mirror(s2); } catch (RuntimeException e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); fail = true; } if (!fail && !s.equals(s2)) { System.out.println("yours = " + s2); fail = true; } testCount++; if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }