Problem #1 Expression Value --------------------------------------------------- 43 % 15 / 3 + 15 / 2 11 6.2 * 5 / 10 + 3.5 6.6 6 * 2.5 / 4 + (2.3 + 2.7) / 4 5.0 "18" + 3 * 4 + (8 + 5) "181213" 59 % 10 / (2 + 2) * 2.5 / 2 2.5 1 point each, type has to match (2.0 vs 2, using quotes for string) ------------------------------------------------------------------------------- Problem #2: Original List Final List -------------------------------------------------------------- {8, 3} {9, 3} {2, 5, 4} {3, 5, 4} {3, 4, 5, 7} {4, 4, 6, 7} {2, 5, 4, 2} {3, 5, 5, 2} {4, 5, 7, 9, 3} {5, 5, 8, 9, 3} 2 points for each answer -1 for each wrong number -1 for each missing number any given answer can't go negative ------------------------------------------------------------------------------- Problem #3: b b c b c 1 c 1 c 1 d 1 a 2 b 2 c 2 b 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 switchData(Scanner input) { String title = input.next(); System.out.print(title); while (input.hasNextInt()) { int n1 = input.nextInt(); if (input.hasNextInt()) { int n2 = input.nextInt(); System.out.print(" " + n2); } System.out.print(" " + n1); } System.out.println(); } 1 header (non-void return type ok if consistent with a return statement) 1 reads/prints title (println okay) 1 correct while loop (while hasNext or while hasNextInt) 1 reads two numbers inside loop for either even or odd case 3 correctly prints the two numbers 1 attempt (two print or println statements) 2 correct order (reversed) 2 properly handles case with odd number of numbers 1 attempt 1 correct 1 attempt to print all elements (title, two numbers) and all formatting correct (all prints, spaces between numbers, println after loop) Problem #5 Solution: public static void reportBlankLines(Scanner input) { int line = 0; int count = 0; while (input.hasNextLine()) { String text = input.nextLine(); line++; if (text.length() == 0) { System.out.println("line " + line + " is blank"); count++; } } System.out.println("total blank lines = " + count); } 1 header (non-void return type ok if consistent with a return statement) 1 correct while loop (while hasNextLine) 1 reads a line using nextLine() and either stores it or immediately tests it for empty string (doesn't have to be a correct test for this point) 2 counter to keep track of line number 1 attempt 1 correct 2 report each blank line 1 attempt 1 correct (requires correct test: line.length()==0 or line.equals("")) 2 reports total number of blank lines 1 attempt (requires 2 of: initialize counter, properly increments counter, prints value of counter after loop) 1 correct Problem #6 Solution: public static boolean isPalindrome(int[] list) { for (int i = 0; i < list.length / 2; i++) { if (list[i] != list[list.length - 1 - i]) { return false; } } return true; } 2 header (-1 per error) 2 for loop 1 attempt (some for loop over arrray size) 1 correct (length/2 <= iterations < length) 4 correct comparison of corresponding pairs 1 attempt (they test whether list[something] equals list[something else]) 3 correct (normally atomic, -1 if OBOB, show to question leader if you think something else deserves partial credit) 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 split(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i += 2) { int n = list.get(i); list.set(i, n / 2 + n % 2); list.add(i + 1, n / 2); } } public static void split(ArrayList<Integer> list) { for (int i = 0; i < list.size(); i += 2) { int n = list.remove(i); list.add(i, n / 2 + n % 2); list.add(i + 1, n / 2); } } 1 header (return type ok if consistent with a return statement, parameter must be ArrayList<Integer>) 4 loop over list 1 attempt (for loop using list.size() or list.length() or list.length) 1 correct bounds (starting at 0, going while < list.size(), must be size()) 2 increment i by 2 or loop goes backwards 2 expressions/code for splitting list.get(i) into two parts including odd case 1 attempt (testing value % 2, finding value / 2, array syntax okay) 1 correctly splits list.get(i) (must be get, must handle odd case, must be going into structure in correct order in odd case) 3 correctly replaces value with two new values 1 reasonable 2-step attempt using ArrayList methods including: add/add set/set array-style assign followed by add get(i) assign followed by add a single add or single set would not count 2 correct approach using ArrayList methods (set/add, remove/add/add)--can get 1/2 if they sometimes do it right 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 Lizard extends Critter { private int count; private boolean increasing; public Lizard() { count = 1; increasing = true; } public Action getMove(CritterInfo info) { if (increasing) { count++; if (count == 10) { increasing = false; } } else { count--; if (count == 1) { increasing = true; } } if (info.getFront() == Neighbor.OTHER) { return Action.INFECT; } else if (info.getFront() == Neighbor.EMPTY) { return Action.HOP; } else return Action.RIGHT; } public Color getColor() { return Color.GREEN; } public String toString() { return "<" + count + ">"; } } 1 class header 7 appropriate state variables to keep track of cycle 1 field to count moves (declared, incremented somewhere) 1 all updates of fields are done in getMove 1 initial display is "<1>" (before getMove is called) 4 correct pattern for a series of calls to getMove: 2 attempt (state variable(s) that allow them to either increment or decrement their count and they reset when count is close to 10) 2 correct (picky) notes: -1 for setting fields to non-zero equivalent outside constructor -1 for non-private fields 2 toString 1 header 1 returns "<" + (a counter) + ">" (requires counter field) 3 getMove 1 header 1 uses tests to return either Action.INFECT or Action.HOP or Action.RIGHT 1 all correct other than header 2 getColor 1 header 1 correct (returns Color.GREEN) Problem #9 Solution: public static int[] interleave(int[] list1, int[] list2) { int min = Math.min(list1.length, list2.length); int[] result = new int[2 * min]; for (int i = 0; i < min; i++) { result[2 * i] = list1[i]; result[2 * i + 1] = list2[i]; } return result; } 2 header (-1 per error) 4 constructing the new array of size 2 * (min length) 1 attempt (construct some new array mostly ignoring syntax) 1 better attempt (array of int, length is either correct or is the sum of the two array lengths) 2 correct (array of int, size correct) 8 copying values from original arrays to new array 1 result[something] = list1[something] 1 result[something] = list2[something] 2 loop that leads to 2 * result.length assignments to result 4 correctly copies values from the two lists to the new array 2 attempt (don't often get this; must involve either * 2 or / 2 or incrementing a counter twice in a loop) 2 correct 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. ArrayList solutions were allowed to get: 1 construct attempt (construct a new ArrayList) 1 ArrayList equivalent of result[something] = list1[something] 1 ArrayList equivalent of result[something] = list2[something] 1 ArrayList equivalent of loop that leads to 2 * result.length assignments to result 1 returns their answer after a for loop Problem #10 Solutions: // startsWith approach public static int count(String target, String source) { target = target.toUpperCase(); source = source.toUpperCase(); int count = 0; for (int i = 0; i < source.length(); i++) { if (source.substring(i).startsWith(target)) { count++; } } return count; } // substring approach public static int count2(String target, String source) { int count = 0; for (int i = 0; i < source.length() - target.length() + 1; i++) { String sub = source.substring(i, i + target.length()); if (sub.equalsIgnoreCase(target)) { count++; } } return count; } // indexOf approach public static int count3(String target, String source) { target = target.toUpperCase(); source = source.toUpperCase(); int count = 0; while (source.length() > 0) { int pos = source.indexOf(target); if (pos == -1) { return count; } else { count++; source = source.substring(pos + 1); } } return count; } // CharAt approach public static int count4(String target, String source) { target = target.toUpperCase(); source = source.toUpperCase(); int count = 0; for (int i = 0; i < source.length() - target.length() + 1; i++) { boolean same = true; for (int j = 0; j < target.length(); j++) { if (target.charAt(j) != source.charAt(i + j)) { same = false; } } if (same) { count++; } } return count; } } } (not very good) Plus criteria (max +3) Good things that each get 1 point: 1 header 1 for loop using source length 1 count = 0 1 count++ 1 return count 1 substring of source or charAt 1 startsWith 1 .equals or .equalsIgnoreCase 1 toUpperCase or toLowerCase ------------------------------------------------------------------------------- (pretty good) Minus criteria (min + 4) Grade this way if you see: comparing target to substring of source using startsWith with substring of source using charAt with an inner loop using indexOf and substring or 2-argument indexOf anything else that you think might work, show to Stuart Deductions: -1 OBOB -2 string index out of bounds
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011