A class TimeOp has been defined to add any two accepted time periods. Example: Time A = 6 hours 35…
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 21minutes 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.
Answer
Answer
AIWritten by AI (antigravity) - it can contain mistakes.
import java.util.Scanner;
class TimeOp
{
int arr[];
TimeOp()
{
arr = new int[3];
}
void readTime()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter hours: ");
arr[0] = sc.nextInt();
System.out.print("Enter minutes: ");
arr[1] = sc.nextInt();
System.out.print("Enter seconds: ");
arr[2] = sc.nextInt();
}
TimeOp addTime(TimeOp tt)
{
TimeOp result = new TimeOp();
int totalSec = this.arr[2] + tt.arr[2];
int sec = totalSec % 60;
int carryMin = totalSec / 60;
int totalMin = this.arr[1] + tt.arr[1] + carryMin;
int min = totalMin % 60;
int carryHr = totalMin / 60;
int hr = this.arr[0] + tt.arr[0] + carryHr;
result.arr[0] = hr;
result.arr[1] = min;
result.arr[2] = sec;
return result;
}
void dispTime()
{
System.out.println(arr[0] + " hours " + arr[1] + " minutes " + arr[2] + " seconds");
}
public static void main(String[] args)
{
System.out.println("Enter details for first time period:");
TimeOp t1 = new TimeOp();
t1.readTime();
System.out.println("Enter details for second time period:");
TimeOp t2 = new TimeOp();
t2.readTime();
TimeOp t3 = t1.addTime(t2);
System.out.println("First Time:");
t1.dispTime();
System.out.println("Second Time:");
t2.dispTime();
System.out.println("Added Time:");
t3.dispTime();
}
}Explanation: The default constructor initialises the array arr of size 3. readTime accepts hours, minutes, and seconds. addTime adds corresponding elements with carry propagation (60 seconds = 1 minute, 60 minutes = 1 hour) and returns a new TimeOp object. dispTime displays the result.From ISC 2026 Computer Science Paper 1, question 6.