Key to CSE142 Final handout #22 Winter 2009 1. Expression Value --------------------------------------------------- 8.0/4/4 + 3/4 + 0.5 * 3 2.0 88 % 10 % 5/2 + 7 % 8 8 2 * 3 + 4 + " = " + 5 * 6 + 7 "10 = 307" 54/5/2.0/4 * 3 3.75 2 * (0.75 + 1.25)/5 * 10 - 7/4 7.0 2. Original List Final List -------------------------------------------------------------- {6, 3} {7, 3} {2, 4, 6} {3, 3, 6} {1, 2, 3, 4} {2, 1, 4, 4} {2, 2, 2, 2, 2} {3, 1, 3, 1, 2} {7, 3, 2, 0, 5, 1} {8, 2, 3, -1, 6, 1} 3. Polymorphism. The output produced is as follows. b c 1 b 2 a c 1 d 2 d c 1 d 2 b b 1 b 2 4. Token-Based File Processing. One possible solution appears below. public static double evaluate(Scanner input) { double result = input.nextDouble(); while (input.hasNext()) { String operator = input.next(); double num = input.nextDouble(); if (operator.equals("+")) { result += num; } else { // operator.equals("-") result -= num; } } return result; } 5. Line-Based File Processing. One possible solution appears below. public static void processScores(Scanner input) { while (input.hasNextLine()) { String name = input.nextLine(); String data = input.nextLine(); int plus = 0; int count = 0; for (int i = 0; i < data.length(); i++) { count++; if (data.charAt(i) == '+') { plus++; } } double percent = 100.0 * plus / count; System.out.println(name + ": " + percent + "% plus"); } } 6. Arrays. One possible solution appears below. public static boolean isFibLike(int[] list) { for (int i = 2; i < list.length; i++) { if (list[i] != list[i - 2] + list[i - 1]) { return false; } } return true; } 7. ArrayLists. Two possible solutions appear below. public static void reverse3(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 2; i += 3) { int n1 = list.get(i); int n3 = list.get(i + 2); list.set(i, n3); list.set(i + 2, n1); } } public static void reverse3(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 2; i += 3) { list.add(i, list.remove(i + 2)); list.add(i + 2, list.remove(i + 1)); } } 8. Critters. One possible solution appears below. public class Orca extends Critter { int count; public Orca() { count = 0; } public Action getMove(CritterInfo info) { if (count % 6 == 4 || count % 6 == 5) { count++; return Action.LEFT; } else if (info.getFront() != Neighbor.EMPTY) { return Action.INFECT; } else { count++; return Action.HOP; } } public String toString() { if (count % 6 < 4) { return "M"; } else { return "T"; } } } 9. Arrays. One possible solution appears below. public static int[] append(int[] list1, int[] list2) { int[] result = new int[list1.length + list2.length]; for (int i = 0; i < list1.length; i++) { result[i] = list1[i]; } for (int j = 0; j < list2.length; j++) { result[list1.length + j] = list2[j]; } return result; } 10. Programming. One possible solution appears below. public static String acronym(String s) { boolean inWord = false; s = s.toUpperCase(); String result = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == ' ' || ch == '-') { inWord = false; } else if (!inWord) { inWord = true; result += ch; } } return result; }
Stuart Reges
Last modified: Fri Sep 16 16:10:08 PDT 2011