Problem #1
Expression Value
---------------------------------------------------
17 / 3 + 55 % 20 / 2 12
19.0 / 2 + 2 * 2.25 14.0
2 + 2 + "." + 2 + 2 + 4 * 5 "4.2220"
73 / 10 * 4 / 3 / 2.0 * 3 13.5
3 / 2 * (1.5 + 2.5) + 98 % 5 / 2 5.0
1 point each, type has to match (2.0 vs 2, using quotes for string)
-------------------------------------------------------------------------------
Problem #2:
Original List Final List
--------------------------------------------------------------
{2, 5, 8} {2, 5, 10}
{3, 4, 7, 5} {3, 4, 10, 5}
{2, 4, 6, 2, 6} {2, 4, 8, 2, 14}
{4, 5, 8, 9, 3} {4, 5, 12, 9, 15}
{1, 2, 8, 5, 10, 5, 4} {1, 2, 9, 5, 19, 5, 23}
2 points for each answer
-1 for each wrong number
-1 for each missing number
any given answer can't go negative
-------------------------------------------------------------------------------
Problem #3:
a c a c
a 1 c 1 a 1 d 1
b 2 c 2 c 2 c 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 printStrings(Scanner input) {
while (input.hasNextInt()) {
int times = input.nextInt();
String word = input.next();
for (int i = 0; i < times; i++) {
System.out.print(word);
}
System.out.println();
}
}
1 header (non-void return type ok if consistent with a return statement)
2 correct while loop
1 attempt (exists, calling hasNext, hasNextInt, or hasNextLine)
1 correct (hasNext or hasNextInt)
2 reads int/string combination
1 attempt (reads at least one)
1 correct (reads both correctly)
3 inner for loop (requires an attempt to read an int/string combination)
2 attempt (exists, using the int to print the string multiple times)
1 correct (prints the string exactly n times, println is okay)
2 correct formatting (requires for loop attempt): calls on print inside the for
loop, a single call on println after the for loop
Problem #5
Solution:
public static void flipLines(Scanner input) {
while (input.hasNextLine()) {
String first = input.nextLine();
if (input.hasNextLine()) {
String second = input.nextLine();
System.out.println(second);
}
System.out.println(first);
}
}
1 header (non-void return type ok if consistent with a return statement)
1 correct while loop (while hasNextLine)
5 reading and printing lines assuming even number of lines
3 attempt (doesn't require a loop)
1 reads one line somewhere
1 reads second line somewhere
1 attempt to print both lines in reverse order (this is meant to be
generous...you're mostly looking for a call that prints the second
string with a later call that prints the first string)
2 correct: prints both lines in reverse order (requires a loop)
3 works for odd number of lines (generally atomic, but ask the question leader
if you think someone is close)
Problem #6
Solution:
public static void minToFront(int[] list) {
if (list.length > 0) {
int min = 0;
for (int i = 0; i < list.length; i++) {
if (list[i] < list[min]) {
min = i;
}
}
int temp = list[0];
list[0] = list[min];
list[min] = temp;
}
}
2 header (-1 per error)
1 works (does nothing) for empty array based on a test
2 identifying the min for nonempty array
1 reasonable attempt: initialize min variable to something, compare against
list[i] in a loop resetting min
1 correct (initialize to list[0], check every value of array to see if it
is smaller than min)
2 attempt to swap the min
1 reasonable attempt:
list[0] = min;
list[i] = list[0];
1 correctly swaps list[0] with some other array value
3 correct approach: computing minIndex in a loop and after the loop, swapping
list[0] and list[minIndex] without changing any other values of the array.
No double jeopardy with previous correctness points lost (e.g., min
computation, swap error).
Problem #7
Solutions:
public static void reverse3(ArrayList list) {
for (int i = 0; i < list.size() - 2; i += 3) {
int n1 = list.get(i);
int n3 = list.get(i + 2);
list.set(i, n3);
list.set(i + 2, n1);
}
}
public static void reverse3(ArrayList list) {
for (int i = 0; i < list.size() - 2; i += 3) {
list.add(i, list.remove(i + 2));
list.add(i + 2, list.remove(i + 1));
}
}
1 header (return type ok if consistent with a return statement, parameter must
be ArrayList)
4 loop over triples (ignore errors introduced by reversing code)
1 attempt (some kind of loop from the start of the list to list.size())
1 correct number of iterations (iterates exactly n times where n is the
number of complete triples or uses something like an if to exclude an
incomplete triple)
2 increments an index variable by 3 each time or equivalent
4 reverses each triple (using an index variable, they are manipulating a group
of 3 adjacent elements of the list--for this just grade whether it works for
that tiny neighborhood of 3 values without thinking about loop correctness):
1 reasonable attempt to perform one of the required steps
a call on set that makes sense
a call on both add and remove that makes sense
an array-style assign that makes sense
1 reasonable attempt to perform a second required step
a call on set that makes sense
a call on both add and remove that makes sense
note: here an array-style assign is not okay
2 correct (any combination that works)
1 correct combination of loop and reversal (in other words, they got all of
those correctness points AND the two work together properly)
Notes:
Saying list.size without parens or using brackets (list.get[i], list.remove[i])
are syntax mistakes. They get one free syntax mistake, max -2 for syntax.
-2 for calling set/add/get without saying "list." (only apply this rule if
their score goes up)
extra data structure goes to question leader
Problem #8
Solution:
public class Racoon extends Critter {
private Random r;
private String display;
private int count;
public Racoon() {
r = new Random();
display = "<->";
}
public Action getMove(CritterInfo info) {
count++;
if (info.getFront() == Neighbor.OTHER) {
display = "";
return Action.INFECT;
} else {
int flip = r.nextInt(2);
if (flip == 0) {
display = "";
return Action.LEFT;
} else {
display = "";
return Action.RIGHT;
}
}
}
public Color getColor() {
if (count % 2 == 0) {
return Color.BLUE;
} else {
return Color.RED;
}
}
public String toString() {
return display;
}
}
1 class header
4 fields
1 move counter
1 last move
1 Random or they used Math.random()
1 all fields updated in getMove (requires a field and an update)
4 toString
1 header
1 returns "<->" initially
2 changes correctly after getMove is called
1 attempt: returns a value that is based on a field that they are
trying to use to record the last move
1 correct: field is updated correctly ( for INFECT, for LEFT,
for RIGHT)
3 getMove
1 header
1 has cases for returning INFECT, LEFT, RIGHT
1 correct move (INFECT when enemy in front, otherwise randomly choose
between LEFT and RIGHT)
3 getColor
1 header
1 uses a field to return either Color.RED or Color.BLUE
1 correctly switches between colors using a field starting with blue (they
lose this if they aren't incrementing their move counter once per round)
notes:
-1 if they have code after a return (e.g., setting a field)
-1 if any field is not private
-1 if any field set to non-default outside constructor
Problem #9
Solutions:
public static int[] collapse(int[] list) {
int[] result = new int[list.length / 2 + list.length % 2];
for (int i = 0; i < list.length; i++)
result[i / 2] += list[i];
return result;
}
public static int[] collapse2(int[] list) {
int size = list.length / 2;
if (list.length % 2 == 1) {
size++;
}
int[] result = new int[size];
for (int i = 0; i < list.length / 2; i++) {
result[i] = list[i * 2] + list[i * 2 + 1];
}
if (list.length % 2 == 1) {
result[result.length - 1] = list[list.length - 1];
}
return result;
}
2 header (-1 per error)
4 constructing the new array of appropriate size
1 attempt (construct some new array mostly ignoring syntax)
1 better attempt (array of int, length is within 2 of correct length)
2 correct (array of int, size correct)
6 correctly collapses arrays of even length
1 loop over their result array setting result[i] to something
2 setting result[i] to sum of adjacent values from list
3 correct (this is the hard part--using /2 or *2 or equivalent to match up
appropriate indexes between the two arrays; mostly atomic, but -1 if
OBOB)
2 correctly handles odd case (sets last element of result to last element of
list)
1 returns their answer after a for loop
Notes:
extra data structures go to question leader
ArrayList solutions were allowed to get:
1 construct attempt (construct a new ArrayList)
1 ArrayList equivalent of loop over their result array setting result[i] to
something
2 ArrayLislt equivalent of setting result[i] to sum of adjacent values from
list
1 returns their answer after a for loop
Problem #10
Solutions:
public static String acronym(String s) {
boolean inWord = false;
s = s.toUpperCase();
String result = "";
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (ch == ' ' || ch == '-') {
inWord = false;
} else if (!inWord) {
inWord = true;
result += ch;
}
}
return result;
}
public static String acronym(String s) {
s = s.replaceAll("-", " ");
Scanner words = new Scanner(s);
String result = "";
while (words.hasNext()) {
String word = words.next();
result += word.toUpperCase().charAt(0);
}
return result;
}
(not very good) Plus criteria (max +4)
Good things that each get 1 point:
1 header
1 String s = "";
1 for (int i = 0; i < s.length(); i++) {...}
1 s += something.charAt(something) or something.substring(something)
1 convert to uppercase
1 return s at the end
-------------------------------------------------------------------------------
(pretty good) Minus criteria (min + 5)
Grade this way if you see:
constructing a Scanner from the string, calling next
testing adjacent characters for space/dash versus !space/dash
Without the string replace, the Scanner solution gets 5 points
-0 s.length vs s.length(), string/char confusion (==, .equals)
-1 charAt or length instead of s.charAt, s.length
-1 OBOB
-1 testing whether character is null instead of ' '
-1 didn't return result
-2 fails on tokens composed of just dashes
-2 fails on strings that begin with a letter
-2 fails on strings that end with a dash
-2 switching || and &&
-4 requires mutable strings...attempting to do String replace for a Scanner
solution by saying something like:
s.charAt(i) = ' ';
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011