Course: Introduction to Computers & Programming (JAVA), Summer 2009
Instructor: Christopher Conway

Homework #1

Due: Tuesday, 26 May 2009 Wednesday, 27 May 2009 at 11:59 PM

Write a program that computes the difference between two time readings.

The times are read into the program as integers of at most 6 digits (without leading zeros): hhmmss, where hh stands for hour, mm minutes and ss seconds. The difference should be displayed in the same format (called "military format").

The difference can be both positive and negative. It doesn't matter in which direction it's negative, as long as it's consistent. That is, you can subtract the second time from the first time or the second time from the first.

For the purposes of this assignment you can assume that the inputs are correct (that is, you are not going to have more than 59 seconds or more than 59 minutes).


Ex (the blue text indicates user input):

    Enter first time:  230000
    Enter second time: 210001
    Time difference:    15959

    Enter first time:  123245
    Enter second time: 112955
    Time difference:    10250

    Enter first time:   23245
    Enter second time:  32815
    Time difference:    -5530

    Enter first time:     245
    Enter second time:    235
    Time difference:       10

Your assignment should be submitted BY EMAIL to our class grader: Andrew Lott, adl316 (AT) nyu (DOT) edu. Attach your .java file—and only your .java file—to an email with the subject line "Homework #1".

Notes and Hints:
  • The above example is for 4 separate runs corresponding to the following times. Note that we are subtracting the second time from the first time.
    1. 11:00:00 PM and 9:00:01 PM [difference is 1:59:59, or 1 hr 59 mins 59 secs]
    2. 12:32:45 PM and 11:29:55 AM [= 1:02:50, or 1 hr 2 mins and 50 secs]
    3. 2:32:45 AM and 3:28:15 AM [= -0:55:30, or negative 55 mins 30 secs]
    4. 0:02:45 AM and 0:02:35 AM [= 0:00:10, or in0other words 10 seconds]
  • Example algorithm for this assignment:
    1. Isolate the different parts of each time: hours, minutes and seconds (use the mod operator % ... hint, hint, hint; you could use integer division instead, which may be more intuitive for some).
    2. Compute the time in terms of seconds (1 hour = 3600 seconds, 1 minute = 60 seconds).
    3. Compute the difference in seconds.
    4. Convert the above number into hours, minutes and seconds (again the mod operator %, but with a different base ... hint, hint, hint).
    5. Create the new 6-digit integer (the time in military format) and print it.
  • You are NOT being asked to convert a PM or AM time to military time format. The user should be entering the time in military format.
  • You may draw inspiration from the following example program: ReverseDigits.java

Good luck!

Last modified: 26 May 2009