// Program to test solutions to problem #5 on the cse143 midterm, summer 2013. public class Test5 { public static void code1() { // fill in your code for the first problem } public static void code2() { // fill in your code for the second problem } public static void code3() { // fill in your code for the third problem } public static void code4() { // fill in your code for the fourth problem } public static void main(String[] args) { problem1(); problem2(); problem3(); problem4(); } private static ListNode p, q; public static void problem1() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3); doProblem(1, "p->[1]->[3]", "q->[2]"); } public static void problem2() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4)); doProblem(2, "p->[1]->[3]", "q->[4]->[2]"); } public static void problem3() { p = new ListNode(1, new ListNode(2, new ListNode(3))); q = null; doProblem(3, "p->[3]->[2]", "q->[1]"); } 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]->[1]->[4]", "q->[3]->[5]"); } 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"); } 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; } }