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

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 / 60 and the minutes left are total % 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

  1. main creates three objects of class Time: t1, t2 and t3. The default constructor sets hours and minutes to 0 in each. main also creates one Scanner, sc.
  2. t1.accept(sc) reads hours and minutes into t1 and returns true if hours >= 0 and 0 <= minutes < 60. If it returns false, main prints INVALID INPUT and stops.
  3. t2.accept(sc) does the same for the second time. The same Scanner is passed to both calls so that no typed input is lost.
  4. t3.add(t1, t2) works on the current object t3:
    • totalMin = t1.minutes + t2.minutes;
    • hours = t1.hours + t2.hours + totalMin / 60;
    • minutes = totalMin % 60.
  5. main prints Sum = and calls t3.display(), which prints hours, the word hours, minutes and the word minutes.

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

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)
scScannerReads the input. Created in main and passed to accept() as a parameter
t1TimeIn main: the first time. In add(): parameter, the first time to be added
t2TimeIn main: the second time. In add(): parameter, the second time to be added
t3TimeObject created in main that receives the sum
totalMinintIn add(): the sum of the two minutes before normalising
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: first time 2 h 45 m, second time 3 h 30 m.

Stept1.hourst1.minutest2.hourst2.minutestotalMint3.hourst3.minutes
Objects created0000-00
t1.accept(sc) reads 2, 4524500-00
t2.accept(sc) reads 3, 30245330-00
totalMin = 45 + 302453307500
hours = 2 + 3 + 75 / 602453307560
minutes = 75 % 6024533075615

display() prints 6 hours 15 minutes after Sum =, which is the same as the real run of the first 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."