1. Expression Value -------------------------------------------------------- 2 * (1 + 5) - 3 * 3.2 2.4 25 % 5 + 13 % 3 - 19 % 4 % 7 -2 16 / 3 + 2 * 6.0 + 1 / 5 17.0 1 + 2 + "." + (2 + 3) + 5 * 6 + 7 "3.5307" (5 * 3 != 14 && 6 + 1 > 5 % 3) == false false 2. Parameter Mystery. The program produces the following output. 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 3. Method Call Output Produced --------------------------------------- ifElseMystery(3, 8); 7 11 ifElseMystery(7, 10); 11 10 ifElseMystery(8, 7); 10 7 ifElseMystery(12, 9); 14 10 ifElseMystery(6, 6); 9 6 ifElseMystery(1, 6); 5 9 4. Method Call Output Produced --------------------------------------- whileMystery(6, 0); 0 whileMystery(8, 1); 8, 8 whileMystery(3, 3); 3, 9, 17, 17 whileMystery(4, 5); 4, 13, 26, 42, 60, 60 5. x > y z == 0 x == y +---------------------+---------------------+---------------------+ Point A | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point B | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point C | always | never | never | +---------------------+---------------------+---------------------+ Point D | never | never | never | +---------------------+---------------------+---------------------+ Point E | never | sometimes | always | +---------------------+---------------------+---------------------+ 6. Programming. Two possible solutions appear below: 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; } 7. Programming. One possible solution appears below: public static void simulateTeenagers(int n, Random r) { int age = r.nextInt(7) + 13; int count = 0; int sum = age; if (age >= 18) { count++; } System.out.print("ages: " + age); for (int i = 1; i <= n - 1; i++) { age = r.nextInt(7) + 13; sum += age; System.out.print(", " + age); if (age >= 18) { count++; } } System.out.println(); System.out.println(“average age: “ + (double) sum / n); System.out.println(count + " teenagers of age”); } 8. Two possible solutions appear below. public static boolean contains(String s1, String s2) { for (int i = 0; i <= s1.length() - s2.length(); i++) { boolean matches = true; for (int j = 0; j < s2.length(); j++) { if (s1.charAt(i + j) != s2.charAt(j)) { matches = false; } } if (matches) { return true; } } return false; } public static boolean contains(String s1, String s2) { for (int i = 0; i <= s1.length() - s2.length(); i++) { String substring = ""; for (int j = 0; j < s2.length(); j++) { substring += s1.charAt(i + j); } if (substring.equals(s2)) { return true; } } return false; }