Solution to CSE142 Final handout #32 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 -------------------------------------------------------------- (5) (5) (4, 7) (11, 7) (2, 3, 4) (5, 7, 4) (2, 4, 6, 8) (6, 10, 14, 8) (9, 7, 5, 3, 1) (16, 12, 8, 4, 1) 3. Polymorphism. The output produced is as follows. d b 1 b 2 a b 1 b 2 d d 1 d 2 d c 1 d 2 4. Token-Based File Processing. One possible solution appears below. public static void reportRunningSum(Scanner input) { double sum = input.nextDouble(); double max = sum; System.out.print("running sum = " + sum); while (input.hasNextDouble()) { sum += input.nextDouble(); System.out.print(" " + sum); if (sum > max) { max = sum; } } System.out.println(); System.out.println("max sum = " + max); } 5. Line-Based File Processing. One possible solution appears below. public static void listBlankLines(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 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. Two possible solutions appear below. public static void switchPairs(ArrayList<String> list) { for (int i = 0; i < list.size() - 1; i += 2) { String first = list.remove(i); list.add(i + 1, first); } } public static void switchPairs(ArrayList<String> list) { int i = 0; while (i < list.size() - 1) { String first = list.get(i); list.set(i, list.get(i + 1)); list.set(i + 1, first); i += 2; } } 8. Critters. One possible solution appears below. public class Yak implements Critter { private int move; private int direction; private int max; public char getChar() { return 'Y'; } public int getMove(int x, int y) { if (this.move == this.max) { this.move = 0; this.max++; double n = Math.random(); if (n < 0.25) { this.direction = NORTH; } else if (n < 0.5) { this.direction = SOUTH; } else if (n < 0.75) { this.direction = EAST; } else { // n > 0.75 this.direction = WEST; } } this.move++; return this.direction; } } 9. Arrays. 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 removeZeros2(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; } } The following solution would receive 10 out of 15 points. public static void removeZeros(int[] list) { int[] temp = new int[list.length]; int count = 0; for (int i = 0; i < list.length; i++) { if (list[i] != 0) { temp[count] = list[i]; count++; } } for (int i = 0; i < list.length; i++) { list[i] = temp[i]; } } 10. Programming. Two possible solutions appear below. public static void rearrange(int[] list) { int count = 0; for (int i = 0; i < list.length; i++) { if (list[i] % 3 == 0) { swap(list, count, i); count++; } } for (int i = count; i < list.length; i++) { if (list[i] % 3 == 1) { swap(list, count, i); count++; } } } public static void rearrange(int[] list) { int index1 = 0; int index2 = 0; int index3 = list.length - 1; while (index2 <= index3) { if (list[index2] % 3 == 1) { index2++; } else if (list[index2] % 3 == 0) { swap(list, index1, index2); index1++; index2++; } else if (list[index3] % 3 == 2) { index3--; } else { swap(list, index2, index3); index3--; } } } Each of these solutions relies on the following "helper" method. public static void swap(int[] list, int index1, int index2) { int temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; }
Stuart Reges
Last modified: Sun Dec 18 10:31:02 PST 2005