3. Write a program to add two different time variables and display the output using
following details in class Time :
Members
Member Name
Description
Member variables
int hr, min
To store hours and minutes value
Member Method
input(int n, int m)
To accept hours and minutes
addTime(Time tl,
Time t2)
To add two different time variables
display
To display time
Example : 3 hrs 40 min + 3 hrs 40 min = 7 hrs 20 min
Answers
Answer:
class Time
{
int hr, min;
void input(int h, int m)
{
hr=h;
min=m;
}
void addtime(Time t1, Time t2)
{
hr=t1.hr+t2.hr;
min=t1.min+t2.min;
if(min>60)
{
min=min-60;
hr=hr+1;
}
}
void display()
{
System.out.println("Time="+hr+"h"+min+"min");
}
}
Explanation:
The above program has been done in java coding language of OOP Environment. Hope it helps!
Answer:
public class Time
{
int hr,min;
public void input(int h, int m)
{
hr=h;
min=m;
}
public void addtime(Time t1, Time t2)
{
hr=t1.hr+t2.hr;
min=t1.min+t2.min;
if(min>=60)
{
min=min-60;
hr=hr+1;
}
}
public void display()
{
System.out.println("Time="+hr+"h"+min+"min");
}
public static void main()
{
Time y1=new Time();
Time t1 =new Time();
Time t2 =new Time();
t1.input(3,30);
t2.input(2,30);
y1.addtime(t1,t2);
y1.display();
}
}