Categories
Java

Addition of two complex numbers passing object as arguments

java program to add two complex numbers by passing objects as arguments in a user defined method.

import java.io.*;
class Complex
{
int rp,ip;
void get()throws Exception
{
System.out.println("Enter real part and imaginary part");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
rp=Integer.parseInt(br.readLine());
ip=Integer.parseInt(br.readLine());
}
void pro(Complex a,Complex b)
{	
	rp=a.rp+b.rp;
	ip=a.ip+b.ip;
}
void show()
{
	if(ip>0)
		System.out.println(rp+"+"+ip+"i");
	else
		System.out.println(rp+" "+ip+"i");
}
}
class CMain
{
	public static void main(String arg[])throws Exception
	{
		Complex a=new Complex();
		Complex b=new Complex();
		Complex c=new Complex();
		a.get();
		b.get();
		c.pro(a,b);
		c.show();	
	}	
}

Categories
Java

Addition of two Complex Numbers using object as a return value

java program to add two complex numbers using object as a return value.

import java.io.*;
class Complex
{
int rp,ip;
void get()throws Exception
{
System.out.println("Enter real part and imaginary part");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
rp=Integer.parseInt(br.readLine());
ip=Integer.parseInt(br.readLine());

}
Complex pro(Complex b)
{
	Complex c=new Complex();
	c.rp=rp+b.rp;
	c.ip=ip+b.ip;
	return(c);
	
}
void show()
{
	if(ip>0)
		System.out.println(rp+"+"+ip+"i");
	else
		System.out.println(rp+" "+ip+"i");
}
}
class CMain
{
	public static void main(String arg[])throws Exception
	{
		Complex a=new Complex();
		Complex b=new Complex();
		Complex c=new Complex();
		a.get();
		b.get();
		c=a.pro(b);
		c.show();
		}
}