// Program to test solutions to problem #3 on the cse143 midterm, winter 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 Test3 { // Type Student code here public static void code1() { } public static void code2() { } public static void code3() { } public static void code4() { } public static void main(String[] args) { passCount = 0; problem1(); problem2(); problem3(); problem4(); } private static ListNode p, q; private static int passCount; public static void problem1() { p = new ListNode(1); q = new ListNode(2, new ListNode(3)); doProblem(1, "p", "q->[2]->[3]->[1]"); } public static void problem2() { p = new ListNode(1, new ListNode(2, new ListNode(3))); q = null; doProblem(2, "p->[2]->[3]", "q->[1]"); } public static void problem3() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4)); doProblem(3, "p->[2]", "q->[4]->[3]->[1]"); } public static void problem4() { p = new ListNode(1, new ListNode(2)); q = new ListNode(3, new ListNode(4, new ListNode(5))); doProblem(4, "p->[4]->[2]->[3]", "q->[5]->[1]"); } public static void doProblem(int n, String answer1, String answer2) { System.out.println("Problem #" + n + ":"); String pre1 = text(p, "p"); String pre2 = text(q, "q"); String exception = null; 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(); exception = "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("before: | after:"); System.out.printf(" %-23s", pre1); if (text1.equals(answer1)) { correct++; System.out.println("| " + text1); } else { System.out.println("| " + text1 + " (should be " + answer1 + ")"); if (text1.startsWith(answer1)) { nulls++; } } System.out.printf(" %-23s", pre2); if (text2.equals(answer2)) { correct++; System.out.println("| " + text2); } else { System.out.println("| " + text2 + " (should be " + answer2 + ")"); if (text2.startsWith(answer2)) { nulls++; } } if (exception != null) { System.out.println(exception); } 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.n; } } if (p != null) { result += "..."; } return result; } } class ListNode { public int data; // data stored in this node public ListNode n; // link to n 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 n) { this.data = data; this.n = n; } }