HOMEWORK 3 - SAMPLE ANSWERS 1. (a) true (b) false (c) false (d) true 2. (a) | V / \ T / \ F ___/ x<0 \___ | \ / | | \ / | | \ / | V ------- / \ / "A" /-> T / \ F ------- ___/x-y<0\___ | | \ / | | | \ / | | | \ / | | V V | ------- ------- | / "B" / / "C" / | ------- ------- | |_____________| | |_____________| | V (b) | | B | A | ------------------- /| B / | A / | / C | 3. (a) if ((age <= 25) || (veteran == true)) (b) if (!((age > 25) && (veteran == false))) (c) if ((age <= 25) || veteran) 4. average >= 90 average >= 80 average < 60 average >= 70 5. (a) The else matches with the 2nd if. if (x > 0) if (y > 0) z = 5; else z = 3; (b) if (x > 0) { if (y > 0) z = 5; } else z = 3; 6. import java.util.*; public class MusicSuggestor { public static void main (String[] args) { Scanner scan = new Scanner(System.in); // Prompt and read in user input. // Convert userInput to uppercase. System.out.println("Please enter a musical genre: "); String userInput = scan.nextLine().toUpperCase(); // Must use .equals on String instead of ==. if (userInput.equals("POP")) System.out.println("Kelly Clarkson"); else if (userInput.equals("RAP")) System.out.println("Kanye West"); else if (userInput.equals("COUNTRY"); System.out.println("Kenny Chesney"); else System.out.println(""No suggestion available."); } } 7. (a) public static void randomMove() { int randNum = (int)(Math.random() * 100) + 1; if (randNum % 2 == 0) { Robot.turnLeft(); Robot.turnLeft(); Robot.turnLeft(); } else { Robot.turnLeft(); Robot.turnLeft(); Robot.move(); Robot.turnLeft(); Robot.turnLeft(); } } (b) public static void fancyMove() { if (!Robot.onDark()) { if (Robot.frontIsClear()) { Robot.move(); } else { Robot.turnLeft(); } } else { Robot.turnLeft(); Robot.turnLeft(); } }