Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. main reads h1, m1 (first time) and h2, m2 (second time) with one Scanner.
  2. It creates t1 = new Time(h1, m1) and t2 = new Time(h2, m2). The constructor copies the parameters h and m into hours and minutes.
  3. isValid() is called on both objects. If either returns false, main prints INVALID INPUT. Both times are read before they are checked.
  4. Otherwise Time t3 = t1.add(t2) is called. In add(Time t) the current object is t1 and the parameter t is t2:
    • totalMin = minutes + t.minutes;
    • h = hours + t.hours + totalMin / 60;
    • m = totalMin % 60;
    • return new Time(h, m) returns a new object. Neither t1 nor t2 is changed.
  5. main prints Sum = and calls t3.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

VariableData TypePurpose
hoursintData member: number of hours of the time
minutesintData member: number of minutes of the time (0 to 59 for a valid time)
hintIn the constructor: parameter, the hours of the new time. In add(): the hours of the sum
mintIn the constructor: parameter, the minutes of the new time. In add(): the minutes of the sum
tTimeParameter of add(): the time to be added to the current object
totalMinintIn add(): the sum of the two minutes before normalising
scScannerReads the input in main
h1intIn main: hours of the first time
m1intIn main: minutes of the first time
h2intIn main: hours of the second time
m2intIn main: minutes of the second time
t1TimeObject for the first time
t2TimeObject for the second time
t3TimeObject returned by add(): the sum
argsString[]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 minutes

Sample 2

First time
Enter hours: 1
Enter minutes: 30
Second time
Enter hours: 1
Enter minutes: 30
Sum = 3 hours 0 minutes

Sample 3

First time
Enter hours: 0
Enter minutes: 0
Second time
Enter hours: 0
Enter minutes: 0
Sum = 0 hours 0 minutes

Sample 4

First time
Enter hours: 5
Enter minutes: 0
Second time
Enter hours: 0
Enter minutes: 60
INVALID INPUT

One value per line

Dry 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:

Stepminutes (of t1)t.minuteshours (of t1)t.hoursValue
totalMin = minutes + t.minutes303011totalMin = 60
h = hours + t.hours + totalMin / 60303011h = 1 + 1 + 1 = 3
m = totalMin % 60303011m = 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

  1. Subtract times "Write a program to subtract one time from another, each given in hours and minutes."
  2. 24-hour clock "Write a program to add two times so that the hours wrap at 24."
  3. Minutes to hours "Write a program to convert a number of minutes into hours and minutes."