GROUNDWORK Chapter 4 - Making Decisions Name: ________________________________ Date: ______________ Fill in every blank. Where a term is asked for, write the word the course uses. Where code is asked for, write exact Java. Where output is asked for, write what the program prints, one line per line. VOCABULARY ---------- 1. A __________________ is an expression, written in parentheses after the if keyword, that produces true or false. 2. The statements between a { and its }, which run only when the condition is true, are called a __________________. 3. __________________ (two words) lets you chain more than two possibilities together in one decision. 4. In Java a condition must produce a __________________ value, true or false, which is why if (name) is a compile error when name is a String. READING CODE ------------ 5. What does this print? int score = 85; String grade; if (score >= 90) { grade = "A"; } else if (score >= 80) { grade = "B"; } else if (score >= 70) { grade = "C"; } else if (score >= 60) { grade = "D"; } else { grade = "F"; } IO.println("Score " + score + " earns a " + grade + "."); ____________________________________ 6. What does this print? int score = 95; String grade; if (score >= 60) { grade = "D"; } else if (score >= 70) { grade = "C"; } else if (score >= 90) { grade = "A"; } else { grade = "F"; } IO.println(grade); ____________________________________ 7. What does this print? String name = ""; if (!name.isEmpty()) { IO.println("Hello, " + name + "."); } else { IO.println("Hello, stranger."); } ____________________________________ 8. What does this print? boolean hasTicket = false; int age = 20; if (hasTicket) { if (age >= 13) { IO.println("Enjoy the show."); } else { IO.println("You need an adult with you."); } } else { IO.println("Please buy a ticket first."); } ____________________________________ 9. What does this print? int score = 45; if (score >= 60) IO.println("You passed."); IO.println("Congratulations."); ____________________________________ WRITING CODE ------------ 10. Complete this line so the age the user types is read as a whole number: int age = __________________(IO.readln("Your age: ").strip()); 11. Complete the catch so it handles the exception Integer.parseInt throws when it cannot read the text: try { int tickets = Integer.parseInt(answer.strip()); IO.println("That will be $" + (tickets * 12) + "."); } catch (__________________ e) { IO.println("Please enter a whole number."); } 12. Complete this program so a price that is not a number is caught: __________________ { double price = Double.parseDouble(answer.strip()); IO.println("With tax: $" + String.format("%.2f", price * 1.08)); } catch (NumberFormatException e) { IO.println("That is not a number."); } 13. Complete this condition so the Tuesday discount applies only when the price is greater than 0, never to a free ticket: int discount = 0; if (day.equals("tuesday") && price __________________ 0) { discount = 3; price -= discount; } WHY IT WORKS ------------ 14. Java checks else if conditions in the order they are written and stops at the first one that is __________________, which is why a score of 95 in the reordered chain gets the grade "__________________" instead of "A". 15. The ticket program puts Integer.parseInt inside a try because, given text such as three, it throws a NumberFormatException that would otherwise __________________. 16. name.isEmpty() gives back __________________ for the empty string "", which is why !name.isEmpty() is false and the program prints "Hello, stranger." when the user presses Enter without typing anything.