-
-
Save whaozl/2e4c88fd0945fe14e78f54a9163ad47b to your computer and use it in GitHub Desktop.
Complex Number representation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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