Key to CSE142 Final handout #24 Winter 2011 1. Expression Value --------------------------------------------------- (6 + 7) / 4 / 2.0 1.5 13 / 2 * 3 % 10 - 1 7 30 / (2 * 6) + 1.5 3.5 13 % 5 + 5 * 3 / 4 6 "3 * 4" + 3 * 4 + 10 "3 * 41210" 2. Original List Final List -------------------------------------------------------------- [12, 7] [13, 8] [2, 3, 4, 5, 6] [3, 5, 5, 5, 6] [3, 4, 5, 7, 9] [3, 5, 7, 9, 10] [2, 3, 5, 7, 9] [3, 5, 7, 9, 10] [4, 5, 9, 6, 2] [5, 7, 11, 7, 2] 3. Inheritance. The output produced is as follows. d a d d b 1 b 1 d 1 c 1 b 2 b 2 d 2 d 2 4. Token-Based File Processing. One possible solution appears below. public static void showSums(Scanner input) { int count = 0; int sum = 0; while (input.hasNextInt()) { sum += input.nextInt(); count++; System.out.println("Sum of " + count + " = " + sum); } double average = (double) sum / count; System.out.println("Average = " + average); } 5. Line-Based File Processing. One possible solution appears below. public static void underline(Scanner input) { while (input.hasNextLine()) { String text = input.nextLine(); if (!text.startsWith(".")) { System.out.println(text); } else { System.out.println(text.substring(1)); for (int i = 1; i <= text.length() - 1; i++) { System.out.print("-"); } System.out.println(); } } } 6. Arrays. One possible solution appears below. public static boolean hasAlternatingParity(int[] list) { for (int i = 0; i < list.length - 1; i++) { if (list[i] % 2 == list[i + 1] % 2) { return false; } } return true; } 7. ArrayLists. Two possible solutions appear below. public static void switchPairs(ArrayList<String> list) { for (int i = 0; i < list.size() - 1; i += 2) { String first = list.remove(i); list.add(i + 1, first); } } public static void switchPairs(ArrayList<String> list) { int i = 0; while (i < list.size() - 1) { String first = list.get(i); list.set(i, list.get(i + 1)); list.set(i + 1, first); i += 2; } } 8. Critters. One possible solution appears below. public class Chameleon extends Critter { private int count = 0; public Action getMove(CritterInfo info) { count++; if (count % 3 == 1) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else { return Action.RIGHT; } } public Color getColor() { if (count % 3 == 0) { return Color.RED; } else if (count % 3 == 1) { return Color.WHITE; } else { return Color.BLUE; } } public String toString() { if (count % 3 == 0) { return "R"; } else if (count % 3 == 1) { return "W"; } else { return "B"; } } } 9. Arrays. One possible solution appears below. public static int[] doubleSize(int[] list) { int[] result = new int[2 * list.length]; for (int i = 0; i < list.length; i++) { result[2 * i] = list[i] / 2 + list[i] % 2; result[2 * i + 1] = list[i] / 2; } return result; } 10. Programming. One possible solution appears below. 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; }
Stuart Reges
Last modified: Fri Sep 16 16:10:42 PDT 2011