1. Expression Value 8 + 5 * 3 / 2 15 1.5 * 4 * 7 / 8 + 3.4 8.65 73 % 10 - 6 % 10 + 28 % 3 -2 4 + 1 + 9 + "." + (-3 + 10) + 11 / 3 "14.73" 3 / 14 / 7 / (1.0 * 2) + 10 / 6 1.0 2. happy and pumpkin were orange! orange and happy were pumpkin! orange and sleepy were y! pumpkin and x were green! green and pumpkin were vampire! 3. Method Call Returns mystery(2, 9) 0 mystery(5, 1) 6 mystery(38, 5) 119 mystery(5, 5) 0 mystery(40, 10) 57 4. next < 0 y > z y == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | never | always | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | never | always | never | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point E | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ 5. Five solutions are shown. public static boolean enoughTimeForLunch( int h1, int m1, int h2, int m2) { if (h1 > h2) { return false; } else if (h1 == h2) { return m2 - m1 >= 45; } else if (h1 == h2 - 1) { return 60 + m2 - m1 >= 45; } else { return true; } } public static boolean enoughTimeForLunch( int h1, int m1, int h2, int m2) { if (h2 > h1 + 1) { return true; } else if (h2 == h1 && m1 + 45 <= m2) { return true; } else if (h2 == h1 + 1 && m1 - 15 <= m2) { return true; } else { return false; } } public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) { if (h1 > h2) { return false; } else if (h1 == h2) { // same hour if (m1 + 45 <= m2) { // must be >= 45 min apart return true; } else { return false; } } else if (h2 == h1 + 1) { // h1 is just before h2 if (m1 - 15 <= m2) { // must be >= -15 min apart return true; } else { return false; } } else { // time 1 is > 1 hour before time 2 return true; } } public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) { if ((h1 == h2 && m1 + 45 <= m2) || (h2 == h1 + 1 && m1 - 15 <= m2) || (h1 < h2 - 1)) { return true; } else { return false; } } public static boolean enoughTimeForLunch(int h1, int m1, int h2, int m2) { return 60 * h1 + m1 + 45 <= 60 * h2 + m2; } 6. One solution is shown. public static void threeHeads() { Random r = new Random(); int numHeads = 0; while (numHeads < 3) { int flip = r.nextInt(2); // flip coin if (flip == 0) { // heads numHeads++; System.out.print("H "); } else { numHeads = 0; System.out.print("T "); } } System.out.println(); System.out.println("Three heads in a row!"); } 7. Two solutions are shown. public static int numTWords(String s) { int count = 0; int i = 0; while (i < s.length()) { while (i < s.length() && s.charAt(i) == ' ') { // skip over whitespace i++; } if (i < s.length()) { if (s.charAt(i) == 't' || s.charAt(i) == 'T') { count++; } while (i < s.length() && s.charAt(i) != ' ') { // skip until find next whitespace i++; } } } return count; } public static int numTWords(String s) { s = s.toLowerCase(); int count = 0; if (s.charAt(0) == 't') { count++; } for (int i = 0; i < s.length() - 1; i++ ) { if (s.charAt(i) == ' ' && s.charAt(i + 1) == 't') { count++; } } return count; }