Problem #1
Expression Value
---------------------------------------------------
8.0 / 4 / 4 + 3 / 4 + 0.5 * 3 2.0
88 % 10 % 5/2 + 7 % 8 8
2 * 3 + 4 + " = " + 5 * 6 + 7 "10 = 307"
54 / 5 / 2.0 / 4 * 3 3.75
2 * (0.75 + 1.25) / 5 * 10 - 7/4 7.0
1 point each, type has to match (2.0 vs 2, using quotes for string)
-------------------------------------------------------------------------------
Problem #2:
Original List Final List
--------------------------------------------------------------
{8} {8}
{6, 3} {6, 9}
{2, 4, 6} {2, 6, 12}
{1, 2, 3, 4} {1, 3, 6, 10}
{7, 3, 2, 0, 5} {7, 10, 12, 12, 17}
2 points for each answer
-1 for each wrong number
-1 for each missing number
any given answer can't go negative
-------------------------------------------------------------------------------
Problem #3:
d c c d
d 1 c 1 c 1 d 1
b 2 c 2 a 2 a 2
-1 if columns and/or rows are labeled but in the right order
-2 if columns and/or rows are labeled and in the wrong order
-1 if two columns transposed or two rows transposed
-2 if you need to transpose twice
-2 if rows and columns are transposed
-0 for quotes or lack of spaces or capital letters
-------------------------------------------------------------------------------
Problem #4
Solution:
public static void reportRunningSum(Scanner input) {
double sum = input.nextDouble();
double max = sum;
System.out.print("running sum = " + sum);
while (input.hasNextDouble()) {
sum += input.nextDouble();
System.out.print(" " + sum);
if (sum > max) {
max = sum;
}
}
System.out.println();
System.out.println("max sum = " + max);
}
1 header
1 correct while loop (while hasNextDouble)
4 computing running sums
1 initialize sum correctly
1 update sum inside of loop
1 attempt at printing sums
1 correct printing of sums (print before loop, print inside loop, println
after loop, spaces between numbers)
4 computing max sum
2 initialize sum to first value
1 attempt to update inside loop and print after loop (either one is
completely correct or both are partly correct)
1 correct update and print (requires loop)
Problem #5
Solution:
public static int analyzeParagraphs(Scanner input) {
int max = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
int count = 0;
while (!line.equals("")) {
count++;
line = input.nextLine();
}
System.out.println(count + "-line paragraph");
if (count > max) {
max = count;
}
}
return max;
}
public static int analyzeParagraphs(Scanner input) {
int max = 0;
int count = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.equals("
")) {
System.out.println(count + "-line paragraph");
count = 0;
} else {
count++;
}
if (count > max) {
max = count;
}
}
return max;
}
1 header
1 correct while loop (while hasNext or while hasNextLine)
1 reads and stores a line using nextLine()
3 loop that counts lines until "
" or if/else that handles "
"
1 attempt (inner loop or if/else with some kind of "
" test or "<" test)
1 correctly counting number of lines in each paragraph
1 correct comparison (requires equals instead of ==)
1 println for "-line paragraph" (generous: doesn't even require loop let
alone correct count computation)
2 max computation
1 attempt (initialize to something, update inside loop)
1 all correct (initialize to 0, correct update in loop, returned after
loop)
Problem #6
Solution:
public static int numUnique(int[] list) {
if (list.length == 0) {
return 0;
} else {
int count = 1;
for (int i = 1; i < list.length; i++) {
if (list[i] != list[i - 1]) {
count++;
}
}
return count;
}
}
2 header (-1 per error)
1 some kind of loop that iterates over the array (requires test against
list.length and some kind of reference to list[])
2 correct initialization of their counter, which generally requires a special
case for the empty list (without this they generally either have a problem
for the empty list or they are off-by-one for nonempty lists)
4 correctly counts values (ignoring issues that came up because of the special
case for empty list)
1 some kind of comparison of list[i] versus list[i + 1] or list[i - 1]
1 correct loop bounds
2 correct comparison (whether adjacent values are not equal) used to
increment count
1 return count after loop
Problem #7
Solutions:
public static void removeAdjacentMatches(ArrayList list) {
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) == list.get(i + 1)) {
list.remove(i + 1);
i--;
}
}
}
public static void removeAdjacentMatches(ArrayList list) {
int i = 0;
while (i < list.size() - 1) {
if (list.get(i) == list.get(i + 1)) {
list.remove(i + 1);
} else {
i++;
}
}
}
2 header (non-void return ok if consistent with a return statement, parameter
must be ArrayList, -1 for misspelled name or ArrayList)
2 loop over list
1 attempt (loop using list.size() or list.length() or list.length)
1 correct within OBOB (uses list.size() or list.size)
4 removes adjacent duplicates
1 attempt to test whether adjacent elements are equal (array notation is
okay for this point)
1 correct test (test whether adjacent values are equal using calls on get)
1 calls remove on one of the two values when they match (requires test
attempt)
1 correct loop bounds (compare each adjacent pair without out-of-bounds)
2 works for 3 or more in a row (usually either a while loop that avoids the
increment or an extra decrement to counteract the increment or the loop goes
backwards)
Notes:
-0 for list.get[i] and/or list.remove[i] versus list.get(i) and list.remove(i)
Problem #8
Solution:
public class Eagle extends Critter {
private int count;
private int max;
public Eagle() {
count = 0;
max = 1;
}
public Action getMove(CritterInfo info) {
count++;
if (count == 2 * max) {
count = 0;
max++;
}
if (info.getFront() == Neighbor.EMPTY) {
return Action.HOP;
} else if (info.getFront() == Neighbor.WALL) {
return Action.RIGHT;
} else {
return Action.INFECT;
}
}
public Color getColor() {
if (count < max) {
return Color.RED;
} else {
return Color.BLUE;
}
}
public String toString() {
return "<>";
}
}
1 class header
2 toString
1 header
1 returns "<>"
5 getMove
1 header
1 returns Action.HOP based on some kind of empty neighbor test
1 returns Action.RIGHT based on some kind of wall test
1 returns Action.INFECT in some of the other cases based on tests
1 all correct
7 getColor
1 header
1 returns Color.RED in some cases (generous)
1 returns Color.BLUE in some cases (generous)
1 declared a field for counting moves and incremented it in getMove or
getColor
1 second field for current maximum
2 all correct (-1 if close)
Problem #9
Solution:
public static int[] reverseSublist(int[] list, int from, int to) {
int[] result = new int[to - from];
for (int i = 0; i < result.length; i++) {
result[i] = list[to - i - 1];
}
return result;
}
2 header (-1 per error)
4 constructing the new array
1 attempt (construct some new array mostly ignoring syntax)
1 better attempt (array of int, length within 2 of being right or is
correct but has opposite sign)
2 correct (array of int, size correct)
8 copying values from original to new array
2 for loop over new array
1 attempt (any for loop will do)
1 correct (from 0 to size of new array--requires array construct
attempt)
2 copying from old array to result
result[something] = list[something]
4 correctly reverses (-1 if expressions are OBOB)
1 returns their answer after a for loop
Notes:
Students weren't supposed to use ArrayList or Arrays.copyOf or
Collections.reverse--when in doubt, show to question leader
Syntax errors are capped at -2.
Problem #10
Solutions:
public static void removeZeros(int[] list) {
for (int i = list.length - 2; i >= 0; i--) {
if (list[i] == 0) {
for (int j = i; j < list.length - 1; j++) {
list[j] = list[j + 1];
}
list[list.length - 1] = 0;
}
}
}
public static void removeZeros(int[] list) {
int numNonZero = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] != 0) {
list[numNonZero] = list[i];
numNonZero++;
}
}
for (int i = numNonZero; i < list.length; i++) {
list[i] = 0;
}
}
public static void removeZeros(int[] list) {
int numZeros = 0;
for (int i = 0; i < list.length - numZeros; i++) {
if (list[i] == 0) {
numZeros++;
for (int j = i; j < list.length - 1; j++) {
list[j] = list[j + 1];
}
list[list.length - 1] = 0;
i--;
}
}
}
(not very good) Plus criteria (max +3)
Good things that each get 1 point:
1 correct header
1 for loop using array length
1 testing if an array element is 0
1 an inner loop that is used when an array element is 0
1 a counter to keep track of the number of zeros seen
1 some kind of array-shifting code: array[one thing] = array[another thing]
-------------------------------------------------------------------------------
(pretty good) Minus criteria (min + 4)
Grade this way if you see:
an inner loop that shifts down when a 0 is seen
counting the number of nonzeros and moving values to the next available
spot
anything else that you think might work, show to question leader
Deductions:
-1 OBOB
-2 runs off end of the array
-2 doesn't handle two in a row
-3 doesn't stop early (forward loop that processes 0's that they put at the
end of the list)