Feedback? Participation admin 3.10.4 Arrange the code to fix the 'dangling else' Full screen Fix the code from the preceding problem so that the correct greeting is produced. Be sure to use braces for all branches. How to use this tool v Unused If the hour is 6: { time = "morning"; } If time = "afternoon"; i) I. if (suffix.equals ("pm")) else Greeting.java Load default template import java.util.Scanner; public class Greeting { public static void main (String[] args) { Scanner in = new Scanner (System.in); String time = "evening"; int hour = in.nextInt(); String suffix = in.next(); System.out.println("Good " + time); } } Feedback? 3.10.4 Arrange the code to fix the dangling else Full screen Fix the code from the preceding problem so that the correct greeting is produced. Be sure to use braces for all branches. How to use this tool -v Greeting.java import java.util.Scanner; public class Greeting Load default template if (hour == 6) { time = "morning"; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String time = "evening"; int hour = in.nextInt(); String suffix = in.next(); System.out.println("Good " + time); time = "afternoon"; if (suffix.equals("pm")) { } else
Added by Donald T.
Close
Step 1
Ensure all conditional branches use braces to avoid the dangling else problem. Show more…
Show all steps
Your feedback will help us improve your experience
Patina Herring and 64 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Read in a time such as 3 pm and print the equivalent military hour (such as 15). Validate the input. If the input doesn't start with an integer, print: Error: Not an integer. If the number isn't between 1 and 12, print: Error: The hour must be between 1 and 12. If the suffix isn't "am" or "pm", print: Error: The suffix must be am or pm. import java.util.Scanner; public class TimeReader { public static void main(String[] args) { Scanner in = new Scanner(System.in); if (in.hasNextInt()) { int hour = in.nextInt(); if (hour >= 1 && hour <= 12) { String suffix = in.next(); if (suffix.equals("am") || suffix.equals("pm")) { /* Your code goes here */ // Convert hour to military time if (suffix.equals("pm")) { hour = hour + 12; } hour = hour % 24; System.out.println(hour); } else { System.out.println("Error: The suffix must be am or pm."); } } else { System.out.println("Error: The hour must be between 1 and 12."); } } else { System.out.println("Error: Not an integer."); } } }
Willis J.
a. public void setHours(int hours); public int getHours(); @Test public void testSetHours() { Time time = new Time(4, 14, 43); // replace this with your test code } b. public void setSeconds(int seconds); public int getSeconds(); @Test public void testSetSeconds() { Time time = new Time(4, 14, 43); // replace this with your test code } c. public void changeTime(int hours, int minutes, int seconds); public int getHours(); public int getMinutes(); public int getSeconds(); @Test public void testChangeTime() { Time time = new Time(4, 14, 43); // replace this with your test code } d. public String formatTime(); // output should be in format hh:mm:ss @Test public void testFormatTime() { Time time = new Time(4, 14, 43); // replace this with your test code }
Akash M.
12.1.5: Handling input exceptions: restaurant max occupancy tracker. Arrange the following lines to make a program that determines when the number of people in a restaurant equals or exceeds 10 occupants. The program continually gets the number of people entering or leaving the restaurant. Ex: 2 means two people entered, and -3 means three people left. After each input, the program outputs the number of people in the restaurant. Once the number of people in the restaurant equals or exceeds 10, the program exits. If an InputMismatchException exception occurs, the program should get and discard a single string from input. Ex: The input '2 abc 8' should result in 10 occupants. Not all lines are used in the solution. import java.util.Scanner; import java.util.InputMismatchException; public class MaxOccupancyTracker { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int maxNumPeople = 10; int totalNumPeople = 0; while (totalNumPeople < maxNumPeople) { try { totalNumPeople += scnr.nextInt(); System.out.println("Occupancy: " + totalNumPeople); } catch (InputMismatchException e) { scnr.next(); } } System.out.println("We're full!"); } }
Supreeta N.
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD