Key to CSE142 Final, Autumn 2014 handout #23 1. Expression Value --------------------------------------------------- 2 + 3 * (2 + 3) + 17 / 4 21 67 % 10 % 5 * 6 - 3 % 6 9 7.5 * 2 + 19 / 2 / 2.0 - 0.5 - 1 / 2 19.0 4 + 2 * 2 + "2 * 2" + 2 * 2 + 4 "82 * 244" 43 / 4 / 3 * 5.0 / 2 + 5 / 2 9.5 2. Original List Final List -------------------------------------------------------------- {1} {1} {2, 5, 9} {2, 7, 9} {2, 3, 7, 2} {2, 5, 2, 2} {2, 4, 5, 4, 3} {2, 6, -1, 3, 3} {5, 1, 8, 4, 8, 4} {5, 6, 2, 6, 2, 4} 3. Inheritance. The output produced is as follows. d a d b c 1 a 1 d 1 c 1 d 2 d 2 d 2 d 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 int analyzeParagraphs(Scanner input) { int max = 0; while (input.hasNextLine()) { String line = input.nextLine(); int count = 0; while (!line.equals("<p>")) { count++; line = input.nextLine(); } System.out.println(count + "-line paragraph"); if (count > max) { max = count; } } return max; } 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. One possible solution appears below. public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { int sum = list.get(i) + list.get(i + 1); list.set(i, sum); list.remove(i + 1); } } 8. Critters. One possible solution appears below. public class Counter extends Critter { private int count; private int max; public Counter(int n) { count = 1; max = n; } public Action getMove(CritterInfo info) { if (count < max) { count++; } else { count = 1; } if (info.getFront() == Neighbor.OTHER) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else return Action.LEFT; } public Color getColor() { return Color.BLUE; } public String toString() { return count + "/" + max; } } 9. Arrays. One possible solution appears below. public static int[] combineSublists(int[] list1, int[] list2, int start, int stop) { int sublistLength = stop - start; int[] result = new int[2 * sublistLength]; for (int i = start; i < stop; i++) { result[i - start] = list1[i]; result[i - start + sublistLength] = list2[i]; } return result; } 10. Programming. Two possible solutions appear below. public static void removeZeros(int[] list) { for (int i = list.length - 2; i >= 0; i--) { if (list[i] == 0) { for (int j = i; j < list.length - 1; j++) { list[j] = list[j + 1]; } list[list.length - 1] = 0; } } } public static void removeZeros(int[] list) { int numNonZero = 0; for (int i = 0; i < list.length; i++) { if (list[i] != 0) { list[numNonZero] = list[i]; numNonZero++; } } for (int i = numNonZero; i < list.length; i++) { list[i] = 0; } }
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011