Define a class with following specification.
Class Name: TimeAdder
Member Variables:
t[] – Array to store two elements that is hours and minutes.
Member Methods:
TimeAdder() – Default constructor to initialize member variables.
void getTime() – Accept the time using Scanner class.
void addTime(TimeAdder X,TimeAdder Y) –
To add the time stored in the given object.
void showTime() –
To print the added time.
Invoke all the above methods in main() function using object of the class.
Example: 7 Hours 45 Minutes + 5 Hours 35 Minutes = 13 Hours 20 Minutes
Note: 60 Minutes = 1 Hour
Java
import java.util.*;
public class TimeAdder
{
int t[];
TimeAdder()
{
t=new int[2];
}
public void getTime()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter time in Hours and Minutes:-");
System.out.print("Hours: ");
t[0]=sc.nextInt();
System.out.print("Minutes: ");
t[1]=sc.nextInt();
System.out.println();
}
public void addTime(TimeAdder X,TimeAdder Y)
{
t[1]=X.t[1]+Y.t[1];
t[0]=X.t[0]+Y.t[0]+(t[1]/60);
t[1]=t[1]%60;
}
public void showTime()
{
System.out.println("Hours: "+t[0]+"\nMinutes: "+t[1]);
}
public static void main(String args[])
{
TimeAdder ob1=new TimeAdder();
TimeAdder ob2=new TimeAdder();
TimeAdder ob3=new TimeAdder();
ob1.getTime();
ob2.getTime();
ob3.addTime(ob1,ob2);
ob3.showTime();
}
}Java