Problem #1 Expression Value ----------------------------------------------- 5 * 6 - (4 + 3) * 2 - 2 * 3 10 208 / 20 / 4 + 12 / 10.0 + 0.4 * 2 4.0 8 - 2 + "8 - 2" + 8 * 2 + 8 "68 - 2168" 4 * 5 % 6 + 297 % 10 + 4 % 8 13 13 / 2 * 3.0 + 5.5 * 3 / 2 26.25 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 off the top if any error in non-parameter text (no dittos, quotes, labels) -4 if two columns transposed -8 if you need to transpose twice okay to use underscore for space ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(5, 20); 7 0 ifElseMystery(42, 42); 43 41 ifElseMystery(6, 1); 9 0 ifElseMystery(2, 0); 3 -1 ifElseMystery(7, 10); 9 0 ifElseMystery(4, 4); 5 3 each number is worth a point -2 overall if columns transposed -1 off the top if formatting error (e.g., commas between numbers, quotes) okay to use underscore for space ------------------------------------------------------------------------------- Problem #4: Method Call Output Produced --------------------------------------- mystery(8); 1 8 mystery(32); 2 5 mystery(184); 3 13 mystery(8239); 4 22 column 1 is 1 point, column 2 is 2 points -2 overall if columns transposed -1 off the top if formatting error (e.g., commas between numbers, quotes) -1 if individual pair is reversed okay to use underscore for space Problem #5: n == 0 n % 2 == 1 x == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | never | always | never | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | always | never | sometimes | +---------------------+---------------------+---------------------+ 1 point per answer Problem #6 Solution: public static void testLuck(Scanner console, int numProblems) { Random r = new Random(); int numRight = 0; System.out.println("guess " + numProblems + " one-digit odd numbers"); for (int i = 1; i <= numProblems; i++) { int answer = r.nextInt(5) * 2 + 1; System.out.print("guess #" + i + "? "); int guess = console.nextInt(); if (guess == answer) { System.out.println("correct!"); numRight++; } else { System.out.println("incorrect...answer was " + answer); } } double expected = numProblems / 5.0; System.out.println("expected correct = " + expected); System.out.println("you got " + numRight + " correct"); } There is an overall printing correctness point for print vs println and minor formatting issues like forgetting a number sign or exclamation mark or spaces (ignore these issues for other points). 1 header (non-void return okay if consistent with an actual return) 3 correct use of Random to generate numbers to be guessed 1 correctly constructs a random object 1 reasonable attempt for expression (one OBOB okay): r.nextInt(5) * 2 + 1 1 correct expression and new random number generated and stored in a variable of type int for each iteration of a loop 3 loop that prompts for and reads the given number of guesses 1 reasonable attempt for both the loop and the prompt/read (nextDouble ok) 1 prompt includes i to produce text like "guess #4?" 1 correct: correct loop structure and bounds, correct prompt, response is read with nextInt and stored in a variable of type int, exactly one prompt/read for each loop iteration 4 print response & update 1 reasonable attempt to test whether guess equals answer 1 reasonable attempt to print correct vs incorrect 1 reasonable attempt to count num right (init, increment inside an if) 1 all of these are correct: correct test for guess==answer, prints appropriate messages (correct vs incorrect with answer), initializes numRight to 0 before loop, increments numRight when correct) 2 prints expected number correct 1 reasonable attempt to compute and print expected correct after a loop (can be wrong expression or int) 1 correctly prints expected number correct after a loop (must be double) 1 prints numRight after a loop 1 All six printing elements exist (initial print, prompt, correct, incorrect, expected correct, numRight) and formatted correctly including use of print/println Notes: If you see something odd that might actually generate 1-digit odd numbers, show it to the question leader. For example, this works: int n = r.nextInt(10); n = n + (n + 1) % 2; Problem #7 Solution: public static int printSequenceTo(double value) { double sum = 0.5; System.out.print("1/2"); int n = 1; while (sum < value) { n++; System.out.print(" + " + n + "/" + (n + 1)); sum = sum + (double) n / (n + 1); } System.out.println(" = " + sum); return n; } 1 correct header 1 loop while sum is less than target 6 compute sequence and sum 2 declare and properly initialize sum (must be double) 1 attempt: at 2 of declaring, using type double, init to 0 or 0.5 1 correct (must be double, setting it to any value(s) printed outside the loop) 1 declare and properly initialize counter (could be double) 2 correctly updates the sum inside a loop 1 attempt (some form of n/(n+1)) 1 correct (including cast or equivalent) 1 correctly updates counter inside a loop 3 prints the sequence (ignoring fencepost problems) 1 reasonable attempt to print terms inside the loop (some form of n/(n+1)) 1 reasonable attempt to print sum after the loop 1 all correct (first term if printed separately, slashes and plusses, spacing, print/println, integer terms) 1 correctly returns the number of terms after a loop 3 solved the fencepost 1 attempt (printed first term before the loop or equivalent) 2 correct approach (printing later terms inside a loop with leading plus or equivalent) Problem #8 Solutions: public static boolean isConsecutive(int n) { if (n < 10) { return true; } else { int lastDigit = n % 10; n = n / 10; while (n != 0) { int nextDigit = n % 10; n = n / 10; if (nextDigit != lastDigit - 1) { return false; } lastDigit = nextDigit; } return true; } } Can get up to 3 points for any of the following good things: 1 correct header 1 while loop involving n > 0 or n > 9 or n > 99 1 using n % 10 1 setting n to n / 10 1 something that looks like a comparison of two digits being consecutive 1 returning true after the loop 1 some attempt to store previous digit or compare adjacent digits Pass into round 2 if it has some hope of working: * keeping track of the previous digit (requires a second variable) * trying to pull apart the last 2 digits of a number for a comparison When in doubt, pass on to round 2. ------------------------------------------------------------------------------- Round 2 grading: 9 all correct 8 one minor error like an OBOB or one time mixing up true/false 7 two minor errors or a medium error (examples of medium error below) digit order is backwards: looking for 654 instead of 456 returns early or resets a flag back to true completely reverses when to return true and when to return false 6 a medium and minor error or a major error (examples below) compares pairs of adjacent digits only like 127845 (true 12.78.45) 5 correctly identifies a consecutive pair and has a loop that has most of the pieces you need to keep computing more consecutive pairs 4 computes a consecutive pair of digits, not necessarily in a loop
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011