Lab 03 A Later Time Objective: Create a program which prompts the user to enter
ID: 3878601 • Letter: L
Question
Lab 03 A Later Time Objective: Create a program which prompts the user to enter a time in "Hour:Minute" format and then prompts the user to enter a value, in minutes, that wll add to that time. Once the calculation is completed output the resulting time . This is assumed to be in military time, so make sure that there is only 24 hours (0-23), 60 minutes (0-59) in any given time . Take a variety of inputs as long as it is in the format "Hour:Minute o 06:02 and 6:2 are both valid inputs . Output the original time along with the new time Example Dialog: Enter a time in the format hour minute 3:30 Enter a number of minutes to add to this time 50 The time 50 minutes after 3:30 will be 4:20 Hint: Here is an algorithm to solve this problem 1. Read the time. 2. Make a copy of the date. 3. Find the index of the ‘:". 4. Copy the hour part out of the time to a new variable. s. Parse the hour string into a number 6. Next save the minutes into a different variable . Parse the minutes s. Add the new minutes to the recently parsed minutes 9. Adjust the number of minutes to only be 0-59 and add the number of hours, if needed to the hours. Also adjust the hours to be 0-23 if needed. 10. Print the results!Explanation / Answer
import java.util.Scanner;
/*The java program that read time in hh:mm and prompt
* to enter mins to add then the find time after
* adding time*/
//Timer.java
public class Timer
{
public static void main(String[] args)
{
//declare string variable to read user input time
String usertime;
int addMins;
Scanner scan=new Scanner(System.in);
//read time
System.out.println("Enter a time in the format hour:minute");;
usertime=scan.nextLine();
System.out.println("Enter a number of minutes to add to this time");
addMins=Integer.parseInt(scan.nextLine());
//split by semi colon
String data[]=usertime.split(":");
//get hours and mins
int hours=Integer.parseInt(data[0]);
int mins=Integer.parseInt(data[1]);
//add addMins to mins
mins=mins+addMins;
//adjust time
//checking if mins is greater than 59
if(mins>59)
{
hours++;
mins=mins%60;
}
//print time in hh:mm
System.out.print("The time "+addMins+
" minutes after "+usertime+" will be ");
if(hours<9 && mins<9)
System.out.println("0"+hours+":0"+mins);
else if(mins<9)
System.out.println(hours+":0"+mins);
else if(hours<9)
System.out.println("0"+hours+":"+mins);
else
System.out.println("0"+hours+":"+mins);
}
}
------------------------------------------------------------------------------
Sample Output:
Enter a time in the format hour:minute
3:30
Enter a number of minutes to add to this time
50
The time 50 minutes after 3:30 will be 04:20
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.