// 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 TestQ3 { // Type your solutions here, one for each problem 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 = list(1, 2, 3); q = list(); doProblem(1, "p->[1]->[2]", "q->[3]"); } public static void problem2() { p = list(1, 3); q = list(2); doProblem(2, "p->[1]->[2]->[3]", "q"); } public static void problem3() { p = list(1, 2); q = list(3, 4); doProblem(3, "p->[2]->[4]", "q->[3]->[1]"); } public static void problem4() { p = list(1, 2, 3); q = list(4, 5); doProblem(4, "p->[2]->[1]->[4]", "q->[3]->[5]"); } public static ListNode list(int... data) { if (data.length == 0) { return null; } else { ListNode result = new ListNode(data[data.length - 1]); for (int i = data.length - 2; i >= 0; i--) { result = new ListNode(data[i], result); } return result; } } 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.next; } } if (p != null) { result += "..."; } return result; } } class ListNode { public int data; // data stored in this node public ListNode next; // 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 next) { this.data = data; this.next = next; } }