In: Computer Science
Create an ElapsedTimeCalculator application that prompts the user for a starting hour, whether it is am or pm, and the number of elapsed hours. The application then displays the time after that many hours have passed. (Java)
/*********************************ElapsedTimeCalculator.java*********************************/
import java.util.Scanner;
public class ElapsedTimeCalculator {
public static void main(String[] args) {
Scanner scan = new
Scanner(System.in);
System.out.println("Enter the
starting hour: ");
int startHour =
scan.nextInt();
System.out.println("AM or
PM?");
String amOrPm = scan.next();
System.out.println("How many hour
elapsed: ");
int elapsedHour =
scan.nextInt();
if
(elapsedHour>12&&elapsedHour<24&&amOrPm.equalsIgnoreCase("AM"))
{
amOrPm =
"PM";
elapsedHour =
elapsedHour%12;
}
else if
(elapsedHour>12&&elapsedHour<24&&amOrPm.equalsIgnoreCase("PM"))
{
amOrPm =
"AM";
elapsedHour =
elapsedHour%12;
}
else if (elapsedHour>24) {
elapsedHour =
elapsedHour%12;
}
//condition check for valid
time
if (startHour / 12 <= 1
&& amOrPm.equalsIgnoreCase("AM")) {
//condition if
total time is greater than 12
if ((startHour +
elapsedHour) / 12 < 1 && startHour != 12) {
System.out.println("Time is: " + (startHour +
elapsedHour) + "AM");
} else if
(startHour == 12 && (startHour + elapsedHour) / 12 < 2)
{
System.out.println("Time is: " + (elapsedHour) +
"AM");
} else {
System.out.println("Time is: " + Math.abs((12 -
(elapsedHour + startHour))) + "PM");
}
} else if (startHour / 12 <= 1
&& amOrPm.equalsIgnoreCase("PM")) {
if ((startHour + elapsedHour) / 12 < 1 && startHour != 12) {
System.out.println("Time is: " + (startHour +
elapsedHour) + "PM");
} else if
(startHour == 12 && (startHour + elapsedHour) / 12 < 2)
{
System.out.println("Time is: " + (elapsedHour) +
"PM");
} else {
System.out.println("Time is: " + Math.abs((12 -
(startHour + elapsedHour))) + "AM");
}
}
else {
System.out.println("Please enter valid time");
}
}
}
/****************************ouput**************************/
Enter the starting hour:
12
AM or PM?
PM
How many hour elapsed:
10
Time is: 10PM
Enter the starting hour:
1
AM or PM?
AM
How many hour elapsed:
26
Time is: 3AM
Thanks a lot, Please let me know if you have any problem,,