Monday, January 20, 2014

Java Program to illustrate Passing and Returning Object

with 0 Comment
import java.io.*;
import java.lang.*;
class complex
{
double x,y;
complex()
{
x=y=0.0;
}
complex(double real,double imag)
{
x=real;
y=imag;
}
complex add(complex c1,complex c2)
{
complex c=new complex();
c.x=c1.x+c2.x;
c.y=c1.y+c2.y;
return c;
}
void display()
{
System.out.println(x+"+i"+y);
}
}
class complexdemo
{
public static void main(String args[])throws IOException
{
DataInputStream x=new DataInputStream(System.in);
double a,b,c,d;
System.out.println("Passing & Returning Object");
System.out.println("*****************");
System.out.println("Enter the first real & imaginary value");
a=Double.parseDouble(x.readLine());
b=Double.parseDouble(x.readLine());
System.out.println("Enter the Second real & imaginary value");
c=Double.parseDouble(x.readLine());
d=Double.parseDouble(x.readLine());
complex c1=new complex(a,b);
complex c2=new complex(c,d);
c3=c3.add(c1,c2);
System.out.println("First Complex Value:");
c1.display();
System.out.println("Second Complex Value:");
c2.display();
System.out.println("Result:");
c3.display();
}
}

-------------
OUTPUT:
*********
Passing and Returning Object
***********************
Enter the first real & imaginary value
3.4
4.5

Enter the Second real & imaginary value
3.2
6.2

First Complex Value       :3.4+i4.5
Second Complex Value   :3.2+i6.2

Result                              :6.6+i10.7

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive