Problem #1 Expression Value ----------------------------------------------- 13 + 2 * 5 / 3 16 2.5 * 2 * 5 / 10 + 1.5 4.0 85 % 10 + 4 % 10 - 17 % 3 7 2 + 3 + "." + (3 + 4) + 2 * 3 "5.76" 482 / 10 / 5 / 2.0 * 2 + 14 / 5 11.0 2 points each, 1 point for correct value of wrong type (missing quotes, no .0) ------------------------------------------------------------------------------- Problem #2: solution: she can't take she with her it can't take her with you you can't take it with you fred can't take her with him 1 point for each of the 12 values -1 off the top if any error in surrounding text (no dittos, quotes, labels) -4 if two columns transposed -8 if you need to transpose twice okay to use underscore for space ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(2, 5); 4 2 ifElseMystery(5, 5); 10 20 ifElseMystery(6, 3); 3 13 ifElseMystery(4, 4); 8 9 ifElseMystery(3, 8); 5 15 ifElseMystery(9, 5); 6 5 each number is worth a point -2 overall if columns transposed -1 off the top if formatting error (e.g., commas between numbers, quotes) okay to use underscore for space ------------------------------------------------------------------------------- Problem #4: Method Call Output Produced --------------------------------------- mystery(2); 1 2 mystery(5); 1 5 mystery(9); 2 3 mystery(12); 3 3 column 1 is 1 point, column 2 is 2 points -2 overall if columns transposed -1 off the top if formatting error (e.g., commas between numbers, quotes) -1 if individual pair is reversed okay to use underscore for space Problem #5: y == 0 y % 2 == 0 z == 0 +---------------------+---------------------+---------------------+ Point A | never | always | always | +---------------------+---------------------+---------------------+ Point B | never | always | sometimes | +---------------------+---------------------+---------------------+ Point C | sometimes | always | never | +---------------------+---------------------+---------------------+ Point D | never | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | always | sometimes | +---------------------+---------------------+---------------------+ 1 point per answer Problem #6 Solution: public static void spinWheel(Random r, int n) { int spin = r.nextInt(5) * 10 + 20; System.out.print("spins: " + spin); int count = 0; if (spin == 20) { count++; } int totalSpins = 1; while (count < n) { spin = r.nextInt(5) * 10 + 20; totalSpins++; System.out.print(", " + spin); if (spin == 20) { count++; } else { count = 0; } } System.out.println(); System.out.println(n + " in a row after " + totalSpins + " spins"); } 1 correct header (non-void return okay if they return something of that type) 2 correct use of Random object 1 reasonable attempt to use Random object to produce spins 1 correct expression to produce a spin at least once 5 loop to generate random numbers until n-in-a-row 1 correct loop structure: while loop that generates a new random spin each time, loops until count is n (lose this if they use an initial value that is not random) 1 correctly counts total spins 1 reasonable attempt to increment for 20 OR reset for non-20 2 correctly increments for 20 AND resets for non-20 AND correct initialization (-1 if one minor error like missing initialization or not testing the first value) 3 solved fencepost problem 2 attempt: print first spin before a loop or the last spin after a loop or test for when to print comma (atomic) 1 correct (prints each spin exactly once separated by commas) 4 printing the required three elements (ignore fencepost issue) 1 print or println for 2 of the 3 elements: * "spins: " * spin (ignore comma except for the format point below) * n + " in a row after " + totalSpins + " spins" 1 reasonable attempt at print or println for all 3 elements (could be just one of n/totalSpins for third element) 1 all 3 elements present and pieces fit together: "spins" printed once before loop, spin printed once inside loop, "in a row" printed once after loop including both n and totalSpins 1 all 3 elements present with correct format: text is all correct, print/println is correct, commas are correct solution that prints solves the fencepost by printing 20 after the loop: public static void spinWheel(Random r, int n) { System.out.print("spins: "); int count = 0; int totalSpins = 0; while (count < n) { int spin = r.nextInt(5) * 10 + 20; totalSpins++; if (spin == 20) { count++; } else { count = 0; } if (count < n) { System.out.print(spin + ", "); } } System.out.println(20); System.out.println(n + " in a row after " + totalSpins + " spins"); } Problem #7 Solution: public static boolean balanceCheckbook(Scanner console) { System.out.print("initial balance? "); double balance = console.nextDouble(); System.out.print("how many transactions? "); int count = console.nextInt(); double min = balance; for (int i = 1; i <= count; i++) { System.out.print(i + "/" + count + " amount? "); double amount = console.nextDouble(); balance = balance + amount; System.out.println("new balance = $" + balance); if (balance < min) { min = balance; } } System.out.println("minimum balance = $" + min); return (min < 0); } There is an overall printing correctness point for print vs println and minor formatting issues like forgetting a dollar sign (ignore elsewhere). 1 correct header 2 prompt/read for initial balance and transaction count 1 reasonable attempt to prompt and read these two values (int/double okay) 1 correct prompt/read for these two values (balance is of type double and read with nextDouble, count is of type int and read with nextInt) 3 loop that prompts and reads the given number of transaction amounts 1 reasonable attempt for both the loop and the prompt/read (int okay) 1 prompt includes i and count to produce text like "3/5" 1 correct: correct loop structure, amount stored in a variable of type double and read with nextDouble 3 compute & print min balance 1 reasonable attempt to initialize and update min balance (int is okay) 1 correctly initialize min to initial balance and correct update in a loop after balance is updated (variable must be double) 1 prints minimum balance after a loop 3 update & print balance 1 reasonable attempt to update the balance using the amount 1 reasonable attempt to print the new balance 1 correctly updates balance, prints new balance after update inside a loop 1 All printing elements exist (3 prompts, new balance, min balance) and formatted correctly including use of print/println 2 returns whether account was overdrawn (lose this is they return early) 1 reasonable attempt to test for this at the end of their method 1 correct (testing <= 0 is okay) Notes: -1 if they construct a new Scanner in the method even if they lost their header point, but only take off this point if they got at least one point for using the Scanner for prompt/read Problem #8 Solutions: public static boolean sameDashes(String str1, String str2) { for (int i = 0; i < str1.length(); i++) { if (str1.charAt(i) == '-') { if (i > str2.length() || str2.charAt(i) != '-') { return false; } } } for (int i = 0; i < str2.length(); i++) { if (str2.charAt(i) == '-') { if (i > str1.length() || str1.charAt(i) != '-') { return false; } } } return true; } public static boolean sameDashes(String str1, String str2) { int i1 = str1.indexOf('-'); int i2 = str2.indexOf('-'); while (i1 != -1 || i2 != -1) { if (i1 != i2) { return false; } str1 = str1.substring(i1 + 1, str1.length()); str2 = str2.substring(i2 + 1, str2.length()); i1 = str1.indexOf('-'); i2 = str2.indexOf('-'); } return true; } public static boolean sameDashes(String s1, String s2) { int length = Math.min(s1.length(), s2.length()); boolean bad = s1.substring(length).contains("-") || s2.substring(length).contains("-"); for (int i = 0; i < length; i++) { if (s1.charAt(i) == '-' && s2.charAt(i) != '-') { bad = true; } else if (s1.charAt(i) != '-' && s2.charAt(i) == '-') { bad = true; } } return !bad; } public static boolean sameDashes(String s1, String s2) { String r1 = "", r2 = ""; for (int i = 0; i < s1.length(); i++) { if (s1.charAt(i) == '-') { r1 += "." + i; } } for (int i = 0; i < s2.length(); i++) { if (s2.charAt(i) == '-') { r2 += "." + i; } } return r1.equals(r2); } Can get up to 3 points for any of the following good things: 1 correct header 1 for loop involving s1.length or s2.length 1 second for loop involving other string length 1 s.charAt(i) or s.substring(i, i + 1) for one string 1 s.charAt(i) or s.substring(i, i + 1) for other string 1 s.indexOf("-") or s.indexOf("-") or s.contains("-") return false inside a loop based on a relevant test return true after a loop ------------------------------------------------------------------------------- To make it to round 2, they must have either: * a loop that compares one character from each of the two strings that are in the same position to see if one is a dash and one is not a dash * a loop that uses a method like indexOf to find the location of the next dash in each of the two strings and compares the indexes to make sure they are equal ------------------------------------------------------------------------------- To make it to round 3, they must have either: * a loop that tests dash/non-dash in both directions * a loop that tests dash/non-dash in one direction and correctly handling BOTH kinds of longer string ------------------------------------------------------------------------------- Round 2 grading: 4 mostly correct loop to check in one direction, no attempt to handle longer string 5 mostly correct loop to check in one direction and reasonable attempt to handle longer string 6 mostly correct loop to check in one direction and correct handling of longer string in one direction ------------------------------------------------------------------------------- Round 3 grading: 7 correctly handles both directions for strings of equal length OR correctly handles one direction and correctly handles both long strings 8 correctly handles both directions for strings and reasonable attempt to deal with longer strings 9 all correct
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011