Add Two Time Periods | ISC Computer Science 2026 Theory

A class TimeOp has been defined to add any two accepted time periods.
Example:
Time A = 6 hours 35 minutes 40 seconds
Time B = 7 hours 45 minutes 30 seconds
Time A + Time B = 14 hours 21 minutes 10 seconds
(where 60 minutes = 1 hour and 60 seconds = 1 minute)

The details of the members of the class are given below:
Class name: TimeOp
Data member/instance variable:
arr[]: integer array to hold three elements (hours, minutes and seconds)
Methods/Member functions:
TimeOp(): default constructor
void readTime(): to accept the elements of the array
TimeOp addTime(TimeOp tt): to add the time of the parameterised object tt and the current object, to store it in a local object and return it
void dispTime(): to display the array elements in hours:minutes:seconds format

Specify the class TimeOp giving the details of the constructor, void readTime(), TimeOp addTime(TimeOp) and void dispTime(). Define the main() function to create objects and call the functions accordingly to enable the task.

import java.util.Scanner;
class TimeOp{
    private int arr[];
    public TimeOp(){
        arr = new int[3];
    }
    public void readTime(){
        Scanner in = new Scanner(System.in);
        System.out.print("Hours: ");
        arr[0] = in.nextInt();
        System.out.print("Minutes: ");
        arr[1] = in.nextInt();
        System.out.print("Seconds: ");
        arr[2] = in.nextInt();
    }
    public TimeOp addTime(TimeOp tt){
        TimeOp temp = new TimeOp();
        temp.arr[2] = (this.arr[2] + tt.arr[2]) % 60;
        int minExtra = (this.arr[2] + tt.arr[2]) / 60;
        temp.arr[1] = (this.arr[1] + tt.arr[1] + minExtra) % 60;
        int hrExtra = (this.arr[1] + tt.arr[1]) / 60;
        temp.arr[0] = this.arr[0] + tt.arr[0] + hrExtra;
        return temp;
    }
    public void dispTime(){
        System.out.println(arr[0] + ":" + arr[1] + ":" + arr[2]);
    }
    public static void main(String[] args){
        TimeOp t1 = new TimeOp();
        TimeOp t2 = new TimeOp();
        TimeOp t3 = new TimeOp();
        System.out.println("First time period:");
        t1.readTime();
        System.out.println("Second time period:");
        t2.readTime();
        t3 = t1.addTime(t2);
        t3.dispTime();
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *