Exp 3
Exp 3
OBJECT PASSING
AIM
A class contains time in hours, minutes and seconds. Write a Java program to add two time objects
by passing and returning objects.
OBJECIVES
To implement Java program to handle passing and returning of objects.
COURSE OUTCOME
CO1 Develop programs using Java class & object
MODULE OUTCOME
M1.02 Implement java programs that handle objects such as array of objects, passing and
returning objects as arguments.
PROGRAM
import java.io.*;
class Time
{
int h,m,s;
Time add(Time x)
{
int hr=0, mt=0, sc;
Time y=new Time();
sc = s + x.s;
if (sc >=60)
{
mt++;
sc%=60;
}
mt += m + x.m;
if(mt >= 60)
{
hr++;
mt%=60;
}
hr += h + x.h;
if(hr >= 24) hr=0;
return(y);
}
void display()
{
System.out.println(h+":"+m+":"+s);
}
t3 = t1.add(t2);
System.out.print("First time: ");
t1.display();
System.out.print("Second time: ");
t2.display();
System.out.print("Sum of time: ");
t3.display();
}
}
OBSERVATIONS
1)
Enter first time in hour, minutes and seconds
2
30
25
Enter second time in hour, minutes and seconds
1
10
15
First time: 2:30:25
Second time: 1:10:15
Sum of time: 3:40:40
2)
Enter first time in hour, minutes and seconds
2
30
40
Enter second time in hour, minutes and seconds
1
25
30
First time: 2:30:40
Second time: 1:25:30
Sum of time: 3:56:10
RESULT
Studied implementation of passing and returning objects as arguments using Java program.