package ClassesIntro;
public class ComplexNumber {
private double real;
private double imaginary;
public ComplexNumber(double initReal, double initIm) {
setReal(initReal);
setImaginary(initIm);
}
public ComplexNumber multiply(ComplexNumber otherNumber) {
double a = this.real;
double b = this.imaginary;
double c = otherNumber.real;
double d = otherNumber.imaginary;
double nReal = a * c - b * d;
double nImaginary = a * d + b * c;
return new ComplexNumber(nReal, nImaginary);
}
@Override
public String toString() {
String formatted;
double r = Math.round(this.real * 100000) / 100000.0;
double i = Math.round(this.imaginary * 100000) / 100000.0;
if (i > 0)
formatted = r + "+" + i + "i";
else if (imaginary < 0)
formatted = r + "" + i + "i";
else {
formatted = Double.toString(r);
}
return "(" + formatted + ")";
}
public double getReal() {
return this.real;
}
public double getImaginary() {
return this.imaginary;
}
public void setReal(double newReal) {
this.real = newReal;
}
public void setImaginary(double newIm) {
this.imaginary = newIm;
}
}