Key to CSE142 Final, Winter 2019 handout #22 1. Reference Mystery. The program produces the following output. 3 [3, 6, 8] 7 [0, 3, 8] 7 7 2. Original List Final List -------------------------------------------------------------- {0, 2, 3} {0, 2, 5} {1, 1, 4, 5} {1, 1, 3, 7} {3, 1, 2, 9, 5} {3, 1, 3, 7, 5} {5, 1, 1, 1, 8} {5, 1, 3, 7, 8} {2, 1, 1, 2, 3, 4} {2, 1, 3, 7, 3, 7} 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 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 reportBlankLines(Scanner input) { int line = 0; int count = 0; while (input.hasNextLine()) { String text = input.nextLine(); line++; if (text.length() == 0) { System.out.println("line " + line + " is blank"); count++; } } System.out.println("total blank lines = " + count); } 6. Arrays. One possible solution appears below. public static void switchPairs(int[] list) { for (int i = 0; i < list.length - 1; i += 2) { int temp = list[i]; list[i] = list[i + 1]; list[i + 1] = temp; } } 7. ArrayLists. One possible solution appears below. public static void removeAdjacentMatches(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { if (list.get(i) == list.get(i + 1)) { list.remove(i + 1); i--; } } } 8. Critters. One possible solution appears below. public class Iguana extends Critter { private Random r; private String display; private int count; public Iguana() { r = new Random(); display = "<->"; } public Action getMove(CritterInfo info) { count++; if (info.getFront() == Neighbor.OTHER) { display = "<I>"; return Action.INFECT; } else { int flip = r.nextInt(2); if (flip == 0) { display = "<L>"; return Action.LEFT; } else { display = "<R>"; return Action.RIGHT; } } } public Color getColor() { if (count % 2 == 0) { return Color.BLUE; } else { return Color.RED; } } public String toString() { return display; } } 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: Thu Jun 16 11:49:53 PDT 2022