Complex Number Operations In Java
To design a class Complex to represent operations on Complex numbers,ie. Addition, subtraction, modulus, conjugate, multiplication, division using objects of the same class.
import java.util.*;
class Complex
{ double r,i;
public Complex()
{r=0;
i=0;
}
public Complex(double a,double b)
{r=a;
i=b;
}
public double mod()
{ return Math.sqrt(r*r+i*i);}
public Complex conju()
{Complex ob= new Complex();
ob.r=this.r;
ob.i=-this.i;
return ob;
}
public Complex sum(Complex ob)
{ Complex xy= new Complex();
xy.r=this.r+ob.r;
xy.i=this.i+ob.i;
return xy;
}//end of sum()
public Complex dif(Complex ob)
{ Complex xy1= new Complex();
xy1.r=this.r-ob.r;
xy1.i=this.i-ob.i;
return xy1;
}//end of dif()
public Complex mul(Complex ob)
{ Complex p= new Complex();
p.r=this.r*ob.r-ob.i*this.i;
p.i=this.r*ob.i-ob.r*this.i;
return p;
}//end of mul()
public Complex div(Complex ob)
{ double den=Math.pow(ob.mod(),2);
Complex d= new Complex();
d.r=(this.r*ob.r+ob.i*this.i)/den;
d.i=(this.i*ob.r-ob.i*this.r)/den;
return d;
}//end of div()
public void display()
{ System.out.println(r+"i"+i);}//end of display()
public static void main()
{ int c;double u,v,w,z,d=0;
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division");
c=sc.nextInt();
if(c==3)
{ System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.sum(ob2);
System.out.println("sum is "+ob3.r+"+"+ob3.i+"i");
}
else if(c==1)
{ System.out.println("enter the complex number");
w=sc.nextDouble();
z=sc.nextDouble();
Complex ob1=new Complex(w,z);
d=ob1.mod();
System.out.println("the modulus is "+d);
}
else if(c==4)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.dif(ob2);
System.out.println("difference is "+ob3.r+"i"+ob3.i);
}else if(c==5)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.mul(ob2);
System.out.println("multiplied number is "+ob3.r+"i"+ob3.i);
}
else if(c==6)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.dif(ob2);
System.out.println("divided complex number is "+ob3.r+"i"+ob3.i);
}
else if(c==2)
{ System.out.println("enter the complex number");
w=sc.nextDouble();
z=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob3=new Complex();
ob3=ob1.conju();
System.out.println("the conjugate is "+ob3.r+ob3.i+"i");
}
else
System.out.println("Wrong Choice");
}//end of class
}//end of class
OUTPUT---
Press 1 for mod, 2 for conjugate, 3 for sum,4 for difference,5 for multiplication, 6 for division
1
enter the complex number
2
5
the modulus is 5.385164807134504
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
2
enter the complex number
5
4
the conjugate is 5.0-4.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
3
enter the 1st complex number
5
4
enter the 2nd complex number
3
2
sum is 8.0+6.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
4
enter the 1st complex number
5
6
enter the 2nd complex number
2
3
difference is 3.0+3.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
5
enter the 1st complex number
2
3
enter the 2nd complex number
4
9
multiplied number is -19.0+6.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
6
enter the 1st complex number
5
3
enter the 2nd complex number
7
2
divided complex number is -2.0+1.0i
import java.util.*;
class Complex
{ double r,i;
public Complex()
{r=0;
i=0;
}
public Complex(double a,double b)
{r=a;
i=b;
}
public double mod()
{ return Math.sqrt(r*r+i*i);}
public Complex conju()
{Complex ob= new Complex();
ob.r=this.r;
ob.i=-this.i;
return ob;
}
public Complex sum(Complex ob)
{ Complex xy= new Complex();
xy.r=this.r+ob.r;
xy.i=this.i+ob.i;
return xy;
}//end of sum()
public Complex dif(Complex ob)
{ Complex xy1= new Complex();
xy1.r=this.r-ob.r;
xy1.i=this.i-ob.i;
return xy1;
}//end of dif()
public Complex mul(Complex ob)
{ Complex p= new Complex();
p.r=this.r*ob.r-ob.i*this.i;
p.i=this.r*ob.i-ob.r*this.i;
return p;
}//end of mul()
public Complex div(Complex ob)
{ double den=Math.pow(ob.mod(),2);
Complex d= new Complex();
d.r=(this.r*ob.r+ob.i*this.i)/den;
d.i=(this.i*ob.r-ob.i*this.r)/den;
return d;
}//end of div()
public void display()
{ System.out.println(r+"i"+i);}//end of display()
public static void main()
{ int c;double u,v,w,z,d=0;
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division");
c=sc.nextInt();
if(c==3)
{ System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.sum(ob2);
System.out.println("sum is "+ob3.r+"+"+ob3.i+"i");
}
else if(c==1)
{ System.out.println("enter the complex number");
w=sc.nextDouble();
z=sc.nextDouble();
Complex ob1=new Complex(w,z);
d=ob1.mod();
System.out.println("the modulus is "+d);
}
else if(c==4)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.dif(ob2);
System.out.println("difference is "+ob3.r+"i"+ob3.i);
}else if(c==5)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.mul(ob2);
System.out.println("multiplied number is "+ob3.r+"i"+ob3.i);
}
else if(c==6)
{System.out.println("enter the 1st complex number");
w=sc.nextDouble();
z=sc.nextDouble();
System.out.println("enter the 2nd complex number");
u=sc.nextDouble();
v=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob2=new Complex(u,v);
Complex ob3=new Complex();
ob3=ob1.dif(ob2);
System.out.println("divided complex number is "+ob3.r+"i"+ob3.i);
}
else if(c==2)
{ System.out.println("enter the complex number");
w=sc.nextDouble();
z=sc.nextDouble();
Complex ob1=new Complex(w,z);
Complex ob3=new Complex();
ob3=ob1.conju();
System.out.println("the conjugate is "+ob3.r+ob3.i+"i");
}
else
System.out.println("Wrong Choice");
}//end of class
}//end of class
OUTPUT---
Press 1 for mod, 2 for conjugate, 3 for sum,4 for difference,5 for multiplication, 6 for division
1
enter the complex number
2
5
the modulus is 5.385164807134504
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
2
enter the complex number
5
4
the conjugate is 5.0-4.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
3
enter the 1st complex number
5
4
enter the 2nd complex number
3
2
sum is 8.0+6.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
4
enter the 1st complex number
5
6
enter the 2nd complex number
2
3
difference is 3.0+3.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
5
enter the 1st complex number
2
3
enter the 2nd complex number
4
9
multiplied number is -19.0+6.0i
Press 1 for mod, 2 for conjugate,3 for sum,4 for difference,5 for multiplication, 6 for division
6
enter the 1st complex number
5
3
enter the 2nd complex number
7
2
divided complex number is -2.0+1.0i
Comments
Post a Comment