// Program to test solutions to problem #4 on the cse143 midterm, spring 2018. // The methods code1/code2/code3/code4 have the sample solution to the four // coding problems. Replace the code with your own code to test it. public class Test4 { public static void code1() { p = q.next.next; q.next.next = null; } public static void code2() { q.next.next = q; q = q.next; q.next.next = null; } public static void code3() { q.next.next = p.next; p.next = q; q = p; p = q.next.next; q.next.next = null; } public static void code4() { p.next.next.next = p; q.next.next = p.next.next; p.next.next = q; q = q.next; p = p.next; p.next.next = null; q.next.next.next = null; } public static void main(String[] args) { passCount = 0; problem1(); problem2(); problem3(); problem4(); System.out.println("Passed " + passCount + " of 4 tests"); } private static ListNode p, q; private static int passCount; public static void problem1() { p = null; q = new ListNode(1, new ListNode(2, new ListNode(3))); doProblem(1, "p->[3]", "q->[1]->[2]"); } public static void problem2() { p = new ListNode(1); q = new ListNode(2, new ListNode(3)); doProblem(2, "p->[1]", "q->[3]->[2]"); } public static void problem3() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4)); doProblem(3, "p->[4]->[2]", "q->[1]->[3]"); } public static void problem4() { p = new ListNode(1, new ListNode(2, new ListNode(3))); q = new ListNode(4, new ListNode(5)); doProblem(4, "p->[2]->[4]", "q->[5]->[3]->[1]"); } public static void doProblem(int n, String answer1, String answer2) { System.out.println("Problem #" + n + ":"); System.out.println("before:"); System.out.println(" " + text(p, "p")); System.out.println(" " + text(q, "q")); boolean threwException = false; try { if (n == 1) { code1(); } else if (n == 2) { code2(); } else if (n == 3) { code3(); } else if (n == 4) { code4(); } else { System.out.println("unknown problem number"); } } catch (Exception e) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println("threw " + e + " at line #" + line); threwException = true; } String text1 = text(p, "p"); String text2 = text(q, "q"); int correct = 0; int nulls = 0; System.out.println("after:"); if (text1.equals(answer1)) { correct++; System.out.println(" " + text1); } else { System.out.println(" " + text1 + " (should be " + answer1 + ")"); if (text1.startsWith(answer1)) { nulls++; } } if (text2.equals(answer2)) { correct++; System.out.println(" " + text2); } else { System.out.println(" " + text2 + " (should be " + answer2 + ")"); if (text2.startsWith(answer2)) { nulls++; } } if (correct == 2 && !threwException) { System.out.println("PASS"); passCount++; } else if (correct + nulls == 2) { System.out.print("PARTIAL PASS"); if (nulls > 0) { System.out.print(" with " + nulls + " null assignment(s)"); } if (threwException) { System.out.print(" eliminating exception code"); } System.out.println(); } else { System.out.println("FAILED"); } System.out.println(); } public static String text(ListNode p, String text) { String result = text; for (int i = 0; i < 5; i++) { if (p != null) { result += "->[" + p.data + "]"; p = p.next; } } if (p != null) { result += "..."; } return result; } } class ListNode { public int data; // data stored in this node public ListNode next; // link to next node in the list // post: constructs a node with data 0 and null link public ListNode() { this(0, null); } // post: constructs a node with given data and null link public ListNode(int data) { this(data, null); } // post: constructs a node with given data and given link public ListNode(int data, ListNode next) { this.data = data; this.next = next; } }