Problem #1 Expression Value --------------------------------------------------- 2 + 3 * (2 + 3) + 17 / 4 21 67 % 10 % 5 * 6 - 3 % 6 9 7.5 * 2 + 19 / 2 / 2.0 - 0.5 - 1 / 2 19.0 4 + 2 * 2 + "2 * 2" + 2 * 2 + 4 "82 * 244" 43 / 4 / 3 * 5.0 / 2 + 5 / 2 9.5 1 point each, type has to match (2.0 vs 2, using quotes for string) ------------------------------------------------------------------------------- Problem #2: Original List Final List -------------------------------------------------------------- {1} {1} {2, 5, 9} {2, 7, 9} {2, 3, 7, 2} {2, 5, 2, 2} {2, 4, 5, 4, 3} {2, 6, -1, 3, 3} {5, 1, 8, 4, 8, 4} {5, 6, 2, 6, 2, 4} 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 a d b c 1 a 1 d 1 c 1 d 2 d 2 d 2 d 2 1/2 point each, round up their score -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 per extra column or row (max -2) -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 -0 for solid lines between that separate columns or rows ------------------------------------------------------------------------------- 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 value with nextDouble 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(), always reads 2 tokens on each loop iteration) 3 adds/subtracts number 1 attempt (attempt at testing for +/-, attempt at adding/subtracting) 1 correct comparison (String comparison using equals) 1 all correct ignoring == vs equals 1 returns result notes: only take off once for using nextInt instead of nextDouble Problem #5 Solutions: public static int analyzeParagraphs(Scanner input) { int max = 0; while (input.hasNextLine()) { String line = input.nextLine(); int count = 0; while (!line.equals("<p>")) { 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("<p>")) { 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 "<p>" or if/else that handles "<p>" 1 attempt (inner loop or if/else with some kind of "<p>" test or "<" test) 1 correctly counting number of lines in each paragraph 1 correct comparison (requires equals instead of ==) 1 println for "<count>-line paragraph": printed once for each paragraph, but no double jeopardy with counting correctness and comparison correctness 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 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 array length) 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: // set/remove with calls on get public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { int sum = list.get(i) + list.get(i + 1); list.set(i, sum); list.remove(i + 1); } } // add/remove/remove, using remove instead of get public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { list.add(i, list.remove(i) + list.remove(i)); } } // set/remove, using remove instead of one get public static void collapse(ArrayList<Integer> list) { for (int i = 0; i < list.size() - 1; i++) { list.set(i, list.remove(i) + list.get(i)); } } 1 header (return type ok if consistent with a return statement, parameter must be ArrayList<Integer>) 3 loop over pairs 1 attempt (loop using list.size() or list.length() or list.length) 1 handles odd case properly (same number of iterations for 2n vs 2n+1) 1 correct for even case: * requires using list.size * loops n times for size of 2n either with an i++ loop that has a clear attempt to remove something near i or an i+=2 loop with no indication of removing near i * correct alignment with i starting at 0 and dealing with i/i+1 or i starting at 1 and dealing with i-1/i 2 correctly computes the sum of adjacent values from the list 1 attempt (array syntax okay) 1 correct (must be get or remove and must correctly add adjacent values although they don't have to be the correct pairs) 4 replaces each pair with sum (lose all 4 of these points for array syntax) 1 reasonable attempt to replace one value with the sum either by calling add/remove or set 1 reasonable attempt to call remove on second value 2 correct for length 2n assuming minor loop problems have been fixed and assuming sums are correct (in other words, we're figuring out whether they are using an appropriate combination of calls on add, remove, get, and set, and making sure the pieces fit together assuming we fix minor problems with their loop and with their sum) Notes: If they construct a second ArrayList or other structure, take off 4 "replace" points but allow them points for loop structure and computing sums. Saying list.size without parens or using brackets on method calls (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/remove without saying "list." (only apply this rule if their score goes up) extra data structure goes to question leader Problem #8 Solution: public class Counter extends Critter { private int count; private int max; public Counter(int n) { count = 1; max = n; } public Action getMove(CritterInfo info) { if (count < max) { count++; } else { count = 1; } if (info.getFront() == Neighbor.OTHER) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else return Action.LEFT; } public Color getColor() { return Color.BLUE; } public String toString() { return count + "/" + max; } } 1 class header 4 fields/constructor 1 field to count moves (declared, incremented somewhere) 1 field to keep track of max (declared, used somewhere) 1 reasonable attempt to set fields so that initial display is "1/max" 1 constructor has max parameter and correctly sets fields so that initial display is "1/max" 4 correctly updates state 1 reasonable attempt to increment count 1 reasonable attempt to reset count when it equals max 2 correctly updates count to cycle 1 through max (-1 if close as in OBOB) 2 toString 1 reasonable attempt to return (count) + "/" + (max) 1 all correct 3 getMove 1 header 1 uses tests to return either Action.INFECT or Action.HOP or Action.LEFT 1 all correct other than header 1 getColor (correct header, returns Color.BLUE) overal deductions (can lose any given point just once, ask question leader if they seem to be losing lots of points for this or if it feels like double jeopardy): -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 -1 if they update any fields outside of getMove -1 if they have a local variable in their constructor that shadows a field -1 if they make a field static Problem #9 Solutions: public static int[] combineSublists(int[] list1, int[] list2, int start, int stop) { int sublistLength = stop - start; int[] result = new int[2 * sublistLength]; for (int i = start; i < stop; i++) { result[i - start] = list1[i]; result[i - start + sublistLength] = list2[i]; } return result; } public static int[] combineSublists(int[] list1, int[] list2, int start, int stop) { int[] result = new int[2 * stop - 2 * start]; int index = 0; for (int i = start; i < stop; i++) { result[index++] = list1[i]; } for (int i = start; i < stop; i++) { result[index++] = list2[i]; } return result; } public static int[] combineSublists(int[] list1, int[] list2, int start, int stop) { int len = stop - start; int[] result = new int[len + len]; for (int i = 0; i < len; i++) { result[i] = list1[i + start]; } for (int i = len; i < 2 * len; i++) { result[i] = list2[i - len + start]; } return result; } 2 header (-1 per error) 3 constructing a new int 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) 1 correct (array of int, size correct) 1 reasonable attempt to copy values from list1 and list2 to a result array 4 copying from list1 to result with code like the following: result[expression1] = list1[expression2]; 2 correct number of iterations: code is inside a loop that executes (stop - start) times (-1 if obob) 2 correct expressions (-1 if obob or otherwise close): * expression2 - expression1 = start * expression1 varies 0 <= expression1 < (stop-start) 4 copying from list2 to result with code like the following: result[expression1] = list2[expression2]; 2 correct number of iterations: code is inside a loop that executes (stop - start) times (-1 if obob) 2 correct expressions (-1 if obob or otherwise close): * expression2 - expression1 = 2 * start - stop * expression1 varies len <= expression1 < 2 * len 1 returns their answer (requires construct attempt) 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. ArrayList solutions were allowed to get: 1 construct attempt (construct a new ArrayList) 2 loop bounds if you see indication of list1 values going into result 2 loop bounds if you see indication of list2 values going into result 1 returns their answer after a for loop 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; } } (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)
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011