In: Computer Science
write this program in java... don't forget to put comments.
You are writing code for a calendar app, and need to determine the end time of a meeting. Write a method that takes a String for a time in H:MM or HH:MM format (the program must accept times that look like 9:21, 10:52, and 09:35) and prints the time 25 minutes later.
For example, if the user enters 9:21 the method should
output
9:46
and if the user enters 10:52 the method should output
11:17
and if the user enters 12:45 the method should output
1:10
import java.util.*;
public class Main
{
//method to calcuate the meeting end time
public static void meetingTime(String startTime)
{
//variable declaration and initialization
int hh=0, mm=0;
//check the hours is single digit or two digit
if(startTime.charAt(1) ==
':')
{
hh =
Integer.parseInt(startTime.substring(0,1));
mm =
Integer.parseInt(startTime.substring(2,4));
}
else
{
hh =
Integer.parseInt(startTime.substring(0,2));
mm =
Integer.parseInt(startTime.substring(3,5));
}
//calculate minutes
mm = mm + 25;
if(mm>59)
{
mm = mm % 60;
hh++;
}
//check if hh is greater than
12
if(hh>12)
hh = hh % 12;
//display the end time
System.out.println("The end time of
meeting is: "+hh+":"+mm);
}
public static void main(String[] args)
{
//Scanner
Scanner input = new Scanner(System.in);
//get user input
System.out.print("Enter the meeting
start time: ");
String startTime =
input.nextLine();
//method calling
meetingTime(startTime);
}
}
OUTPUT:
RUN 1:
Enter the meeting start time: 9:21
The end time of meeting is: 9:46
RUN 2:
Enter the meeting start time: 10:52
The end time of meeting is: 11:17
RUN 3:
Enter the meeting start time: 12:45
The end time of meeting is: 1:10