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:
table and chair like notes
notes and notes like x
notesx and notestable like notesx
chair and boring like today
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(6, 5); 5 15
ifElseMystery(4, 6); 7 5
ifElseMystery(9, 5); 7 15
ifElseMystery(3, 6); 5 5
ifElseMystery(2, 7); 8 6
ifElseMystery(1, 3); 3 13
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(5); 0 0
mystery(28); 2 82
mystery(346); 2 64
mystery(265408); 3 804
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 > 0 x == 0 y == 0
+---------------------+---------------------+---------------------+
Point A | always | never | always |
+---------------------+---------------------+---------------------+
Point B | always | never | sometimes |
+---------------------+---------------------+---------------------+
Point C | never | never | never |
+---------------------+---------------------+---------------------+
Point D | never | sometimes | never |
+---------------------+---------------------+---------------------+
Point E | never | always | never |
+---------------------+---------------------+---------------------+
1 point per answer
Problem #6
Solution:
public static void printSum(int n, int low, int high) {
System.out.println("sum " + n + " numbers " + low + " to " + high);
Random r = new Random();
int next = r.nextInt(high - low + 1) + low;
int max = next;
int sum = next;
System.out.print(next);
for (int i = 2; i <= n; i++) {
next = r.nextInt(high - low + 1) + low;
System.out.print(" + " + next);
sum = sum + next;
if (sum > max) {
max = sum;
}
}
System.out.println(" = " + sum);
System.out.println("max = " + max);
}
1 header (non-void return okay if consistent with an actual return)
3 correct use of Random object
1 reasonable attempt to construct and use a Random object
1 expression in parens is correct (high - low + 1) OR both the expression
in parens and the offset (+ low) are within 2 of correct
1 correctly constructs Random object and correct expression (if the
expression appears twice, it's okay if one has a minor copying error)
4 loop to generate n random numbers that solves the fencepost
1 reasonable attempt at a for loop that executes n times
1 reasonable attempt to solve fencepost: one number generated and printed
outside the loop (lose this if it is a specific number) or equivalent
1 correct structure for fencepost: printing first term outside loop without
a plus and printing later terms inside a loop with leading plus or
equivalent (can get this even if some terms are not randomly generated,
but not if it's the same number over and over)
1 loop prints exactly n numbers (either one outside and n-1 in loop or
exactly n in loop; lose this if numbers generated inside loop aren't
randomly selected each time through the loop)
2 max
1 reasonable attempt to declare, initialize and update the max
1 all correct to find the max of the partial sums for the sequence they
print (generally max should be initialized to first number, but it can be
set to a multiple of low if they check every number including any single
term outside the loop)
2 sum
1 reasonable attempt to declare, initialize and update the sum
1 all correct to find the sum of the numbers they print
3 printing the required three elements other than the numbers
1 reasonable attempt for 2 of the 3 printing elements (print is okay):
* initial println
* completing output with " = " + sum
* println for "max = "
1 reasonable attempt for all 3 of the elements (print is okay):
1 all correct including spacing around characters and print in loop with
plus, but ignore an extra plus from incorrect fencepost structure
Problem #7
Solutions:
public static double tallyVotes(Scanner input) {
System.out.print("vote? ");
String vote = input.next();
double result = 0;
int total = 0;
while (!vote.equals("q")) {
if (vote.equals("y")) {
result++;
}
total++;
System.out.print("vote? ");
vote = input.next();
}
result = result / total * 100;
System.out.println("total votes = " + total);
System.out.println("result = " + result + "%");
return result;
}
public static double tallyVotes(Scanner input) {
String vote = "";
int yes = 0;
int no = 0;
while (!vote.equals("q")) {
System.out.print("vote? ");
vote = input.next();
if (vote.equals("y")) {
yes++;
} else if (vote.equals("n")) {
no++;
}
}
int total = yes + no;
double result = 100.0 * yes / total;
System.out.println("total votes = " + total);
System.out.println("result = " + result + "%");
return result;
}
1 correct header
5 loop to read one token at a time until the user types "q"
1 attempt to test whether user response is not "q" to control a while loop
1 correctly tests whether user response is not "q" to control a while loop
1 correctly handles initial read (either prompt/read before loop or set
their variable to a value that gets them into the loop)
2 reads and stores exactly one more response each time through their loop
3 counting yes/no votes
1 reasonable attempt to count yes versus no votes (doesn't require a loop,
can get this even if they don't read inside their loop, but requires an
if/else testing a string for yes versus no)
2 correctly counts yes/no votes: requires two variables initialized to
zero, a correct if/else structure to distinguish between them (unguarded
else is okay if their loop tests guarantees it is not "q"), and pieces
fitting together to process each vote exactly once; this is most often
atomic, but ask question leader for permission to give 1 point if they
are close
2 computing percent yes votes (requires two counters, assume counters have
correct values)
1 reasonable attempt to compute percent yes votes
1 correctly computes percent yes votes
3 printing
1 reasonable attempt for two of the three required printing tasks: prompt,
total votes, result
1 reasonable attempt for all three required printing tasks
1 all 3 elements present with correct format: text is all correct,
print/println is correct, prompt is inside a loop, printlns are after a
loop, no extra printing
1 returns a value that they believe is percent yes votes (has to be at the end
of the method, not something that would return early)
Notes:
-0 for confusion of char/String with single quotes versus double quotes
if they use == instead of .equals, they lose 1 point once assuming they got
some credit for the code where that occurred
-1 if they store console.next() in a char variable instead of a String variable
(assuming they get some points for it somewhere), then allow them to use ==
instead of .equals
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