Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that asks for two times, both expressed in military time format,

ID: 3855058 • Letter: W

Question

Write a program that asks for two times, both expressed in military time format, and calculates the difference in hours and minutes between them. As an example 2:03am is written 02:03 in military time format while 2:03pm is written as 14:03 in military time format. Your program should allow (and expect) the user to enter the two times in this format. For calculation, the case where the first time is an earlier time than the second is probably the easiest to understand and implement. Thus, the difference between 08:12 and 09:06 should be of 0 hours and 54 minutes. When the two times are equal (the same) the difference should be 0 (zero hours and zero minutes). For the case where the second time is an earlier time than the first please consider the second ime to be next day. As an example if the first time is 23:59 and the second time is 09:01, then the time difference calculated and reported by your program should be 0 hours and 2 minutes. Thus, no matter what the two times entered by the user, your program will always produce an answer that is always greater (or equal) to 0 (zero) and less than (or equal to) 23 hours and 59 minutes (inclusive). How do you solve such a program? First of, you put on your navigator hat and imagine the problem solved: Inberactions Censole Compiler Output We Lcome to DJava. Woking direct Eates the second t09 08 0 hours and 54 minutes. the fazatt086 Enter the "cond time: 1959 1959 11 hours and 3 minutes un Howewor kone the tärst t 195 Eates the second t0858 E bouEs and 57 minutes the firstti 23 5 Enter the second tue0001 0 hours and inutes Enter the f Enter the second ti 23s9 bours and 53 minutes

Explanation / Answer

Given below is the code needed and output. In case of any issues, please post a comment. If happy with the answer, please rate it. Thank you.

import java.util.Scanner;

public class TimeDifference {

   //converts the input string time into minutes

   public static int convertTime(String time)

   {

       int idx = time.indexOf(':');

       if(idx != -1)

       {

           //extract the hours and minuts portions

           String hstr = time.substring(0, idx);

           String mstr = time.substring(idx+1);

          

           //convert to int

           int hours = Integer.parseInt(hstr);

           int mins = Integer.parseInt(mstr);

           mins += 60 * hours;

           return mins;

       }

       else

           return 0;

   }

   public static void main(String[] args) {

       Scanner keybd = new Scanner(System.in);

       String t1 , t2;

       System.out.print("Enter first time t1: ");

       t1 = keybd.next();

       System.out.print("Enter second time t2: ");

       t2 = keybd.next();

      

       int min1 = convertTime(t1);

       int min2 = convertTime(t2);

      

       int diff = min2 - min1 + 24 * 60;

      

       diff = diff % (24 * 60);

      

       //convert difference into hours and mins

       int h = diff / 60;

       int m = diff % 60;

      

       System.out.printf("The difference is %02d:%02d",h, m);

   }

}

output

(normal case... when t2 is greater than t1)

Enter first time t1: 08:12

Enter second time t2: 09:16

The difference is 01:04

=============

(case when t2 is less than t1)

Enter first time t1: 09:16

Enter second time t2: 08:12

The difference is 22:56

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote