Key to CSE142 Final, Spring 2012 1. Original List Final list --------------------------------------- {3, 2}; [4, 2] {3, 5, 4}; [4, 6, 4] {1, 2, 4, 8}; [2, 2, 5, 8] {1, 5, 3, 2, 1}; [2, 6, 4, 2, 1] 2. Reference Mystery. The program produces the following output: [w:20 h:40] 2 40 [w:20 h:40] 2 10 [w:40 h:10] 2 10 [w:40 h:10] 2 3 3. Inheritance Mystery. The program produces the following output: Dasher1 Vixen2 Dasher2 Vixen Vixen2 Dasher2 Prancer1 Vixen2 Dasher2 Vixen Dasher1 Dasher2 Dasher Rudolf2 Prancer1 Rudolf2 Rudolf 4. Token-based processing public static void filter(Scanner input, int min, int max) { int count = 0; int total = 0; while (input.hasNext()) { String label = input.next(); int value = input.nextInt(); if (value >= min && value <= max) { System.out.println(label + " " + value); count++; total += value; } } System.out.println("Average: " + (double)total/count); } 5. Line-based processing public static int capitalize(Scanner input) { int count = 0; boolean capital = false; while (input.hasNextLine()) { String line = input.nextLine(); if (line.equals("-")) { capital = !capital; } else if (capital) { System.out.println(line.toUpperCase()); } else { System.out.println(line); } Scanner lineScan = new Scanner(line); while (capital && lineScan.hasNext()) { String word = lineScan.next(); String capitalWord = word.toUpperCase(); if (!word.equals(capitalWord)) { // doesn't count words that were already capitalized (not needed but ok) count++; } } } return count; } public static int capitalize(Scanner input) { int dash = 0; int capsWords = 0; while (input.hasNextLine()) { String line = input.nextLine(); if (line.equals("-")) { dash++; } else if (dash % 2 == 1) { Scanner lineScan = new Scanner(line); while (lineScan.hasNext()) { capsWords++; lineScan.next(); } System.out.println(line.toUpperCase()); } else { System.out.println(line); } } return capsWords; } public static int capitalize(Scanner input) { int count = 0; while (input.hasNextLine()) { String line = input.nextLine(); if (!line.equals("-")) { System.out.println(line); } else { line = input.nextLine(); while (!line.equals("-")) { Scanner lineScan = new Scanner(line); while (lineScan.hasNext()) { lineScan.next(); count++; } line = line.toUpperCase(); System.out.println(line); line = input.nextLine(); } } } return count; } 6. Array programming public static boolean sameGap(int[] list) { if (list.length >= 2) { int gap = Math.abs(list[0] - list[1]); for (int i = 2; i < list.length; i++) { int gap2 = Math.abs(list[i] - list[i - 1]); if (gap != gap2) { return false; } } } return true; } public static boolean sameGap(int[] list) { for (int i = 0; i < list.length - 2; i++) { int gap1 = Math.abs(list[i] - list[i + 1]); int gap2 = Math.abs(list[i + 1] - list[i + 2]); if (gap1 != gap2) { return false; } } return true; } 7. Critters public class Cheetah extends Critter { private boolean goingSouth; private int foodNeeded; private int foodEaten; public Cheetah(int foodNeeded) { this.foodNeeded = foodNeeded; Random r = new Random(); int choice = r.nextInt(2); if (choice == 0) { goingSouth = true; } else { goingSouth = false; } } public Direction getMove() { if (goingSouth) { return Direction.SOUTH; } else { return Direction.EAST; } } public boolean eat() { goingSouth = !goingSouth; foodEaten++; return true; } public Attack fight(String opponent) { if (foodEaten < foodNeeded) { return Attack.FORFEIT; } else { foodEaten = 0; return Attack.POUNCE; } } } 8. Classes and objects public int minutesToNoon() { ClockTime temp = new ClockTime(hour, minute, amPm); ClockTime noon = new ClockTime(12, 0, "pm"); int minutes = 0; while (!temp.equals(noon)) { temp.advance(1); minutes++; } return minutes; } public int minutesToNoon() { if (amPm.equalsIgnoreCase("AM")) { if (hour == 12) { return 11 * 60 + 60 - minute; } //return (12 - hour) * 60 - minute; return (11 - hour) * 60 + (60 - minute); } else { if (hour == 12) { return minute == 0 ? 0 : 1440 - minute; } //return (24 - hour) * 60 - minute; return (23 - hour) * 60 + (60 - minute); } } public int minutesToNoon() { int minutes = 0; if (amPm.equalsIgnoreCase("am")) { if (hour != 12) { minutes += (60 * (11 - hour)); } else { minutes += 11 * 60; } } else { if (hour != 12) { minutes += (60 * (12 + 11 - hour)); } else { if (minute != 0) { minutes += 23 * 60; } else { minutes = -60; // hack to get 12:00pm to work } } } minutes += 60 - minute; return minutes; } public int minutesToNoon() { if (hour == 12 && minute == 0 && amPm.equals("PM")) { return 0; } int hours = 11 - getHour(); if (getHour() == 12) { hours = 11; } int min = 60 - getMinute(); if (getAmPm().equals("PM")) { return (hours * 60) + min + 720; } else { return (hours * 60) + min; } } 9. Array programming public static boolean hasTwoPair(int[] data) { int[] counts = new int[6]; for (int i = 0; i < data.length; i++) { counts[data[i] - 1]++; } int num = 0; for (int i = 0; i < counts.length; i++) { if (counts[i] == 2) { num++; } } return num == 2; }