Skip to content

Instantly share code, notes, and snippets.

@dapurv5
Created January 2, 2013 19:34
Show Gist options
  • Save dapurv5/4437216 to your computer and use it in GitHub Desktop.
Save dapurv5/4437216 to your computer and use it in GitHub Desktop.
Complex Number representation
import java.lang.Math;
public class Complex {
protected double re;
protected double im;
public Complex(double real, double imag) {
re = real;
im = imag;
}
public double getReal(){
return re;
}
public double getImaginary(){
return im;
}
@Override
public String toString(){
return re + ((Math.signum(im) >= 0)?" + ":" - ") + Math.abs(im)+"i";
}
@Override
public boolean equals(Object obj){
if(this == obj)
return true;
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
Complex that = (Complex)obj;
return (that.re == this.re) && (that.im == this.im);
}
public Complex add(Complex b){
re = re+b.re;
im = im+b.im;
return this;
}
public static Complex add(Complex a, Complex b){
return new Complex(a.re + b.re, a.im + b.im);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment