Problem #4 Solution: public static double evaluate(Scanner input) { double result = input.nextDouble(); while (input.hasNext()) { String operator = input.next(); double num = input.nextDouble(); if (operator.equals("+")) { result += num; } else { // operator.equals("-") result -= num; } } return result; } 1 header 1 reads initial double 2 while loop 1 attempt (some kind of hasNextSomething) 1 correct (has to be hasNext()) 2 reads operator and number inside loop 1 attempt (2 variables, 2 calls on nextSomething) 1 correct (correct types, correct order, next()/nextDouble()) 3 adds/subtracts number 1 attempt (attempt at testing for +/-, attempt at adding/subtracting) 1 correct comparison (String comparison using equals) 1 all correct 1 returns result Problem #5 Solution: public static void processScores(Scanner input) { while (input.hasNextLine()) { String name = input.nextLine(); String data = input.nextLine(); int plus = 0; int count = 0; for (int i = 0; i < data.length(); i++) { count++; if (data.charAt(i) == '+') { plus++; } } double percent = 100.0 * plus / count; System.out.println(name + ": " + percent + "% plus"); } } 1 header (return type ok if consistent with a return statement) 1 correct while loop (while hasNext or while hasNextLine) 2 reads 2 lines of input 1 attempt (2 variables, 2 calls on nextSomething) 1 correct (correct types, correct order, nextLine()/nextLine()) 3 counting pluses (or counting minuses) 1 attempt at counting : counter variable, test for +/-, increment 1 attempt to loop over second string, calling something like charAt 1 correctly computes the count (lose this point if they fail to reset count for each person) 2 computes/prints percent 1 reasonable attempt (requires counting attempt, ratio of count to str length or to a 2nd counter, some kind of println) 1 correct formula and correct format for println Problem #6 Solution: public static boolean isFibLike(int[] list) { for (int i = 2; i < list.length; i++) { if (list[i] != list[i - 2] + list[i - 1]) { return false; } } return true; } 1 header 3 for loop over array 1 attempt (for loop that iterates around list.length times) 1 loop iterates (list.length-1) or (list.length-2) or (list.length-3) times 1 loop iterates exactly (list.length-2) times 4 testing fib properly (just the test) 1 attempt (comparing something1 against something2 + something3) 3 correct (list[i + 2] == list[i] + list[i + 1] or equivalent; -1 if there is a small discrepancy with loop variable like an OBOB) 2 correctly returns true/false based on fib test (requires test attempt, but otherwise assume test works). This is generally atomic but can be worth 1 point for solutions that are close. For example, if their only error is that they forgot "return true" after the loop, they can get 1 of these points. If they switch true/false or if they prematurely return true, they lose both points. Ask the question leader if you see something that might be close. Note: We generally ignore code like this: if (list.length <= 2) { return true; } unless it screws up an otherwise correct answer, in which case they lose 1 point off the top. Problem #7 Solution: public static void reverse3(ArrayList<Integer> 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<Integer> 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)); } } 2 header (-1 per error, return type ok if consistent with a return statement, ArrayList<int> loses a point, plain ArrayList is okay if they cast to int when they call get) 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 increments by 3 each time or equivalent 1 doesn't include extras at the end 1 all correct (all of the above plus correct bounds, etc) 4 reverses each triple 2 attempt (2 calls on set or 2 calls on add/remove or 1 call on each) 2 correct (-1 if OBOB or otherwise close) If they don't have the 2 points for attempt above: 1 for a single set that makes sense 2 for a single add/remove that makes sense Notes: Other than the header (which requires ArrayList), take off 1 point for each case of array notation versus ArrayList notation for a max of -2. Problem #8 Solution: public class Orca extends Critter { int count; public Orca() { count = 0; } public Action getMove(CritterInfo info) { if (count % 6 == 4 || count % 6 == 5) { count++; return Action.LEFT; } else if (info.getFront() != Neighbor.EMPTY) { return Action.INFECT; } else { count++; return Action.HOP; } } public String toString() { if (count % 6 < 4) { return "M"; } else { return "T"; } } } 3 correct headers 1 class header 1 getMove header 1 toString header 4 getMove attempt 1 getMove sometimes returns Action.INFECT based on a test that the front is blocked (not EMPTY or is some kind of obstacle) 1 getMove sometimes returns Action.HOP 1 getMove sometimes returns Action.LEFT 1 class has a field that is used in getMove to count the number of moves 2 toString attempt 1 toString sometimes returns "M" and sometimes returns "T" 1 class has a field that is being used in toString to distinguish between moving-mode and turning-mode (could be count, boolean, String) 4 correct when there are no obstacles 1 getMove correctly moves forward 4 and only 4 times 1 then getMove turns left exactly 2 times 1 then the cycle properly resets 1 toString correctly displays M vs T when moving vs turning (no double-jeopardy with previous mistakes--this is about toString correctly reflecting the decisions that getMove is making) 2 obstacles handled properly (requires some kind of counter to keep track of how many moves have been made, although counter could be local) 1 within the first 4 moves, it sometimes infects when it sees an obstacle and it sometimes moves) 1 perfect (always infects any kind of obstacle while in moving mode, never infects an obstacle while in turning mode) Ignore any getColor method unless it's otherwise a 15 and they somehow break getColor (should return Color.BLACK or super.getColor()), then it's -1. Problem #9 Solution: public static int[] append(int[] list1, int[] list2) { int[] result = new int[list1.length + list2.length]; for (int i = 0; i < list1.length; i++) { result[i] = list1[i]; } for (int j = 0; j < list2.length; j++) { result[list1.length + j] = list2[j]; } return result; } 2 header (-1 per error) 3 constructing the new array 1 attempt (construct some new array) 1 better attempt (array of int, length within 2 of being right) 1 correct (array of int, size correct) 4 copying values from list1 into new array 1 attempt to loop over list1 3 correct (-1 if OBOB) 5 copying values from list2 into new array 1 attempt to loop over list2 1 attempt to copy: result[something] = list2[something] or list1[something] = list2[something] 3 correct (-1 if OBOB) 1 returns their answer Notes: Students might construct an ArrayList<Integer> or might call methods like Arrays.copyOf to solve this problem. This can get full credit. When in doubt, ask the question leader. For someone who doesn't construct a new array and instead tries to append to the first array, they lose "new array" points, "copy list1" points, but can get attempt for "copy list2" and can get a point for returning the array they are appending to. Problem #10 Solution: 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; } (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 a loop over the string that tests for spaces and/or dashes
Stuart Reges
Last modified: Fri Sep 16 16:12:06 PDT 2011