Excersise: 3 Add to Complex Number | Java | Tamizhan Goals

Add to Complex Number

Excersise: 3

    Create a Program for object as parameters and returning objects.

Program


import java.io.*;
import java.lang.*;
class complex
{
	double rp,ip;
	complex(double real,double img)
	{
		rp=real;
		ip=img;
	}
	complex()
	{
	}
	complex add(complex c1,complex c2)
	{
		complex c3=new complex();
		c3.rp=c1.rp+c2.rp;
		c3.ip=c1.ip+c2.ip;
		return(c3);
	}
}
class Cdemo
{
	public static void main(String arg[])throws IOException
	{
		DataInputStream din=new DataInputStream(System.in);
		System.out.println("Enter the Real and Imaginary parts of complex number 1:");
		double r1=Double.parseDouble(din.readLine());
		double img1=Double.parseDouble(din.readLine());
		System.out.println("Enter the Real and Imaginary parts of complex number 2:");
		double r2=Double.parseDouble(din.readLine());
		double img2=Double.parseDouble(din.readLine());
		System.out.println("The First Complex Number is:"+r1+"+i"+img1);
		System.out.println("The Second Complex Number is:"+r2+"+i"+img2);
		complex com1=new complex(r1,img1);
		complex com2=new complex(r2,img2);
		complex c4=new complex();
		complex c5=new complex();
		c5=c4.add(com1,com2);
		System.out.println("The Sum of Two Complex Number is:"+c5.rp+"+i"+c5.ip);
	}
}

Output

Post a Comment

Thanks for your comments. Please be visit and given positive review on our site.

Previous Post Next Post