Problem #1 Expression Value ----------------------------------------------- 3 * 4 + 5 * 6 42 23 % 5 - 17 % (16 % 10) -2 "1" + 2 + 3 * 4 + (5 + 6) "121211" 1.5 * 2 + 20 / 3 / 4.0 + 6 / 4 5.5 345 / 10 / 3 + 10 / (5 / 2.0) 15.0 2 points each, 1 point for correct value of wrong type (missing quotes, no .0) ------------------------------------------------------------------------------- Problem #2: solution: bill wants sue to buzz. hope wants foo to bill. bill wants sue to hope. hope wants bill to sue. 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 -0 if quoted ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(2, 3); 6 2 ifElseMystery(1, 4); 4 3 ifElseMystery(5, 4); 5 8 ifElseMystery(4, 4); 8 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: Method Call Output Produced --------------------------------------- mystery(1); 1 1 mystery(5); 3 4 mystery(10); 4 8 mystery(42); 6 32 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: x > y z > 0 y % 2 == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | never | never | +---------------------+---------------------+---------------------+ Point B | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point D | sometimes | always | always | +---------------------+---------------------+---------------------+ Point E | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solution: public static void randomWalk(Random r, int goal) { int position = 0; int max = 0; int min = 0; System.out.println("goal = " + goal + ", pos = 0"); while (position != goal) { int add = r.nextInt(5) - 2; position = position + add; System.out.println("adding " + add + ", pos = " + position); if (position > max) { max = position; } if (position < min) { min = position; } } System.out.println("min = " + min + ", max = " + max); } 1 header (non-void return okay if consistent with an actual return) 2 correct initialization and print of position 1 position variable declared and initialized to 0 1 goal and position printed before a loop (ignore formatting issues) 2 while loop 1 reasonable attempt 1 correct: 4 updating/printing position 1 reasonable attempt to calculate amount to add: either nextInt(5) or -2 or similar 1 correct calculation of amount to add 1 value to add is stored in a variable and printed along with position (ignore order issues and ignore formatting) 1 pieces fit together: value actually is stored and added, happens each time through a loop, print is after update (no double jeopardy with calculation of amount to add) 4 max/min computation 1 max declared/initialized properly outside loop (must be set to 0) 1 min declared/initialized properly outside loop (must be set to 0) 1 max updated correctly in loop (must be after an update) 1 min updated correctly in loop (must be after an update) note: if there is a minor error that exists in parallel between max and min like initializing it to the wrong starting value, then take it off once but not twice; doesn't apply to a big error like reversing the tests 1 max and min printed after a loop 1 all of the print/println commands are present and correct Problem #7 Solution: public static boolean checkPrime(int n) { System.out.print("factors of " + n + " = 1"); int count = 1; for (int i = 2; i <= n; i++) { if (n % i == 0) { System.out.print(", " + i); count++; } } System.out.println(); System.out.println("Total factors = " + count); return count == 2; } 1 correct header 3 correct loop structure (ignoring fencepost issue and early return): 1 basic attempt: a loop that has bounds mostly going 1 to n 2 correct: a single for loop that gives a variable i the values 1 through n or the equivalent in while loop form (-1 for obob, okay to handle the special case of printing 1 before the loop and/or n after the loop, lose it if they have nested loops that produce too much output or other odd structures) 3 counting factors 1 attempt: (incrementing a counter with code that finds or prints a factor) OR (counter initialized before a loop and printed after a loop) 2 correct ignoring issues of loop structure and early return: correct test for factor, correct initialization for counter (-1 if obob which is likely if they solved the fencepost), correctly increments in an if or equivalent 4 producing output (ignoring fencepost issue and early return): 1 print the value of n once before any loop along with most of the intro 1 print the value of some variable that could be a factor inside some loop 1 print total factors once with most of the text after any loop 1 all elements are printed and all formatting correct including using print commands for the elements of the first line of output along with an empty println to complete the line, a println used to produce the second line of output with the factor count, an indication of spacing in their string constants, no extra output (blank line at the end is okay) note: if they don't have a loop, they lose the 2nd point but are eligible for the others although they are unlikely to get the 4th point because they won't have an empty println along with print commands 2 returns whether or not the number of factors is exactly 2 (lose both points if they return early) 1 attempt: returns or prints something like true/false by testing against something that looks like the number of factors 1 correct: returns true when number of factors is exactly 2, returns false when it is not, no double jeopardy with earlier code--this is about what they return assuming their counter has the correct value) 2 solved the fencepost 1 reasonable attempt: clear indication of either printing first term before a loop or printing last term after a loop 1 correct approach: printing other terms inside a loop with leading comma (if first term before loop) or trailing comma (if last term after loop), no extra commas printed Problem #8 Solutions: public static String undouble(String s) { String result = ""; if (s.length() > 0) { char last = s.charAt(0); result += last; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != last) { result += s.charAt(i); } last = s.charAt(i); } } return result; } public static String undouble(String s) { String result = ""; if (s.length() > 0) { result = s.substring(0, 1); for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != s.charAt(i - 1)) { result += s.charAt(i); } } } return result; } Can get up to 3 points for any of the following good things: 1 correct header 1 for loop involving s.length 1 returning a String 1 s.charAt(i) or s.substring(i, i + 1) 1 str += charAt or substring 1 comparing charAt(i) with charAt(i + 1) (or substring) ------------------------------------------------------------------------------- To go to round 2, a solution needs: * local string variable that they are building up with concatenation * looping over string using string length * comparison of adjacent characters (either keeping a prev char or comparing at indexes i/i+1 or i-1/i) -3 major error -2 medium error doesn't handle 1st/last char properly && vs || need to delete nontrivial code of theirs result = something vs result += something off by 2 or more -1 minor error fails on empty string fails on particular strings like all doubles (ccaatt) obob (you can fix code by changing one expression by +1 or -1) -0 minor syntax errors String result = s.charAt(0); String new = ... (reserved word)
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011