0% found this document useful (0 votes)
16 views3 pages

Exp 3

The document describes a Java program that adds two time objects by passing and returning them. It defines a Time class with fields for hours, minutes, and seconds, and a method to add two Time objects. The main method gets time input from the user, creates Time objects, calls the add method to sum the times, and displays the results. The program output demonstrates adding different time values and correctly handling rollover of hours and minutes.

Uploaded by

hejafa9465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Exp 3

The document describes a Java program that adds two time objects by passing and returning them. It defines a Time class with fields for hours, minutes, and seconds, and a method to add two Time objects. The main method gets time input from the user, creates Time objects, calls the add method to sum the times, and displays the results. The program output demonstrates adding different time values and correctly handling rollover of hours and minutes.

Uploaded by

hejafa9465
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO 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;

void setTime(int hh, int mm, int ss)


{
h=hh;
m=mm;
s=ss;
}

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;

y.setTime(hr, mt, sc);

return(y);
}

void display()
{
System.out.println(h+":"+m+":"+s);
}

public class Exp3


{
public static void main(String arg[]) throws IOException
{
Time t1, t2, t3;
int h,m,s;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(in);

System.out.println("Enter first time in hour, minutes and seconds");


h=Integer.parseInt(b.readLine());
m=Integer.parseInt(b.readLine());
s=Integer.parseInt(b.readLine());
t1=new Time();
t1.setTime(h, m, s);

System.out.println("Enter second time in hour, minutes and seconds");


h=Integer.parseInt(b.readLine());
m=Integer.parseInt(b.readLine());
s=Integer.parseInt(b.readLine());
t2=new Time();
t2.setTime(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.

You might also like