Problem #1: 2 points each, 1 point for correct value of wrong type (missing parens, no .0) Problem #2: 1 point for each of the 12 values -1 if any error in surrounding text (dittos not allowed) -4 if two columns transposed -8 if you need to transpose twice Problem #3: each is 3 points, -1 for a single number wrong or for a single transpose -2 overall if columns transposed -2 if formatting error (e.g., commas between numbers) -1 if individual pair is reversed -0 if output is quoted -0 if underscore for space Problem #4: column 1 is 1 point, column 2 is 2 points -2 overall if columns transposed -2 if formatting error (e.g., commas between numbers) -1 if individual pair is reversed -0 if output is quoted -0 if underscore for space Problem #5: 1 point per blank Problem #6 Solutions: public static int numUnique(int x, int y, int z) { if (x == y && y == z) { return 1; } else if (x != y && x != z && y != z) { return 3; } else { return 2; } } public static int numUnique2(int x, int y, int z) { if (x == y) { if (y == z) { return 1; } else { return 2; } } else { // x != y if (y == z) { return 2; } else if (x == z) { return 2; } else { return 3; } } } public static int numUnique3(int x, int y, int z) { int count = 0; if (x != y) { count++; } if (y != z) { count++; } if (x != z) { count++; } if (count == 0) { count = 1; } return count; } 2 header (-1 per error) 13 correctly distinguishes cases (requires idea) 4 returns 1 when appropriate: (5, 5, 5) 2 reasonable attempt (trying to test for this case, return 1) 2 correct (always returns 1 for this case based on tests) 4 returns 3 when appropriate: (4, 5, 6) 2 reasonable attempt (trying to test for this case, return 3) 2 correct (always returns 3 for this case based on tests) 5 returns 2 when appropriate: (4, 5, 5); (5, 4, 5); (5, 5, 4) 2 reasonable attempt (trying to test for this case, return 2) 3 correct (always returns 2 for this case based on tests) Notes: -3 for single global change that increases their score. For example: printing instead of returning (then they can have a void method). Problem #7 Solution: public static void printNumbers(Random r) { int n = r.nextInt(50) + 1; System.out.print("[" + n); while (n % 10 != 5) { n = r.nextInt(50) + 1; System.out.print(", " + n); } System.out.println("]"); } public static void printNumbers2(Random r) { System.out.print("["); int n = r.nextInt(50) + 1; while (n % 10 != 5) { System.out.print(n + ", "); n = r.nextInt(50) + 1; } System.out.println(n + "]"); } 1 correct header (non-void return okay if they return something of that type) 2 expression for generating random numbers from 1 to 50: r.nextInt(50) + 1 1 attempt (uses Random, either the 50 is right or the +1 is right) 1 correct (both are correct and they do it right everywhere) 4 loop until they get a number ending in 5 (ignoring fencepost) 1 a loop with some attempt to test for n ending in 5 2 correct loop test 2 loop generates a new value for n each time through the loop and they aren't doing something silly like printing one random and testing another 3 printing (ignoring fencepost) 1 prints left and right brackets (println ok) 1 prints a comma either before or after each number (println ok) 1 overall print correctness ignoring fencepost: print("[") before loop, print(n && ",") in loop, print or println("]") after loop 4 solved fencepost problem 1 attempt to solve this problem 3 correct (-1 if some minor error like reversing order of number/comma) Problem #8 Solutions: public static int numWords(String s) { int count = 0; boolean inWord = false; for (int i = 0; i < s.length(); i++) if (s.charAt(i) == ' ') inWord = false; else if (!inWord) { count++; inWord = true; } return count; } public static int numWords(String s) { int count = 0; for (int i = 1; i < s.length(); i++) if (s.charAt(i - 1) == ' ' && s.charAt(i) != ' ') count++; if (s.length() > 0 && s.charAt(0) != ' ') count++; return count; } public static int numWords(String s) { int count = 0; int i = 0; while (i < s.length()) { while (i < s.length() && s.charAt(i) == ' ') i++; if (i < s.length()) { count++; while (i < s.length() && s.charAt(i) != ' ') i++; } } return count; } Can get up to 3 points for any of the following good things: 1 correct header 1 for loop involving s.length 1 initializing a counter variable 1 returning a counter variable after a loop 1 s.charAt(i) or s.substring(i, i + 1) 1 testing whether charAt or substring is equal or not equal to a space (don't worry about syntax if the intent is clear) 1 using s.indexOf(" ") or s.indexOf(' ') 1 comparing charAt(i) with charAt(i + 1) (or substring) Pass into round 2 if it has some hope of working: * testing adjacent characters for a space/nonspace combination (or using a previous character or flag variable to do so) * a nested loop approach that involves skipping past a word * using s.indexOf(" ") to skip over a word When in doubt, pass on to round 2.
Stuart Reges
Last modified: Fri Sep 16 15:55:46 PDT 2011