Write a a program that requets the current time and a waiting time as two intege
ID: 643554 • Letter: W
Question
Write a a program that requets the current time and a waiting time as two integers for the number of hours and the number of minutes to wait. The program then outputs what the time will be after the waiting period. Use 24-hour notation for the times. Include a loop that lets the user repeat this calculation for additional input values until the user says she or he wants to end the program.
Notes: The 24 hour boundary, i.e., when the time wraps to the next day is important here. So you need to calculate the right completion time when it's past midnight.
Explanation / Answer
import java.util.Scanner;
public abstract class time{
public static void main(String args[]){
String input;
int cth, ctm, wth, wtm, comh, comm;
Scanner in = new Scanner(System.in);
System.out.println("Compute completion time from current time and waiting period");
while(true){
System.out.println("Current time:");
System.out.println("Enter 24 hour time in the format HH:MM");
input = in.next();
if(input.charAt(1) == ':') input = "0" + input;
cth = Integer.parseInt(input.substring(0, 2));
ctm = Integer.parseInt(input.substring(3));
System.out.println("Waiting time:");
System.out.println("Enter 24 hour time in the format HH:MM");
input = in.next();
if(input.charAt(1) == ':') input = "0" + input;
wth = Integer.parseInt(input.substring(0, 2));
wtm = Integer.parseInt(input.substring(3));
comm = ctm + wtm;
comh = cth + wth;
if(comm >= 60){
comm -= 60;
comh += 1;
}
if(comh >= 24){
System.out.println("Completion time is in the day following the start time");
comh -= 24;
}
System.out.println("Completion Time in 24 hour format:");
System.out.println(comh + ":" + comm);
String more;
System.out.println(" Enter Y or y to continue, any other halts ");
more = in.next();
if(more.equalsIgnoreCase("y")) continue;
else break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.