Key to CSE142 Final, Autumn 2017 1. Reference Mystery. The program produces the following output. 3 [2, 3, 7] 1 [2, 3, 7] 17 7 2. Original List Final List -------------------------------------------------------------- {42} {42} {8, 2, 3} {8, 11, 3} {14, 7, 5, 6} {14, 19, 25, 6} {1, 0, 1, 0, 1, 0} {1, 2, 2, 3, 3, 0} {10, 1, 4, 2, 6, 8, 1} {10, 14, 16, 22, 30, 31, 1} 3. Inheritance. The output produced is as follows. b d d b b 1 a 1 b 1 b 1 c 2 a 2 c 2 b 2 4. Token-Based File Processing. One possible solution appears below. public static void redact(Scanner input) { while (input.hasNext()) { String next = input.next(); if (next.equals("CLASSIFIED")) { int count = input.nextInt(); for (int i = 0; i < count; i++) { System.out.println("[redacted]"); input.next(); } } else { System.out.println(next); } } } 5. Line-Based File Processing. One possible solution appears below. public static void diff(Scanner input1, Scanner input2) { int line = 0; int count = 0; while (input1.hasNextLine()) { line++; String line1 = input1.nextLine(); String line2 = input2.nextLine(); if (!line1.equalsIgnoreCase(line2)) { count++; System.out.println("DIFF: difference found on line #" + line); System.out.println("[" + line1 + "] versus [" + line2 + "]"); } } if (count > 0) { System.out.println(); System.out.println(count + " line(s) are different"); } } 6. Arrays. One possible solution appears below. public static void switchPairs(int[] list) { for (int i = 0; i < list.length - 1; i += 2) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } 7. ArrayLists. One possible solution appears below. public static void negativesToFront(ArrayList list) { int nextSpot = 0; for (int i = 0; i < list.size(); i++) { if (list.get(i) < 0) { int n = list.remove(i); list.add(nextSpot, n); nextSpot++; } } } 8. Critters. One possible solution appears below. public class WatchDog extends Critter { private int moveCounter; private Direction dir; private boolean panic; public Direction getMove() { moveCounter++; if (panic) { return dir; } else { if (moveCounter % 24 <= 10) { return Direction.WEST; } else if (moveCounter % 24 <= 12) { return Direction.CENTER; } else if (moveCounter % 24 <= 22) { return Direction.EAST; } else { return Direction.CENTER; } } } public Attack fight(String opponent) { if (opponent.equals("%")) { panic = true; Random r = new Random(); int direct = r.nextInt(); if (direct == 0) { dir = Direction.NORTH; } else if (direct == 1) { dir = Direction.WEST; } else if (direct == 2) { dir = Direction.SOUTH; } else if (direct == 3) { dir = Direction.EAST; } } return Attack.ROAR; } } 9. Arrays. One possible solution appears below. public static int[] expand(int[] data) { int len = 0; for (int i = 0; i < data.length / 2; i++) { len = len + data[2 * i]; } int[] result = new int[len]; int index = 0; for (int i = 0; i < data.length / 2; i++) { int times = data[2 * i]; int n = data[2 * i + 1]; for (int j = 0; j < times; j++) { result[index] = n; index++; } } return result; } 10. Programming. One possible solution appears below. public static void compress(ArrayList words) { int index = 0; while (index < words.size()) { int count = 1; while (index + 1 < words.size() && words.get(index).equals(words.get(index + 1))) { words.remove(index); count++; } if (count > 1) { words.set(index, count + "*" + words.get(index)); } index++; } }