ISC Practicals and Theory - Time Addition
Updated 21 Oct 2026
This program adds two times, each given as hours and minutes, and prints the sum as hours and minutes with the minutes below 60. For example 2 h 45 m + 3 h 30 m = 6 h 15 m. This is the typical single-class version: a default constructor, an accept() method that reads one time, a method add(Time t1, Time t2) that stores the sum of two objects in the current object, and display().
Definition
A time is stored as two data members: hours and minutes. To add two times:
- add the minutes of the two times;
- every 60 minutes make 1 hour, so the carry is
total / 60and the minutes left aretotal % 60; - the hours of the sum are the two hours plus the carry.
Example: 2 h 45 m + 3 h 30 m. Minutes: 45 + 30 = 75, which is 1 hour and 15 minutes. Hours: 2 + 3 + 1 = 6. The sum is 6 h 15 m.
Second example: 1 h 30 m + 1 h 30 m. Minutes: 30 + 30 = 60, which is 1 hour and 0 minutes. Hours: 1 + 1 + 1 = 3. The sum is 3 h 0 m.
Input is valid when hours is 0 or more and minutes is from 0 to 59. The hours are not wrapped at 24: 23 h 59 m + 0 h 1 m gives 24 h 0 m, and hours may be more than 23.
Algorithm
maincreates three objects of classTime:t1,t2andt3. The default constructor setshoursandminutesto 0 in each.mainalso creates oneScanner,sc.t1.accept(sc)reads hours and minutes intot1and returnstrueifhours >= 0and0 <= minutes < 60. If it returnsfalse,mainprintsINVALID INPUTand stops.t2.accept(sc)does the same for the second time. The sameScanneris passed to both calls so that no typed input is lost.t3.add(t1, t2)works on the current objectt3:totalMin = t1.minutes + t2.minutes;hours = t1.hours + t2.hours + totalMin / 60;minutes = totalMin % 60.
mainprintsSum =and callst3.display(), which printshours, the wordhours,minutesand the wordminutes.
Program
The class Time has the data members hours and minutes. accept() reads and validates one time, add() fills the current object with the sum of two others, and display() prints it.
import java.util.Scanner;
/* Class Time stores a time as hours and minutes. It reads two times, adds
them and displays the sum with the minutes brought below 60. The hours are
not wrapped at 24, so the sum may exceed 23 hours. */
public class Time
{
int hours; // number of hours (0 or more)
int minutes; // number of minutes (0 to 59)
/* Default constructor: initialises the data members to zero */
Time()
{
hours = 0;
minutes = 0;
}
/* Parameter sc: the Scanner used to read from the keyboard (one Scanner
is shared by both objects). Reads hours and minutes into the data
members. Returns true if hours >= 0 and 0 <= minutes < 60, else false. */
boolean accept(Scanner sc)
{
System.out.print("Enter hours: ");
hours = sc.nextInt();
System.out.print("Enter minutes: ");
minutes = sc.nextInt();
return hours >= 0 && minutes >= 0 && minutes < 60;
}
/* Parameters t1 and t2: the two times to be added.
Stores their sum in the current object (hours and minutes). */
void add(Time t1, Time t2)
{
int totalMin = t1.minutes + t2.minutes; // minutes before normalising
hours = t1.hours + t2.hours + totalMin / 60; // every 60 minutes carries 1 hour
minutes = totalMin % 60; // minutes left, below 60
}
/* Prints the time of the current object as hours and minutes */
void display()
{
System.out.println(hours + " hours " + minutes + " minutes");
}
/* main creates the objects and calls the methods */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads the input
Time t1 = new Time(); // first time
Time t2 = new Time(); // second time
Time t3 = new Time(); // sum of the two times
System.out.println("First time");
if (!t1.accept(sc))
{
System.out.println("INVALID INPUT");
return;
}
System.out.println("Second time");
if (!t2.accept(sc))
{
System.out.println("INVALID INPUT");
return;
}
t3.add(t1, t2);
System.out.print("Sum = ");
t3.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
hours | int | Data member: number of hours of the time |
minutes | int | Data member: number of minutes of the time (0 to 59 for a valid time) |
sc | Scanner | Reads the input. Created in main and passed to accept() as a parameter |
t1 | Time | In main: the first time. In add(): parameter, the first time to be added |
t2 | Time | In main: the second time. In add(): parameter, the second time to be added |
t3 | Time | Object created in main that receives the sum |
totalMin | int | In add(): the sum of the two minutes before normalising |
args | String[] | Command-line arguments of main (not used) |
Sample input and output
Sample 1
First time
Enter hours: 2
Enter minutes: 45
Second time
Enter hours: 3
Enter minutes: 30
Sum = 6 hours 15 minutesSample 2
First time
Enter hours: 1
Enter minutes: 30
Second time
Enter hours: 1
Enter minutes: 30
Sum = 3 hours 0 minutesSample 3
First time
Enter hours: 0
Enter minutes: 0
Second time
Enter hours: 0
Enter minutes: 0
Sum = 0 hours 0 minutesSample 4
First time
Enter hours: 5
Enter minutes: 0
Second time
Enter hours: 0
Enter minutes: 60
INVALID INPUTDry run
Input: first time 2 h 45 m, second time 3 h 30 m.
| Step | t1.hours | t1.minutes | t2.hours | t2.minutes | totalMin | t3.hours | t3.minutes |
|---|---|---|---|---|---|---|---|
| Objects created | 0 | 0 | 0 | 0 | - | 0 | 0 |
t1.accept(sc) reads 2, 45 | 2 | 45 | 0 | 0 | - | 0 | 0 |
t2.accept(sc) reads 3, 30 | 2 | 45 | 3 | 30 | - | 0 | 0 |
totalMin = 45 + 30 | 2 | 45 | 3 | 30 | 75 | 0 | 0 |
hours = 2 + 3 + 75 / 60 | 2 | 45 | 3 | 30 | 75 | 6 | 0 |
minutes = 75 % 60 | 2 | 45 | 3 | 30 | 75 | 6 | 15 |
display() prints 6 hours 15 minutes after Sum =, which is the same as the real run of the first sample above.
Variations
- Subtract times "Write a program to subtract one time from another, each given in hours and minutes."
- 24-hour clock "Write a program to add two times so that the hours wrap at 24."
- Minutes to hours "Write a program to convert a number of minutes into hours and minutes."