Problem #1
Expression Value
-----------------------------------------------
8 * 2 - 2 * 3 10
87 % 10 + 28 % 5 % 2 8
1 + 2 + "3" + 4 + 5 * 6 "33430"
2 * 2.3 + 5 / 2 + 19 / 4 * 2.0 14.6
436 / 10 / 5 - 9 / 2 * 2.5 / 2 3.0
2 points each, 1 point for correct value of wrong type (missing quotes, no .0)
-------------------------------------------------------------------------------
Problem #2:
solution:
to walk the walk is good
to hear the good is bad
to feel the walk is song
to feel the talk is bad
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(1, 2); 4 2
ifElseMystery(4, 2); 3 8
ifElseMystery(3, 2); 5 3
ifElseMystery(2, 2); 3 2
ifElseMystery(4, 4); 4 9
ifElseMystery(8, 6); 7 12
each number is worth a point
-2 overall if columns transposed
-2 if formatting error (e.g., commas between numbers)
-0 if output is quoted
-0 if underscore for space
-------------------------------------------------------------------------------
Problem #4:
Method Call Output Produced
---------------------------------------
mystery(4); 2 2
mystery(5); 1 5
mystery(24); 4 3
mystery(28); 3 7
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 == 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 blank
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
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)
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 all correct (always computes and returns correct count)
Problem #7
Solution:
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;
}
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) and
that the pieces fit together (e.g., processing only one digit at a time
instead of adjacent pairs); lose this if they use a string length instead
of using a while loop
7 max/min computation
1 some attempt at max/min computation
3 correct initialization of max and min
1 max initialized to a digit or something <= 0
1 min initialized to a digit or something >= 9
1 all correct: either a 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
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)
after a loop
Notes: unusual solutions go to question leader including String based solutions
that use charAt
using if/else-if can fail on many cases if they don't initialize max/min to the
first digit, but take off just that third intialization point if they have the
if/else-if and don't initialize to the first digit
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
* 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
&& vs ||
need to delete nontrivial code of theirs
-1 minor error
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