Key to CSE142 Final, Autumn 2013 handout #25 1. Expression Value --------------------------------------------------- 43 % 15 / 3 + 15 / 2 11 6.2 * 5 / 10 + 3.5 6.6 6 * 2.5 / 4 + (2.3 + 2.7) / 4 5.0 "18" + 3 * 4 + (8 + 5) "181213" 59 % 10 / (2 + 2) * 2.5 / 2 2.5 2. Original List Final List -------------------------------------------------------------- {8, 3} {9, 3} {2, 5, 4} {3, 5, 4} {3, 4, 5, 7} {3, 5, 5, 7} {2, 5, 4, 2} {3, 5, 5, 2} {4, 5, 7, 9, 3} {5, 5, 7, 9, 3} 3. Inheritance. The output produced is as follows. b b c b c 1 c 1 c 1 d 1 a 2 b 2 c 2 b 2 4. Token-Based File Processing. One possible solution appears below. public static void switchData(Scanner input) { String label = input.next(); System.out.print(label); while (input.hasNextInt()) { int n1 = input.nextInt(); if (input.hasNextInt()) { int n2 = input.nextInt(); System.out.print(" " + n2); } System.out.print(" " + n1); } System.out.println(); } 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 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. One possible solution appears below. public static void expand(ArrayList<Integer> list) { int i = 0; while (i < list.size()) { int times = list.remove(i); int n = list.remove(i); for (int j = 0; j < times; j++) { list.add(i, n); } i += times; } } 8. Critters. One possible solution appears below. public class Salmon extends Critter { private Random r; private String display; public Salmon() { r = new Random(); display = "<*>"; } public Action getMove(CritterInfo info) { if (info.getFront() == Neighbor.OTHER) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else if (r.nextInt(2) == 0) { display = "<L>"; return Action.LEFT; } else { display = "<R>"; return Action.RIGHT; } } public Color getColor() { return Color.PINK; } public String toString() { return display; } } 9. Arrays. One possible solution appears below. public static int[] insert(int[] list1, int[] list2, int index) { int[] result = new int[list1.length + list2.length]; for (int i = 0; i < index; i++) { result[i] = list1[i]; } for (int i = 0; i < list2.length; i++) { result[index + i] = list2[i]; } for (int i = index; i < list1.length; i++) { result[list2.length + i] = list1[i]; } return result; } 10. Programming. One possible solution appears below. public static String encode(String s, int n) { String result = ""; for (int j = 0; j < n; j++) { for (int i = 0; i < s.length() - j; i += n) { result += s.charAt(i + j); } } return result; }
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011