Key to CSE142 Midterm, Summer 2012 1. Expression Value ----------------------------------------------- 3 + 5 / 2 - 10 % 6 1 4 + 1 + "foo" + 4 + 1 "5foo41" (1 / 2 / 3.0 + (10 - 3)) / 2 3.5 "foo" + 2.4 / (11 % 3) + "bar" "foo1.2bar" 14 % 8 % 4 - 2.0 * 3 - 3 -7.0 2. Parameter Mystery. The program produces the following output. 1 + 3 = 5 5 + 10 = 3 7 + 7 = 4 2 + 2 = 5 0 + 3 = 4 3. Method Call Output Produced --------------------------------------- ifElseMystery(0, 0); 0 0 ifElseMystery(10, 4); 6 4 ifElseMystery(3, 6); 4 9 ifElseMystery(13, 8); 5 13 4. Method Call Output Produced -------------------------------------- whileMystery(1, 2); 1 1 0 whileMystery(2, 5); 3 4 3 whileMystery(3, 10); 4 6 9 7 5. m == 1 m % 2 == 1 o <= e +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | never | never | sometimes | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point E | always | always | always | +---------------------+---------------------+---------------------+ 6. One possible solution appears below: public static void diagnose(Scanner console) { System.out.print("Temperature? "); double temparature = console.nextDouble(); System.out.print("Headache? "); String headache = console.next().toLowerCase(); System.out.print("Heart rate? "); int heartRate = console.nextInt(); if (heartRate <= 20) { System.out.println("Heart Problem"); } else if (headache.startsWith("y")) { if (temparature >= 100.4) { System.out.println("Fever"); } else { System.out.println("Cold"); } } else { System.out.println("Just Fine"); } } 7. Four possible solutions appear below: public static int exp(int base, int target) { if (base < 2) { return -1; } else if (target < 1) { return -1; } int exp = 0; int cur = 1; while (cur < target) { exp++; cur = cur * base; } return exp; } public static int exp(int base, int target) { if (base < 2) { return -1; } if (target < 1) { return -1; } int exp = 0; int result = (int)Math.pow(base, exp); while (result < target) { exp++; result = (int)Math.pow(base, exp); } return exp; } public static int exp(int base, int target) { if (base < 2 || target < 1) { return -1; } int exp = 0; while (target > 1) { exp++; // I don't expect students to come up with this. if (target % base != 0) { target += base - target % base; } target = target / base; } return exp; } public static int exp(int base, int target) { if (base < 2 || target < 1) return -1; int result = (int)Math.ceil(Math.log10(target) / Math.log10(base)); if (Math.pow(base, result - 1) >= target) result--; return result; } 8. Three possible solutions appear below: public static int diffChars1(String a, String b) { if (b.length() < a.length()) { String temp = a; a = b; b = temp; } a = a.toLowerCase(); b = b.toLowerCase(); int diff = 0; for (int i = 0; i < a.length(); i++) { if (a.charAt(i) != b.charAt(i)) { diff++; } } return diff + b.length() - a.length(); } public static int diffChars2(String a, String b) { int diff = 0; for (int i = 0; i < Math.min(a.length(), b.length()); i++) { if (!a.substring(i, i + 1).equalsIgnoreCase(b.substring(i, i + 1))) { diff++; } } return diff + Math.abs(a.length() - b.length()); } public static int diffChars3(String a, String b) { int diff = 0; while (a.length() > 0 && b.length() > 0) { if (a.toLowerCase().charAt(0) != b.toLowerCase().charAt(0)) { diff++; } a = a.substring(1); b = b.substring(1); } return diff + a.length() + b.length(); } public static int diffChars4(String a, String b) { int diff = Math.max(a.length(), b.length()); for (int i = 0; i < Math.min(a.length(), b.length()); i++) { if (("" + a.charAt(i)).equalsIgnoreCase("" + b.charAt(i))) { diff--; } } return diff; }