Solution to CSE142 Sample Final handout #27 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 -------------------------------------------------------------- (8) (8) () () (3, 0, 1, 4, 7) (3, 0, 4, 8, 19) (0, 1, 2, 3, 4, 5) (0, 1, 3, 7, 14, 26) (7, 4, -10, 8, 2) (7, 4, 1, 13, 16) 3. Polymorphism. The output produced is as follows. squid creature 1 tentacles BIG! spout creature 2 ocean-dwelling creature 1 creature 2 ocean-dwelling warm-blooded creature 2 4. Token-Based File Processing. One possible solution appears below. public static void processData(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 processFile(Scanner input) { while (input.hasNextLine()) { String text = input.nextLine(); if (text.length() == 0 || text.charAt(0) != '.') 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 void printNumber(int[] digits) { for (int i = 0; i < digits.length; i++) { if (i > 0 && (digits.length - i) % 3 == 0) System.out.print(","); System.out.print(digits[i]); } System.out.println(); } 7. ArrayLists. One possible solution appears below. public static void manyStrings(ArrayList list, int n) { for (int i = list.size() - 1; i >= 0; i--) { String target = (String)list.get(i); for (int j = 0; j < n - 1; j++) list.add(i, target); } } 8. Critters. One possible solution appears below. public class Ant implements Critter { private int move; private int direction1; private int direction2; public char getChar() { return 'A'; } public int getMove() { if (this.move % 10 == 0) if (Math.random() < 0.5) { this.direction1 = NORTH; this.direction2 = EAST; } else { this.direction1 = SOUTH; this.direction2 = WEST; } int result; if (this.move % 10 < 5) result = this.direction1; else result = this.direction2; this.move++; return result; } } 9. Arrays. One possible solution appears below. public static int[] interleave(int[] list1, int[] list2) { int[] result = new int[list1.length + list2.length]; int min = Math.min(list1.length, list2.length); for (int i = 0; i < min; i++) { result[2 * i] = list1[i]; result[2 * i + 1] = list2[i]; } for (int i = min; i < list1.length; i++) result[i + min] = list1[i]; for (int i = min; i < list2.length; i++) result[i + min] = list2[i]; return result; } 10. Programming. One possible solution appears below. public static int indexOf(int[] target, int[] list) { for (int i = 0; i < list.length - target.length; i++) { boolean ok = true; for (int j = 0; j < target.length; j++) if (list[i + j] != target[j]) ok = false; if (ok) return i; } return -1; }
Stuart Reges
Last modified: Fri Jun 3 13:39:43 PDT 2005