Problem #1 Expression Value ----------------------------------------------- 2.5 * 2 * 5 / 10 + 1.5 4.0 85 % 10 + 4 % 10 - 17 % 3 7 (2 + 2 < 3 || 2 * 5 / 3 == 3) == false false 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: foo wants buzz to sue. bill wants sue to buzz. bill wants sue to hope. hope wants bill to sue. 1 point for each of the 12 values -1 if any error in surrounding text (no dittos, quotes, labels) -4 if two columns transposed -8 if you need to transpose twice ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(5, 2); 12 3 ifElseMystery(8, 3); 5 24 ifElseMystery(6, 2); 3 3 ifElseMystery(2, 8); 11 4 ifElseMystery(6, 7); 5 23 ifElseMystery(4, 7); 3 3 each number is worth a point -2 overall if columns transposed -1 if formatting error (e.g., commas between numbers, quotes) -0 if underscore for space ------------------------------------------------------------------------------- Problem #4: Method Call Output Produced --------------------------------------- mystery(8); 1 8 mystery(32); 2 5 mystery(184); 3 13 mystery(8239); 4 22 column 1 is 1 point, column 2 is 2 points -2 overall if columns transposed -1 if formatting error (e.g., commas between numbers, quotes) -1 if individual pair is reversed -0 if underscore for space Problem #5: next == 0 prev == 0 next == prev +---------------------+---------------------+---------------------+ Point A | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | never | never | always | +---------------------+---------------------+---------------------+ Point D | sometimes | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solutions: public static double tallyVotes(Scanner input) { System.out.print("vote? "); String vote = input.next(); double result = 0; int total = 0; while (!vote.equals("q")) { if (vote.equals("y")) { result++; } total++; System.out.print("vote? "); vote = input.next(); } result = result / total * 100; System.out.println("total votes = " + total); System.out.println("result = " + result + "%"); return result; } public static double tallyVotes(Scanner input) { String vote = ""; int yes = 0; int no = 0; while (!vote.equals("q")) { System.out.print("vote? "); vote = input.next(); if (vote.equals("y")) { yes++; } else if (vote.equals("n")) { no++; } } int total = yes + no; double result = 100.0 * yes / total; System.out.println("total votes = " + total); System.out.println("result = " + result + "%"); return result; } 1 correct header 5 loop to read one token at a time until the user types "q" 1 attempt to test whether user response is not "q" to control a while loop 1 correctly tests whether user response is not "q" to control a while loop 1 correctly handles initial read (either prompt/read before loop or set their variable to a value that gets them into the loop) 2 reads and stores exactly one more response each time through their loop 3 counting yes/no votes 1 reasonable attempt to count yes versus no votes (doesn't require a loop, can get this even if they don't read inside their loop, but requires an if/else testing a string for yes versus no) 2 correctly counts yes/no votes: requires two variables initialized to zero, a correct if/else structure to distinguish between them (unguarded else is okay if their loop tests guarantees it is not "q"), and pieces fitting together to process each vote exactly once; this is most often atomic, but ask question leader for permission to give 1 point if they are close 2 computing percent yes votes (requires two counters, assume counters have correct values) 1 reasonable attempt to compute percent yes votes 1 correctly computes percent yes votes 3 printing 1 reasonable attempt for two of the three required printing tasks: prompt, total votes, result 1 reasonable attempt for all three required printing tasks 1 all 3 elements present with correct format: text is all correct, print/println is correct, prompt is inside a loop, printlns are after a loop, no extra printing 1 returns a value that they believe is percent yes votes Notes: -0 for confusion of char/String with single quotes versus double quotes if they use == instead of .equals, they lose 1 point once assuming they got some credit for the code where that occurred -1 if they store console.next() in a char variable instead of a String variable (assuming they get some points for it somewhere), then allow them to use == instead of .equals Problem #7 Solutions: public static void simulateTeenagers(int n, Random r) { int age = r.nextInt(7) + 13; int sum = age; int min = age; System.out.print("ages: " + age); for (int i = 1; i <= n - 1; i++) { System.out.print(", " + age); age = r.nextInt(7) + 13; sum = sum + age; if (age < min) { min = age; } } System.out.println(); double average = (double) sum / n; System.out.println("average = " + average); System.out.println("minimum = " + min); } public static void simulateTeenagers(int n, Random r) { int age = r.nextInt(7) + 13; double sum = age; int min = age; S.o.p("ages: "); for (int i = 1; i < n; i++) { S.o.p(age + ", "); age = r.nextInt(7) + 13; sum += age; min = Math.min(age, min); } S.o.pln(age); S.o.pln("average = " + sum / n); S.o.pln("minimum = " + min); } 1 header 2 correct use of Random object 1 reasonable attempt to use a Random object to generate random ages 1 correct: uses Random object passed as a parameter or one they correctly constructed if they didn't include it as a parameter, correct expression to generate range of teenage ages 2 loop that iterates n or n-1 times, generating random ages 1 reasonable attempt 1 correct: requires use of Random to generate a different random age on each iteration (expression doesn't have to be correct) 3 compute average 1 reasonable attempt to find the sum of the ages 1 reasonable attempt to compute the average from a sum 1 correct: correctly computes the sum of the ages they generate (requires generating a different random age on each iteration, no double jeopardy with loop bounds, but initialization and order within the loop matters so that the pieces fit together), divide sum by n, uses double division 2 compute min 1 reasonable attempt to compute min (generally requires an if to reset min or a call on Math.min) 1 correct: initialize either to first generated age or to a value >= 19 (make sure it works even when n is 1 if they don't use a generated age), correctly resets min inside a loop, pieces fit together to consider every age they generate including the first and last one, requires generating a different random age on each iteration 2 solved fencepost problem 1 attempt (print first age before a loop or the last age after a loop or test for when to print comma) 1 correct: prints exactly n numbers separated by commas 3 printing the required elements (ignore fencepost issue) 1 print or println for 3 of the 4 elements: * "ages:" (with or without an initial age) * age (different print/println from previous, ignore comma/space except for the format point below) * "average = " + average * "minimum = " + minimum 1 print or println for all 4 elements 1 all correct: all 4 elements present and pieces fit together so that "ages:" is printed once before a loop, generated age is printed inside a loop, every age they generate is printed exactly once, average and minimum are printed once after a loop, text is all correct, print/println is correct, commas and spacing are correct Problem #8 Solutions: // get one digit, remove it, compare it to the new value of n % 10 public static int digitsInARow(int n) { int max = 1; int count = 1; while (n > 0) { int next = n % 10; n = n / 10; if (n % 10 == next) { count++; } else { count = 1; } if (count > max) { max = count; } } return max; } // keep track of a prev digit and compare it to the next digit public static int digitsInARow(int n) { int max = 1; int count = 0; int prev = -1; while (n != 0) { int next = n % 10; n = n / 10; if (next == prev) { count++; } else { count = 1; } max = Math.max(max, count); prev = next; } return max; } // count how many times the last 2 digits are divisible by 11 public static int digitsInARow(int n) { int max = 1; int count = 1; while (n > 0) { if (n % 100 % 11 == 0) { count++; } else { count = 1; } max = Math.max(max, count); n = n / 10; } return max; } Can get up to 3 points for any of the following good things: 1 correct header 1 while loop testing against n being 0 or a 1-digit number 1 reasonable attempt to get the final digit of a number with % 10 1 reasonable attempt to remove the final digit of a number with n / 10 1 reasonable attempt to compare what they think are 2 digits of the number 1 initializing a counter to 0 or 1 1 incrementing a counter inside a loop with a test 1 intializing a max to 0 or 1 1 reasonable attempt to update a max when compared to a count 1 reasonable attempt to return a max ------------------------------------------------------------------------------- To go to round 2, you need to all of these: * local variable to store a current count of digits * local variable to store a max and you need to see one of these: * storing a prev digit to compare against (a third variable) * checking to see if (ones-digit == tens-digit) * checking to see if last 2 digits are divisible by 11 -3 major error -2 medium error && vs || need to delete nontrivial code of theirs printing result instead of returning (only -1 if they also return) doesn't handle sequences at front or end of a number -1 minor error obob (you can fix code by changing one expression by +1 or -1) bad initialization forgot to return -0 minor syntax errors
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011