Question: 1 6 . 8 . 1 : PRACTICE: Branches * * : 2 4 - hour time 2 4 - hour time ( also known

16.8.1: PRACTICE: Branches**: 24-hour time
24-hour time (also known in the U.S. as military time) is widely used around the world. Time is expressed as hours since midnight. The day starts at 00:00, and ends at 23:59. Write a program that converts am/pm time to 24-hour time. The input is two numbers and a string. If the input is 230 pm, the output should be 14:30. If the input is 1201 am, the output should be 00:01.
Hints:
Think of how each hour should be handled. 1200 am -1259 am becomes what? 800 am becomes what? 1200 pm?100 pm? Group the hours into cases that should be handled similarly (e.g.,100 am to 1100 am are handled the same).
Declare variables for hoursAmPm, minAmPm, and hours24. Note that minutes for 24-hour time remain the same as for am/pm, so no extra variable is needed.
Use an if-else statement to detect each case, and set the hours24 appropriately.
When outputting hour24, check if the hour is 0-9(just check for <10). If so, output a "0". So 7 will be output as 07. Do the same when outputting the minutes.
import java.util.Scanner;
public class main {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
/* Type your code here. */
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!