// Program to test solutions to problem #9 on the cse143 final, autumn // 2017. This is the version we used with the TAs for grading. It provides a // detailed listing of points for each of the cases we graded. Don't forget to // remove the size method for the student version. import java.util.*; class LinkedIntList { public void switchPairsOfPairs() { // fill in your solution here } private ListNode front; // first value in the list // this is the sample solution public void switchPairsOfPairs2() { if (front != null && front.next != null && front.next.next != null && front.next.next.next != null) { ListNode current = front.next.next; front.next.next = current.next.next; current.next.next = front; front = current; current = current.next.next.next; while (current.next != null && current.next.next != null && current.next.next.next != null && current.next.next.next.next != null) { ListNode temp = current.next.next.next; current.next.next.next = temp.next.next; temp.next.next = current.next; current.next = temp; current = temp.next.next.next; } } } // post: constructs an empty list public LinkedIntList() { front = null; } public int size() { int count = 0; ListNode current = front; while (current != null) { current = current.next; count++; } return count; } public boolean equals(Object o) { LinkedIntList other; try { other = (LinkedIntList) o; } catch (Exception e) { return false; } if (other == null) { return false; } ListNode current1 = front; ListNode current2 = other.front; while (current1 != null && current2 != null) { if (current1.data != current2.data) return false; current1 = current1.next; current2 = current2.next; } return current1 == null && current2 == null; } // post: creates a comma-separated, bracketed version of the list public String toString() { if (front == null) return "[]"; else { String result = "[" + front.data; ListNode current = front.next; while (current != null) { result += ", " + current.data; current = current.next; } result += "]"; return result; } } // post: creates a comma-separated, bracketed version of the list public String toString(int number) { if (front == null) return "[]"; else { String result = "[" + front.data; int count = 1; ListNode current = front.next; while (current != null && count < number) { result += ", " + current.data; current = current.next; count++; } if (current != null) { result += "..."; } result += "]"; return result; } } // post: appends the given value to the end of the list public void add(int value) { if (front == null) front = new ListNode(value); else { ListNode current = front; while (current.next != null) current = current.next; current.next = new ListNode(value); } } } public class TestQ8 { private static int[] cases = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 16, 20}; // kept this as doubles but reformatted printf for future flexibility private static double[] points = {1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; private static double[] works = new double[cases.length]; private static int count = 0; // test counter for arrays private static int totalFail = 0; public static boolean usedSize; @SuppressWarnings("deprecation") public static void main(String[] args) { for (int i = 1; i <= cases.length; i++) { System.out.println("case #" + i); Runner run = new Runner(i); run.start(); long slept = 0; try { while(slept < 500 && !run.isFinished()) { Thread.sleep(100); slept += 100; } } catch (InterruptedException e) { System.out.println("something went wrong with thread sleep"); } if(!run.isFinished()) { run.interrupt(); run.stop(); System.out.println("timeout...failed"); System.out.println(); count++; totalFail++; } } double sum = 0; if (totalFail > 0) { System.out.println("failed " + totalFail + " of " + count + " tests"); System.out.println(); double possible = 0; for (int i = 1; i <= cases.length; i++) { if (i == 5) { // subtotal for basic cases System.out.println("------------------"); System.out.printf("%-10s %2.0f / %2.0f\n", "total", sum, possible); System.out.println("Requires test for these cases to earn points"); System.out.println(); sum = 0; possible = 0; } String len = "length " + cases[i - 1]; System.out.printf("%-10s %2.0f / %2.0f", len, works[i - 1], points[i - 1]); System.out.println(); sum += works[i - 1]; possible += points[i - 1]; } System.out.println("------------------"); System.out.printf("%-9s %3.0f /%3.0f\n", "total", sum, possible); } else { System.out.println("passed all tests"); } } private static class Runner extends Thread { private boolean include; private int number; private boolean finished; public Runner(int number) { this.number = number; this.finished = false; } public void run() { int index = number - 1; // list1 for testing sample, list2 for their code LinkedIntList list1 = new LinkedIntList(); LinkedIntList list2 = new LinkedIntList(); for (int i = 0; i < cases[index]; i++) { list1.add(i); list2.add(i); } boolean fail = false; System.out.println("initial list = " + list1); list1.switchPairsOfPairs2(); System.out.println("expected list = " + list1); try { list2.switchPairsOfPairs(); } catch (RuntimeException e) { if(e.getStackTrace().length > 0) { int line = e.getStackTrace()[0].getLineNumber(); System.out.println(" threw " + e + " at line #" + line); } fail = true; } if (!fail && !list1.equals(list2)) { System.out.println("their list = " + list2.toString(cases[index])); fail = true; } if (fail) { System.out.println("failed"); totalFail++; } else { System.out.println("passed"); works[count] = points[count]; } System.out.println(); count++; // increment test counter finished = true; } public boolean isFinished() { return finished; } } } class ListNode { public final 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; } }