1. Array Mystery [1, -1, 2] [1, 1, 3, 2, 3, 3] [-2, -2, 1, 2, 2] [3, -1, 2, -2, -1, -6] 2. Reference Mystery 1 (1, 0) 0 (1, 0) 2 (3, 0) 1 (3, 0) 3. Inheritance Mystery kix kix 1 loops 2 loops 2 trix trix 1 trix 2 kix kix 1 trix 2 trix 2 kix kix 1 puffs 2 puffs 1 puffs 2 4. File Processing public static int darts(Scanner input) { int sum = 0; while (input.hasNext()) { if (input.hasNextInt()) { sum += input.nextInt(); } else { String multiplier = input.next(); int points = input.nextInt() if (multiplier.equals("triple")) { sum += points * 3; } else { sum += points * 2; } } } return sum; } 5. File Processing public static void stockMarket(Scanner input) { double maxDelta = 0; int bestDay = 1; int bestStock = 1; int stock = 1; while (input.hasNextLine()) { String line = input.nextLine(); Scanner lineScan = new Scanner(line); int day = 2; double price1 = lineScan.nextDouble(); while (lineScan.hasNextDouble()) { double price2 = lineScan.nextDouble(); if (price2 - price1 > maxDelta) { maxDelta = price2 - price1; bestDay = day; bestStock = stock; } price1 = price2; day++; } stock++; } System.out.println("Stock " + bestStock + " had the greatest price increase of $" + maxDelta + " on day " + bestDay); } 6. Array Programming public static boolean isPalindrome(int[] list) { for (int i = 0; i < list.length / 2; i++) { if (list[i] != list[list.length - 1 - i]) { return false; } } return true; } 7. Array Programming Two possible solutions: public static int[] weave(int[] a1, int[] a2) { int[] a3 = new int[Math.min(a1.length, a2.length) * 2]; for (int i = 0; i < a3.length; i++) { if (i % 2 == 0) { a3[i] = a1[i / 2]; } else { a3[i] = a2[i / 2]; } } return a3; } public static int[] weave(int[] a1, int[] a2) { int minLength = Math.min(a1.length, a2.length); int[] a3 = new int[minLength * 2]; for (int i = 0; i < minLength; i++) { a3[2 * i] = a1[i]; a3[2 * i + 1] = a2[i]; } return a3; } 8. Critters public class CatDog extends Critter { private int percentDog; private Random r; private int steps; public CatDog(int percentDog) { this.percentDog = percentDog; steps = percentDog; r = new Random(); } public boolean eat() { int num = r.nextInt(100); if (num < percentDog) { steps = 0; return true; } return false; } public Direction getMove() { steps++; if (steps <= percentDog) { // in dog mode if (steps % 8 == 1 || steps % 8 == 2) { return Direction.EAST; } else if (steps % 8 == 3 || steps % 8 == 4) { return Direction.SOUTH; } else if (steps % 8 == 5 || steps % 8 == 6) { return Direction.WEST; } } return Direction.NORTH; } } 9. Classes and Objects Two possible solutions: public boolean transferTo(BankAccount other, double amount) { if (getBalance() < amount) { return false; } other.deposit(amount); withdraw(amount); return true; } public boolean transferTo(BankAccount other, double amount) { if (balance < amount) { return false; } other.balance += amount; other.transactions++; balance -= amount; transactions++; return true; } 10. Array Programming One possible solution: public static int[] removeDuplicates(int[] a) { int duplicates = 0; for (int i = 0; i < a.length - 1; i++) { if (a[i] == a[i + 1]) { duplicates++; } } int[] result = new int[a.length - duplicates]; if (a.length > 0) { // Copy over the first value from the set of duplicates result[0] = a[0]; int resultIndex = 1; // Loop over parameter array a and sometimes copy over the value for (int aIndex = 1; aIndex < a.length; aIndex++) { if (a[aIndex] != a[aIndex - 1]) { // copy from a to result result[resultIndex] = a[aIndex]; resultIndex++; } } } return result; }