Problem #4 Solution: public static void showSums(Scanner input) { int count = 0; int sum = 0; while (input.hasNextInt()) { sum += input.nextInt(); count++; System.out.println("Sum of " + count + " = " + sum); } double average = (double) sum / count; System.out.println("Average = " + average); } 1 header (non-void return type ok if consistent with a return statement) 1 correct while loop (while hasNext or while hasNextInt) 2 sum computation (requires loop) 1 attempt (some attempt to add next value to sum) 1 correct (correct initialization, adds input.nextInt() to sum, order is correct for println) 2 count computation (requires loop) 1 attempt (some attempt to keep a count of values) 1 correct (correct initialization, increments once per loop iteration, order is correct for println) 2 println for "sum of" (requires sum and count variables, but they don't have to have correct values and this doesn't require a loop) 1 attempt (at least one of the variables) 1 correct (both variables, type int or cast to int, correct format) 2 computing/printing average 1 attempt (either correctly computes or correctly prints or reasonable attempt at both, doesn't require loop) 1 correct (requires loop, requires sum and count variables, requires double or cast to double, assume sum and count have correct values) Problem #5 Solution: public static void underline(Scanner input) { while (input.hasNextLine()) { String text = input.nextLine(); if (!text.startsWith(".")) { System.out.println(text); } else { System.out.println(text.substring(1)); for (int i = 1; i <= text.length() - 1; i++) { System.out.print("-"); } System.out.println(); } } } 1 header 1 correct while loop (while hasNextLine) 1 reads and stores a line using nextLine() 1 sometimes echos a line with System.out.println (requires reading point) 5 correctly handles lines with dots 1 reasonable attempt to test for dot (startsWith or charAt/substring to access first character) 1 reasonable attempt to print all but dot (substring or loop) 1 reasonable attempt to print line of dashes (for loop printing dashes) 1 at least 2 of these are correct 1 all three are correct and non-dot lines are echoed correctly and no extraneous output (this is the nit-picky uber-correctness point) Notes: - charAt/substring throw an exception for empty strings (blank lines)--this is part of correctness - It will be common for them to be off by one for the number of dashes (length of original line versus line minus the dot)--this is part of correctness - Ignore any code for special case of a line with just a period. Problem #6 Solutions: public static boolean hasAlternatingParity(int[] list) { for (int i = 0; i < list.length - 1; i++) { if (list[i] % 2 == list[i + 1] % 2) { return false; } } return true; } public static boolean hasAlternatingParity(int[] list) { if (list.length > 0) { int previous = list[0] % 2; for (int i = 1; i < list.length; i++) { int next = list[i] % 2; if (previous == next) { return false; } previous = next; } } return true; } 2 header (-1 per error) 2 loop structure and initialization to be able to compare adjacent elements (or compare current against previous) 1 attempt (some for loop over arrray size) 1 correct (either loop n-1 times or store previous and correctly handle empty array) 4 adjacent parity test 2 attempt (probably atomic) 2 correct (-1 if close--ask question leader) 2 true/false/return structure: correctly returns true/false based on a test in a loop (requires some kind of test in a loop, but 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 get 1 point. Or their only error might be to fail to return true for an array of length less than 2, in which case they get 1 point. 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. Problem #7 Solutions: public static void switchPairs(ArrayList<String> list) { for (int i = 0; i < list.size() - 1; i += 2) { String first = list.remove(i); list.add(i + 1, first); } } public static void switchPairs(ArrayList<String> list) { int i = 0; while (i < list.size() - 1) { String first = list.get(i); list.set(i, list.get(i + 1)); list.set(i + 1, first); i += 2; } } 1 header (return type ok if consistent with a return statement, parameter must be ArrayList<String>) 5 loop over list 1 attempt (for loop using list.size() or list.length() or list.length) 2 increment is i+=2 or equivalent 1 correct bounds for even length list (iterates n/2 times) 1 correct bounds for odd length list (iterates n/2 times) 4 code to switch a pair 2 reasonable attempt: get/get/set/set or get/remove/add or equivalent note: remove returns value being removed; set returns old value 2 correct (order, i vs i+1, etc; probably atomic) note: if they call set/add/get/remove without saying "list.", they can get attempt but not correct Notes: -0 for list.get[i] instead of list.get(i) Problem #8 Solution: public class Chameleon extends Critter { private int count = 0; public Action getMove(CritterInfo info) { count++; if (count % 3 == 1) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else { return Action.RIGHT; } } public Color getColor() { if (count % 3 == 0) { return Color.RED; } else if (count % 3 == 1) { return Color.WHITE; } else { return Color.BLUE; } } public String toString() { if (count % 3 == 0) { return "R"; } else if (count % 3 == 1) { return "W"; } else { return "B"; } } } 2 headers: class header, toString header, getMove header, getColor header 1 one of four correct 1 four of four correct 2 move counter declared as a field 1 attempt 1 correct (private) 6 basic cycle 1 toString has code to sometimes returns "R" or "W" or "B" 1 getColor has code to sometimes return Color.RED or Color.WHITE or Color.BLUE 1 there is a certain state of the critter in which it always infects (involves some kind of test on fields) 1 there is a certain state of the critter in which it hops if it can and turns right if it can't hop 1 these choices are made using (mod 3 of a move counter) or (exact move values AND resetting a move counter) 1 all of these cycle in the proper order (what matters is the sequence of the cycle--red, white, blue--not where it starts) note: lose up to two of these points for failing to use Color.<whatever> and Action.<whatever> and Neighbor.<whatever> 5 move counter is properly updated 1 incremented somewhere 1 incremented exactly once each time getMove is called 3 no boundary errors (these are deep correctness points) simulate call on toString, then getColor, then getMove, repeat step 0: red R that infects step 1: white W that does hop/right step 2: blue B that does hop/right step 3: red R that infects ... Problem #9 Solution: public static int[] doubleSize(int[] list) { int[] result = new int[2 * list.length]; for (int i = 0; i < list.length; i++) { result[2 * i] = list[i] / 2 + list[i] % 2; result[2 * i + 1] = list[i] / 2; } return result; } 2 header (-1 per error) 2 constructing the new array 1 attempt (construct some new array) 1 correct (array of int, size correct) 10 filling in new array 2 for loop over new array 1 attempt (any for loop will do) 1 correct (one iteration per new element or one iteration per old element if loop body has two assignments to the new array)- 4 copying from old array to result 1 attempt: result[something] = list[something] 3 correct: list[i] -> result[2*i] AND result[2*i+1] result[i] coming from list[i/2] (-1 if OBOB) 3 numbers stored in new array are half of original 1 attempt 2 correct, including odd case 1 returns their answer Notes: - If students manipulate the array as if it were an ArrayList, they automatically lose the 3 array correctness points and they lose the loop bounds correctness if they use list.size() instead of list.length, but they can get other points (array construction still has to be an array). - Students weren't supposed to use ArrayList or any Arrays methods--show such solutions to question leader. Problem #10 Solution: public static boolean hasTwoPair(int[] data) { int[] counts = new int[6]; for (int i = 0; i < data.length; i++) { counts[data[i] - 1]++; } int num = 0; for (int i = 0; i < counts.length; i++) { if (counts[i] == 2) { num++; } } return num == 2; } (not very good) Plus criteria (max +3) Good things that each get 1 point: 1 header 1 for loop using array length 1 comparing two array elements for equality 1 initializing a counter to 0 1 incrementing a counter 1 testing if a counter is 2 1 returning true/false depending upon whether some counter is 2 ------------------------------------------------------------------------------- (pretty good) Minus criteria (min + 4) Grade this way if you see: array of 6 or 7 counters 6 counter variables an ArrayList<Integer> with 6 or 7 values Arrays.sort and compare adjacent values for equality/nonequality equivalent: for example, finding matches and setting values to 0
Stuart Reges
Last modified: Fri Sep 16 16:13:47 PDT 2011