ISC Practicals and Theory - Time Addition (OOP)
Updated 21 Oct 2026
This program adds two times, each given as hours and minutes, and prints the sum with the minutes below 60: 2 h 45 m + 3 h 30 m = 6 h 15 m. This is the object-passing version. The class Time has a parameterised constructor Time(int h, int m), and the method Time add(Time t) takes an object as a parameter and returns a new object holding the sum. main creates three objects: t1, t2 and the sum t3.
Definition
A time has hours and minutes. To add two times, add the minutes; every 60 minutes carry 1 hour (total / 60), the minutes left are total % 60, and the hours of the sum are the two hours plus the carry. Example: 1 h 30 m + 1 h 30 m: minutes 30 + 30 = 60, carry 1, minutes left 0, hours 1 + 1 + 1 = 3, so the sum is 3 h 0 m.
A time is valid when hours >= 0 and 0 <= minutes < 60. The hours are not wrapped at 24, so the sum may be 24 hours or more.
Algorithm
mainreadsh1,m1(first time) andh2,m2(second time) with oneScanner.- It creates
t1 = new Time(h1, m1)andt2 = new Time(h2, m2). The constructor copies the parametershandmintohoursandminutes. isValid()is called on both objects. If either returnsfalse,mainprintsINVALID INPUT. Both times are read before they are checked.- Otherwise
Time t3 = t1.add(t2)is called. Inadd(Time t)the current object ist1and the parametertist2:totalMin = minutes + t.minutes;h = hours + t.hours + totalMin / 60;m = totalMin % 60;return new Time(h, m)returns a new object. Neithert1nort2is changed.
mainprintsSum =and callst3.display(), which prints the hours and the minutes.
Program
The data members are hours and minutes. The constructor sets them, isValid() checks them, add() returns the sum as a new Time and display() prints a time.
import java.util.Scanner;
/* Class Time stores a time as hours and minutes. This version uses a
parameterised constructor, and its method add() takes a Time object as a
parameter and returns a new Time object holding the sum. 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)
/* Parameterised constructor. Parameters h and m: the hours and minutes
of the time being created. */
Time(int h, int m)
{
hours = h;
minutes = m;
}
/* Returns true if hours >= 0 and 0 <= minutes < 60, otherwise false */
boolean isValid()
{
return hours >= 0 && minutes >= 0 && minutes < 60;
}
/* Parameter t: the time to be added to the current object.
Returns a new Time object holding the sum, minutes below 60. */
Time add(Time t)
{
int totalMin = minutes + t.minutes; // minutes before normalising
int h = hours + t.hours + totalMin / 60; // every 60 minutes carries 1 hour
int m = totalMin % 60; // minutes left, below 60
return new Time(h, m); // the object is returned
}
/* Prints the time of the current object as hours and minutes */
void display()
{
System.out.println(hours + " hours " + minutes + " minutes");
}
/* main reads the two times, creates three objects and calls the methods */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("First time");
System.out.print("Enter hours: ");
int h1 = sc.nextInt(); // hours of the first time
System.out.print("Enter minutes: ");
int m1 = sc.nextInt(); // minutes of the first time
System.out.println("Second time");
System.out.print("Enter hours: ");
int h2 = sc.nextInt(); // hours of the second time
System.out.print("Enter minutes: ");
int m2 = sc.nextInt(); // minutes of the second time
Time t1 = new Time(h1, m1); // first time
Time t2 = new Time(h2, m2); // second time
if (!t1.isValid() || !t2.isValid())
System.out.println("INVALID INPUT");
else
{
Time t3 = t1.add(t2); // sum, returned as a new object
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) |
h | int | In the constructor: parameter, the hours of the new time. In add(): the hours of the sum |
m | int | In the constructor: parameter, the minutes of the new time. In add(): the minutes of the sum |
t | Time | Parameter of add(): the time to be added to the current object |
totalMin | int | In add(): the sum of the two minutes before normalising |
sc | Scanner | Reads the input in main |
h1 | int | In main: hours of the first time |
m1 | int | In main: minutes of the first time |
h2 | int | In main: hours of the second time |
m2 | int | In main: minutes of the second time |
t1 | Time | Object for the first time |
t2 | Time | Object for the second time |
t3 | Time | Object returned by add(): the sum |
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: 1 h 30 m and 1 h 30 m. main reads h1 = 1, m1 = 30, h2 = 1, m2 = 30 and creates t1 (1, 30) and t2 (1, 30). Both are valid. Then t1.add(t2) runs:
| Step | minutes (of t1) | t.minutes | hours (of t1) | t.hours | Value |
|---|---|---|---|---|---|
totalMin = minutes + t.minutes | 30 | 30 | 1 | 1 | totalMin = 60 |
h = hours + t.hours + totalMin / 60 | 30 | 30 | 1 | 1 | h = 1 + 1 + 1 = 3 |
m = totalMin % 60 | 30 | 30 | 1 | 1 | m = 0 |
return new Time(h, m) | t3 = 3 h 0 m |
display() prints 3 hours 0 minutes after Sum =, the same as the real run of the second 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."