Solutions for Winter 2013 Midterm: ---------------------------------- Q1: 2 points for each question, 1 for the type, 1 for the value, no half points! 0 point for string values if the "" are missing A) double, 542.0 B) int, 0 C) boolean, false D) boolean, true E) String, "754.2" F) String, "61.2" G) int (or long or char), 4 Q2: 4 points for each question, no partial points a) A L shape b) A square with a checkerboard pattern c) A K shape Q3: 4 points for each question a) import java.util.Scanner; -1 if capitalization is wrong -2 if java.util is missing b) double percentage = calculatePercentage(earned, total); or int percentage = (int) calculatePercentage(earned, total); c) double result = (double) quantity / outOf * 100; or double result = quantity / (double) outOf * 100; or public static double calculatePercentage(double quantity, double outOf) or public static double calculatePercentage(double quantity, int outOf) or public static double calculatePercentage(int quantity, double outOf) Q4: 2 points per question, 1 point if something is wrong (e.g. parameter missing, type wrong) and at least something is right (e.g. the right return type, or one correct parameter type) A) int countVowels(String s) B) boolean compareLetterSet(String s1, String s2) C) void displayInBinary(int n) D) int nthPrime(int n) E) String substring(String original, int from, int to) F) boolean isPrime(int n) G) void displayOccurrencesOf(String s, char letter) Q5: a) 2 points: a > 0 ^ b > 0 or (a > 0 || b > 0) && !(a > 0 && b > 0) 1 point: a > 0 || b > 0 b) 2 points + 2 bonus: ((c1 > 'Z' && c2 > 'Z') || (c1 < 'a' && c2 < 'a') && c1 < c2) || (c1 > 'Z' && c2 < 'a' && (c1 - 'a' + 'A' < c2) || (c1 < 'a' && c2 > 'Z' && (c1 - 'A' + 'a' < c2) 2 points + 1 bonus: ("" + c1).compareToIgnoreCase("" + c2) < 0 2 points: c1 < c2 c) 2 points: !a || b (or anything equivalent, such as !(a && !b) Q6: 16 points, -1 point if layout is not correct -1 point if header has correct parts, but not entirely correct (2) public static void displayMoney(int cents) { int remainder = cents; // Compute the number of each type of coin required to have the value. (2) int toonies = remainder / 200; remainder = remainder % 200; (2) int loonies = remainder / 100; remainder = remainder % 100; (2) int quarters = remainder / 25; remainder = remainder % 25; (2) int dimes = remainder / 10; remainder = remainder % 10; (2) int nickels = remainder / 5; (2) int pennies = remainder % 5; (2) System.out.println(toonies + " two-dollar coins,"); System.out.println(loonies + " dollar coins,"); System.out.println(quarters + " quarters,"); System.out.println(dimes + " dimes,"); System.out.println(nickels + " nickels,"); System.out.println(pennies + " cents."); } Q7: 24 points if upper case / lower case not handled correctly, 20 points (1) import java.util.Scanner; (1) public class SentencePlay { (1) public static String tongueTwist(String in, char replacement) { (1) String result = ""; (2) for (int i=0; i