public class Complex {
private float real; //實部
private float imagin;//虛部
public Complex(){//無參默認為(0, 2)
this.real = 0F;
this.imagin = 2F;
}
public String toString(){//以a+bi的形式輸出的復數
return real + "+" + imagin + "i";
}
// a+ bi + (c+ di) = (a+c) + (b+d)i
public static Complex add(Complex c1, Complex c2){//兩個復數相加
Complex complex = new Complex();
complex.setReal(c1.getReal() + c2.getReal());
complex.setImagin(c1.getImagin() + c2.getImagin());
return complex;
}
// a+ bi ==? c + di----> a==c, b==d --> it's true
public static boolean equal(Complex c1, Complex c2){//比較兩個復數是否相等
return c1.getReal() == c2.getReal() && c1.getImagin() == c2.getImagin();
}
//a + bi + f = (a+f) + bi
public static Complex addFloat(Complex c1, float fValue){//復數加壹浮點數
c1.setReal(c1.getReal() + fValue);
return c1;
}
public float getImagin() {
return imagin;
}
public void setImagin(float imagin) {
this.imagin = imagin;
}
public float getReal() {
return real;
}
public void setReal(float real) {
this.real = real;
}
}