1. Expression Value -------------------------------------------------------- 4 * (2 + 4) - 3 * 5.5 9.0 6 % 4 + 4 % 6 + 6 % 6 6 15 / 4 / 3.0 - 18 / 5 + (15 / 10.0) -0.5 3 + 3 + "3 * 3" + (3 + 3) * 3 % 3 "63 * 30" (11 != 7 + 4 || 3 * 2 <= 39 % 10) == false false 2. Parameter Mystery. The program produces the following output. Colonel Mustard in the ballroom with the candlestick conservatory in the study with the wrench rope in the Colonel Mustard with the Mr. study Professor Plum in the ballroom with the Mrs. Peacock 3. Method Call Output Produced --------------------------------------- ifElseMystery(5, 20); 5 20 ifElseMystery(42, 42); 43 41 ifElseMystery(6, 1); 9 7 ifElseMystery(5, 9); 7 5 ifElseMystery(-3, 0); -1 -3 4. Method Call Output Produced --------------------------------------- whileMystery(7, 5); 2, -2, 3 whileMystery(20, 4); 16, 13, 11, 10, 0 whileMystery(40, 10); 30, 21, 13, 6, 0, 5 5. x % 2 == 1 y > x y % 2 == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point B | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point C | never | always | always | +---------------------+---------------------+---------------------+ Point D | never | always | always | +---------------------+---------------------+---------------------+ Point E | sometimes | never | sometimes | +---------------------+---------------------+---------------------+ 6. Programming. One possible solution appears below: public static void slotMachine(Scanner console, Random rand, int amount) { String ans = "yes"; System.out.println("Amount left: $" + amount); while (amount > 0 && ans.equals("yes")) { System.out.print("Your bet? "); int bet = console.nextInt(); int num1 = rand.nextInt(10) + 1; int num2 = rand.nextInt(10) + 1; System.out.println("spin: " + num1 + " " + num2); int win = 0; if (num1 == num2) { win += 5 * bet; } else if (num1 == 7 || num2 == 7) { win += bet; } if (win > 0) { amount += win; System.out.println("You won $" + win + "!"); } else { amount -= bet; } System.out.println("Amount left: $" + amount); if (amount > 0) { System.out.print("spin again? "); ans = console.next(); System.out.println(); } } } 7. Programming. One possible solution appears below: public static double stockMarket(Scanner console) { double delta = 0; int stock = 1; int day = 1; System.out.print("How many stocks? "); int stocks = console.nextInt(); System.out.print("How many days? "); int days = console.nextInt(); for (int i = 1; i <= stocks; i++) { System.out.print("Stock " + i + ": "); double price1 = console.nextDouble(); for (int j = 2; j <= days; j++) { double price2 = console.nextDouble(); if (delta < price2 - price1) { delta = price2 - price1; stock = i; day = j; } price1 = price2; } } System.out.println("Stock " + stock + " had greatest price increase of " + delta + " on day " + day); return delta; } 8. Three possible solutions appear below. // Use a boolean flag to keep track of beginning of words public static String hashTag(String s) { String result = ""; boolean firstLetter = true; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == ' ') { firstLetter = true; } else { if (firstLetter) { result += Character.toUpperCase(s.charAt(i)); firstLetter = false; } else { result += Character.toLowerCase(s.charAt(i)); } } } return "#" + result; } // Compare adjacent characters to find boundaries of words public static String hashTag(String s) { String result = ""; if (s.length() > 0) { if (s.charAt(0) != ' ') { result += Character.toUpperCase(s.charAt(0)); } } for (int i = 1; i < s.length(); i++) { if (s.charAt(i - 1) == ' ' && s.charAt(i) != ' ') { result += Character.toUpperCase(s.charAt(i)); } else if (s.charAt(i) != ' ') { result += Character.toLowerCase(s.charAt(i)); } } return "#" + result; } // repeatedly find words using indexOf(" ") and trimming public static String hashTag(String s) { String result = ""; int space = s.indexOf(" "); while (s.length() > 0) { while (space == 0) { // to trim spaces s = s.substring(1); space = s.indexOf(" "); } if (s.length() > 0) { String word = s; if (space != -1) { word = s.substring(0, space); } char firstLetter = Character.toUpperCase(word.charAt(0)); result += firstLetter + word.toLowerCase().substring(1); s = s.substring(word.length()); space = s.indexOf(" "); } } return "#" + result; }