Key to CSE142 Final, Summer 2012 1. Array Mystery ----------------------------------------------- [42, 84, 42] [6, 10, 12, 2] [7, 7, 15, 16, 1] [4, 7, 9, 10, 10, 0] [6, 5, -1, 4, 4, 1, -3] 2. Inheritance Mystery ----------------------------------------------- Leela2 We're doomed! Leela1 Bender2 Bender1 We're doomed! Bender1 Bender2 Farnsworth1 Good news everyone! Farnsworth1 Fry2 We're doomed! Bender1 3. File Processing - 2 solutions shown --------------------------------------- public static boolean printConsecutive(Scanner input) { String last = null; boolean result = false; if (input.hasNext()) { last = input.next(); } while (input.hasNext()) { String cur = input.next(); if (cur.equalsIgnoreCase(last)) { System.out.print(cur + " "); result = true; } last = cur; } return result; } public static boolean printConsecutive(Scanner input) { String last = null; boolean result = false; while (input.hasNext()) { if (last == null) { last = input.next(); } else { String cur = input.next(); if (cur.equalsIgnoreCase(last)) { System.out.print(cur + " "); result = true; } last = cur; } } return result; } 4. Dog (Critters) -------------------------------------- public class Dog extends Critter { private int totalSteps; private int curMove; // 0 NORTH, 1 WEST, 2 SOUTH, 3 EAST private int steps; public Dog() { totalSteps = 1; steps = 0; curMove = 0; } public Direction getMove() { if (steps == totalSteps) { steps = 0; totalSteps++; curMove = (curMove + 1) % 4; } steps++; if (curMove == 0) { return Direction.NORTH; } else if (curMove == 1) { return Direction.WEST; } else if (curMove == 2) { return Direction.SOUTH; } else { // curMove == 3 return Direction.EAST; } } public Attack fight(String opponent) { curMove = 0; totalSteps = 1; steps = 0; if (opponent.equals("%")) { return Attack.ROAR; } else { return Attack.SCRATCH; } } } 5. Reference Mystery -------------------------------------- [w:20 h:40] 2 40 [w:20 h:40] 2 10 [w:60 h:10] 3 10 [w:60 h:10] 2 3 6. Random Programming - 2 solutions shown -------------------------------------- public static void threeSame(Random r) { int count = 1; int last = -1; while (count < 3) { int c = r.nextInt(2); if (c == 0) { System.out.print("H "); } else { System.out.print("T "); } if (last == c) { count++; } else { count = 1; } last = c; } } public static void threeSame(Random r) { int hCount = 0; int tCount = 0; while (hCount < 3 && tCount < 3) { if (r.nextBoolean()) { hCount++; tCount = 0; System.out.print("H "); } else { tCount++; hCount = 0; System.out.print("T "); } } } 7. Array Programming - 4 solutions shown -------------------------------------- public static void swapHalves(String[] a) { for (int i = 0; i < a.length / 2; i++) { String temp = a[i]; a[i] = a[a.length / 2 + a.length % 2 + i]; a[a.length / 2 + a.length % 2 + i] = temp; } } public static void swapHalves(String[] a) { for (int i = 0; i < a.length / 2; i++) { String temp = a[i]; a[i] = a[(a.length + 1) / 2 + i]; a[(a.length + 1) / 2 + i] = temp; } } public static void swapHalves(String[] a) { int high = a.length / 2; if (a.length % 2 == 1) high++; for (int i = 0; i < a.length / 2; i++, high++) { String temp = a[high]; a[high] = a[i]; a[i] = temp; } } public static void swapHalves(String[] a) { if (a.length % 2 == 0) { for (int i = 0; i < a.length / 2; i++) { String temp = a[i]; a[i] = a[a.length / 2 + i]; a[a.length / 2 + i] = temp; } } else { for (int i = 0; i < a.length / 2; i++) { String temp = a[i]; a[i] = a[a.length / 2 + i + 1]; a[a.length / 2 + i + 1] = temp; } } } 8. Array Programming (Hard) - 3 solutions shown -------------------------------------- public static int[] merge(int[] a1, int[]a2) { int[] merged = new int[a1.length + a2.length]; int loc1 = 0; int loc2 = 0; for (int i = 0; i < a1.length; i++) { while ((loc1 < a2.length) && (a2[loc1] < a1[i])) { merged[loc2] = a2[loc1]; loc1++; loc2++; } merged[loc2] = a1[i]; loc2++; } for (int i = loc1; i < a2.length; i++) { merged[loc2] = a2[i]; loc2++; } return merged; } public static int[] merge(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; int i1 = 0; int i2 = 0; for (int i = 0; i < result.length; i++) { if (i2 >= a2.length || (i1 < a1.length && a1[i1] <= a2[i2])) { result[i] = a1[i1]; i1++; } else { result[i] = a2[i2]; i2++; } } return result; } public static int[] merge(int[] a1, int[] a2) { int[] result = new int[a1.length + a2.length]; int i1 = 0, i2 = 0, i3 = 0; while (i1 < a1.length && i2 < a2.length) { if (a1[i1] < a2[i2]) { result[i3] = a1[i1]; i1++; } else { result[i3] = a2[i2]; i2++; } i3++; } while (i1 < a1.length) { result[i3] = a1[i1]; i1++; i3++; } while (i2 < a2.length) { result[i3] = a2[i2]; i2++; i3++; } return result; }