Problem #1 Expression Value ----------------------------------------------- 13 + 2 * 5 / 3 16 2.5 * 2 * 5 / 10 + 1.5 4.0 85 % 10 + 4 % 10 - 17 % 3 7 2 + 3 + "." + (3 + 4) + 2 * 3 "5.76" 482 / 10 / 5 / 2.0 * 2 + 14 / 5 11.0 2 points each, 1 point for correct value of wrong type (missing quotes, no .0) ------------------------------------------------------------------------------- Problem #2: solution: skip likes to drive with george george likes to work with drive george likes to skip with mary john likes to dance with mary 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(14, 14); 14 13 ifElseMystery(4, 5); 7 5 ifElseMystery(10, 5); 12 10 ifElseMystery(2, 8); 3 8 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(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 -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: next == 0 prev == 0 next == prev +---------------------+---------------------+---------------------+ Point A | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | never | never | always | +---------------------+---------------------+---------------------+ Point D | sometimes | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solution: public static void giveProblems(Scanner console, int numProblems) { Random r = new Random(); int numRight = 0; for (int i = 1; i <= numProblems; i++) { int x = r.nextInt(12) + 1; int y = r.nextInt(12) + 1; System.out.print(x + " * " + y + " =? "); int answer = x * y; int response = console.nextInt(); if (response == answer) { System.out.println("correct"); numRight++; } else { System.out.println("incorrect...the answer was " + answer); } } System.out.println(numRight + " of " + numProblems + " correct"); } 1 header (non-void return okay if consistent with an actual return) 3 correct use of Random 1 constructing a random object 1 attempt: setting two int variables OR correct expression to get 1-12 1 correct: setting two int variables AND correct expression to get 1-12 2 loop numProblems times 1 attempt: some kind of loop 1 correct: correctly using parameter, correct loop bounds, all appropriate code is inside the loop 5 giving problems 1 attempt to prompt for problem (requires two ints) 1 reading an int response 1 correctly tests response versus product of two ints 1 reasonable attempt to report correct 1 reasonable attempt to report incorrect 3 reporting number of problems correctly solved 1 initialize counter before a loop 1 attempt: correctly increments counter OR correctly reports number correct and number tried 1 correct: correctly increments counter AND correctly reports number correct and number tried note: no double jeopardy with random generation and test for correct answer 1 all of the print/println commands are present and correct 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 5 compute sequence and sum 1 declare and properly initialize sum 1 declare and properly initialize counter 2 update the sum inside a loop 1 attempt (some form of n/(n+1)) 1 correct (including cast or equivalent) 1 update 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) 2 return the number of terms after a loop 1 attempt (return something that looks like a counter) 1 correct (returns the correct counter value--requires correct counter update in the loop) 3 solved the fencepost 1 attempt (printed first term before the loop) 2 correct approach (printing later terms inside a loop with leading plus) Problem #8 Solutions: public static int weave(int x, int y) { int result = 0; int multiplier = 1; while (x != 0 || y != 0) { result = result + multiplier * (x % 10 * 10 + y % 10); multiplier = multiplier * 100; x = x / 10; y = y / 10; } return result; } Can get up to 3 points for any of the following good things: 1 correct header 2 using % 10 on a variable 1 first variable 1 second variable 2 setting n = n / 10 for a variable 1 first variable 1 second variable 1 using Math.pow(10, something) 2 multiplying a variable by 10 each time 1 first variable multiplied by 10 or they multiplied by 100 2 second variable multiplied by 10 or they multiplied by 100 2 while loop with a test 1 testing whether the two variables have become 0 (could be &&) 1 testing whether the two variables have become 0 with || 3 manipulating a variable to compute a result: 1 initializing it to something like 0 before a loop 1 resetting it inside the loop to include values related to the parameters 1 returning it after a loop ------------------------------------------------------------------------------- To go to round 2, a solution needs: * use of % 10 on two variables that derive from the parameters or highly clever uses of division by 10 and subtraction to get the equivalent * reasonable attempt at a loop that goes while there are digits left * a local variable (multiplier, result) multiplied by 10 or 100 each time through the loop
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011