Problem #1 Expression Value ----------------------------------------------- 3 * (5 - 2) - 3 - 2 * 2 2 4 * 7 % 8 + 132 % 10 + 3 % 4 9 27 / 5 / 2 + 3.4 * 2 - 1.1 * 2 6.6 9 + 9 + "9 + 9" + 9 + 9 "189 + 999" 19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4 14.0 2 points each, 1 point for correct value of wrong type (missing quotes, no .0) ------------------------------------------------------------------------------- Problem #2: solution: a snow and slippery for sleet a storm and snow for sleetsnow a snowsleet and sleetsnow for snowstorm a sun and storm for sunny 1 point for each of the 12 values -1 off the top if any error in non-parameter text (no dittos, quotes, labels) -1 if there is an extra line of output -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(2, 2); 4 2 ifElseMystery(3, 1); 1 4 ifElseMystery(4, 0); 4 1 ifElseMystery(5, 3); 4 7 ifElseMystery(1, 2); 2 0 ifElseMystery(7, 4); 8 7 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: x == 1 x % 2 == 1 y == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point D | never | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | always | sometimes | +---------------------+---------------------+---------------------+ 1 point per answer Problem #6 Solution: public static int generate(Scanner console) { System.out.print("number between -5 and 5? "); int target = console.nextInt(); Random r = new Random(); int next = r.nextInt(11) - 5; System.out.print("numbers are: " + next); int count = 1; while (next != target) { next = r.nextInt(11) - 5; System.out.print(", " + next); count++; } System.out.println(); System.out.println("came up after " + count + " tries"); return count; } 1 header 2 prompt and read to obtain target number 1 reasonable attempt 1 correct note: ignore print/println here but take off for print format below 2 correct use of Random object 1 attempt, 2 of: constructed it, used it, correct expression 1 correct: all 3 of constructed it, used it, correct expression note that r.nextInt(11) - 5 is equivalent to 5 - r.nextInt(11) 2 loop to generate random numbers until it matches target (ignore fencepost) 1 reasonable attempt for test and for generating a new number each time 1 correct: generates a new number each time, loops until it matches target; lose this if they have a scope problem or if one of their numbers is a specific number rather than a random number 2 solved fencepost problem 1 attempt (print first number before a loop or the target after a loop or test for when to print comma) 1 correct (prints each number exactly once separated by commas--don't worry about count; if printing first number before loop, it has to be randomly generated and different from the numbers printed in the loop) 4 printing the required three elements (ignore fencepost issue) 1 print or println for 2 of the 3 elements: * "numbers are: " * number (ignore comma except for the format point below) * "came up after " + count + " tries" 1 print or println for all 3 elements 1 all 3 elements present and pieces fit together: "numbers are" printed once before loop, number printed once inside loop, "came up after" printed once after loop 1 all 3 elements present with correct format: text is all correct, print/println is correct, commas are correct 2 computes and returns count of generated numbers 1 reasonable attempt, 2 of: initialize, increment, return 1 correct (always computes and returns correct count of numbers printed) Problem #7 Solutions: public static int digitRange(int n) { int min = n % 10; int max = n % 10; while (n > 0) { int digit = n % 10; n = n / 10; if (digit < min) { min = digit; } if (digit > max) { max = digit; } } return max - min; } public static int digitRange(int n) { if (n < 10) { return 0; }; int min = 10; int max = -1; while (n != 0) { min = Math.min(n % 10, min); max = Math.max(n % 10, max); n = n / 10; } return max - min; } public static int digitRange(int n) { int digits = 1; int copy = n; while (copy >= 10) { digits++; copy = copy / 10; } int max = 0; for (int i = 0; i < digits; i++) { int first = (int) (n / Math.pow(10, i)) % 10; for (int j = 0; j < digits; j++) { int second = (int) (n / Math.pow(10, j)) % 10; int difference = Math.abs(first - second); if (difference > max) { max = difference; } } } return max; } 1 correct header 5 loop to get digits 1 attempt to use % 10 to get a digit 1 correctly uses % 10 to get a digit 1 correctly updates n to n / 10 2 correct loop structure to process each digit once: no double-jeopardy with the previous points, but requires correct test (n != 0 or n > 0), requires an attempt to get a digit and an attempt to update n, and that the pieces fit together (e.g., processing only one digit at a time instead of adjacent pairs) 7 max/min computation 1 some attempt at max/min computation 3 correct initialization of max and min 1 max initialized to last digit or something <= 0 1 min initialized to last digit or something >= 9 1 all correct: either last digit or works in all cases including 0 1 correctly updates one of min/max in the loop 2 correctly updates both min/max in the loop (if/else-if can be a problem if they are not initializing to the last digit) 2 returning their result 1 returns something somewhere 1 correctly returns (max digit) - (min digit) after a loop, but no double jeopardy if they had an error in computing those; can get this if they return a max of some digit delta (e.g., difference of adjacent digits) Notes: unusual solutions go to question leader including strings Problem #8 Solutions: public static void printStripped(String s) { boolean inComment = false; for (int i = 0; i < s.length(); i++) { char next = s.charAt(i); if (next == '<') { inComment = true; } else if (inComment && next == '>') { inComment = false; } else if (!inComment) { System.out.print(next); } } System.out.println(); } public static void printStripped(String s) { int start = s.indexOf('<'); while (start != -1) { int stop = s.indexOf('>', start + 1); s = s.substring(0, start) + s.substring(stop + 1); start = s.indexOf('<'); } System.out.println(s); } 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 s.indexOf("<") or s.indexOf(">") or s.contains("<") or s.contains (">") 1 printing an individual character 1 printing a substring that involved s.indexOf("<") or s.indexOf(">") 1 some kind of flag to keep track of whether you are in a comment ------------------------------------------------------------------------------- To go to round 2, a solution needs one of the following: * processing the string character by character and using some kind of flag that keeps track of whether or not they are in a comment * using s.substring and s.indexOf to repeatedly process sections of the string (they will usually lose the "repeatedly" part) * a nested loop approach where there is an inner loop that is skipping past characters that would be inside a comment or a loop that is printing characters that would be outside a comment -3 major error inventing methods for strings that don't exist -2 medium error && vs || need to delete nontrivial code of theirs needs an extra test for a case they missed moving a nontrivial line of code need to add 2nd parameter to indexOf change if's to else if's 2 mediums = -3 -1 minor error obob (you can fix code by changing one expression by +1 or -1) calling charAt not on string scoping issues return string instead of printing using break -0 minor syntax errors String result = s.charAt(0); String new = ... (reserved word) string vs char mistakes missing parens on method calls missing println() at the end
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011