public class ComplexNumber
{
//Imaginary and real numbers
private double im,re;
//Constructor to decide numbers (r,ri)
public ComplexNumber(double r,double i)
{
this.re=r;
this.im=i;
}
public double getReal()
{
return this.re;
}
public double getImaginary()
{
return this.im;
}
//this provides to take reciprocal
public ComplexNumber reciprocal()
{
double bölüm=re*re+im*im;
return new ComplexNumber(re/bölüm,-im/bölüm);
}
//add operation
public ComplexNumber add(ComplexNumber newnumber)
{
double x=re+newnumber.re;
double y=im+newnumber.im;
return new ComplexNumber(x,y);
}
//subtract operation
public ComplexNumber subtract(ComplexNumber newnumber)
{
double sub=re-newnumber.re;
double sub2=im-newnumber.im;
return new ComplexNumber(sub,sub2);
}
//multiply operation
public ComplexNumber multiply(ComplexNumber newnumber)
{
double op1=im*newnumber.re+re*newnumber.im;
double mul2=im*newnumber.im*-1;
double mul=newnumber.re*re+mul2;
return new ComplexNumber(mul,op1);
}
//divide operation
public ComplexNumber divide(ComplexNumber newnumber)
{
double ac=(im*newnumber.re-newnumber.im*(re));
double av=im*newnumber.re+re*newnumber.im;
double ab=re*newnumber.re+im*newnumber.im;
return new ComplexNumber(av/ab,ac/ab);
}
//Conjugation operation
public ComplexNumber conjugate(ComplexNumber newnumber)
{
return new ComplexNumber(re,-im);
}
//equals:if re and im equal so they are equal
public boolean equals(ComplexNumber newnumber)
{
return (re==newnumber.re && im==newnumber.im);
}
//Get angle:I benefit from atan and todegrees from Math class of java.
public double getAngle(ComplexNumber newnumber)
{
return Math.toDegrees(Math.atan(re/im));
}
//Magnitude:squart (re)^2+(im)^2
public double getMagnitude(ComplexNumber newnumber)
{
return Math.sqrt(Math.pow(re,2)+Math.pow(im, 2));
}
public String toString()
{
if(im==0){
//im=0 so we dont write "i"
return re+"";
}
if(im<0){
return re+"-"+ (-im)+"i";
//if im<0 i write -im and symbol is not +,it is-,
}
if(re==0){
return im+"i";
//if re =0 we dont write re
}
return re+"+"+im+"i";
}
}