Solution to CSE142 Final handout #29 Spring 2006 1. Expression Value --------------------------------------------------- 17/3 + 55 % 20/2 12 19.0/2 + 2 * 2.25 14.0 2 + 2 + "." + 2 + 2 + 4 * 5 "4.2220" 73/10 * 4/3/2.0 * 3 13.5 15/2 * (1.5 + 2.5) + 98 % 5/2 29.0 2. Original List Final List -------------------------------------------------------------- (8) (8) (6, 3) (6, 9) (2, 4, 6) (2, 6, 12) (1, 2, 3, 4) (1, 3, 6, 10) (7, 3, 2, 0, 5) (7, 10, 12, 12, 17) 3. Polymorphism. The output produced is as follows. d d 1 b 2 c c 1 c 2 c c 1 a 2 d d 1 a 2 4. Token-Based File Processing. One possible solution appears below. public static void processSets(Scanner input) { int count = 0; double sum = 0.0; while (input.hasNextInt()) { int n = input.nextInt(); if (n == 0) { System.out.println("total values = " + count); System.out.println("average value = " + sum / count); System.out.println(); count = 0; sum = 0; } else { count += n; sum += n * input.nextDouble(); } } } 5. Line-Based File Processing. One possible solution appears below. public static void countOddLength(Scanner input) { int odd = 0; int total = 0; while (input.hasNextLine()) { String text = input.nextLine(); total++; if (text.length() % 2 != 0) { odd++; } } System.out.println("Total lines = " + total); System.out.println("odd length = " + odd); double percent = odd * 100.0 / total; System.out.println("percent = " + percent); } 6. Arrays. One possible solution appears below. public static int numUnique(int[] list) { if (list.length == 0) { return 0; } else { int count = 1; for (int i = 1; i < list.length; i++) { if (list[i] != list[i - 1]) { count++; } } return count; } } 7. ArrayLists. One possible solution appears below. public static void collapse(ArrayList<String> list) { for (int i = 0; i < list.size() - 1; i++) { String pair = "(" + list.get(i) + ", " + list.get(i + 1) + ")"; list.set(i, pair); list.remove(i + 1); } } 8. Critters. One possible solution appears below. public class Orca implements Critter { private int move; private int max; private char display = '+'; public char getChar() { return display; } public int getMove(CritterInfo info) { if (this.move == 3 * this.max) { this.move = 0; this.max++; } this.move++; if (this.move <= this.max) { display = '+'; return CENTER; } else if (this.move <= 2 * this.max) { display = '<'; return WEST; } else { display = '>'; return EAST; } } } 9. Arrays. One possible solution appears below. public static void insert(int[] shortList, int[] bigList, int index) { int lastIndex = bigList.length - 1 - shortList.length; for (int i = lastIndex; i >= index; i--) { bigList[i + shortList.length] = bigList[i]; } for (int i = 0; i < shortList.length; i++) { bigList[index + i] = shortList[i]; } } 10. Programming. One possible solution appears below. public static void printReversed(String s) { boolean inWord = false; int start = -1; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { inWord = false; System.out.print(' '); } else { if (!inWord) { inWord = true; start = i; } if (i == s.length() - 1 || s.charAt(i + 1) == ' ') { for (int j = i; j >= start; j--) { System.out.print(s.charAt(j)); } } } } System.out.println(); }
Stuart Reges
Last modified: Fri Oct 1 14:57:19 PDT 2004