核心代码
public class Complex {
private double real;
private double vir;
//复数的除法,c = a/b
public static Complex divide(Complex one, Complex two) throws DividedByZeroException {
return multiplication(one, reciprocal(two));
}
//复数的除法,a /= b
public Complex divide(Complex one) throws DividedByZeroException {
return multiplication(reciprocal(one));
}
//取倒数运算
private static Complex reciprocal(Complex one) throws DividedByZeroException {
if(one == null) {
return null;
}
double mode = one.real*one.real + one.vir*one.vir;
if(mode <= 1e-6) {
throw new DividedByZeroException("除0错!!!");
}
return new Complex(one.real / mode, -one.vir / mode);
}
//复数的乘法,c = a*b
public static Complex multiplication(Complex one, Complex two) {
return new Complex(one).multiplication(two);
}
//复数的乘法,a *= b
public Complex multiplication(Complex one) {
setComplex(this.real * one.real - this.vir * one.vir,
this.vir * one.real + this.real * one.vir);
return this;
}
//复数的减法,c = a-b
public static Complex subtraction(Complex one, Complex two) {
return (new Complex(one)).subtraction(two);
}
//复数的减法,a -= b
public Complex subtraction(Complex one) {
return add(opposite(one));
}
//求相反数运算
private static Complex opposite(Complex one) {
return new Complex(-one.real, -one.vir);
}
//复数的加法,c = a+b
public static Complex add(Complex one, Complex two) {
return (new Complex(one)).add(two);
}
//复数的加法,a += b
public Complex add(Complex one) {
setComplex(this.real + one.real, this.vir + one.vir);
return this;
}
public void setComplex(Complex one) {
setComplex(one.real, one.vir);
}
public void setComplex(double one) {
setComplex(one, 0.0);
}
public void setComplex(double one, double two) {
setReal(one);
setVir(two);
}
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getVir() {
return vir;
}
public void setVir(double vir) {
this.vir = vir;
}
//重写的equals()方法
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (obj instanceof Complex) {
Complex one = (Complex) obj;
if (Math.abs(this.real - one.real) <= 1e-6 && Math.abs(this.vir - one.vir) <= 1e-6) {
return true;
}
}
return false;
}
//重写的toString()方法
@Override
public String toString() {
return "(" + real + "," + vir + ")";
}
//四个构造方法
public Complex() {
this(0.0, 0.0);
}
public Complex(Complex o) {
this(o.getReal(), o.getVir());
}
public Complex(double real) {
this(real, 0.0);
}
public Complex(double real, double vir) {
setReal(real);
setVir(vir);
}
}
除法中的除零错抛异常代码
public class DividedByZeroException extends Exception {
private static final long serialVersionUID = -1917368295506021042L;
public DividedByZeroException() {
}
public DividedByZeroException(String message) {
super(message);
}
public DividedByZeroException(Throwable cause) {
super(cause);
}
public DividedByZeroException(String message, Throwable cause) {
super(message, cause);
}
public DividedByZeroException(String message, Throwable cause, boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
测试代码
public class Test {
public static void main(String[] args) throws DividedByZeroException {
System.out.println("调用无参构造方法");
Complex complex1 = new Complex();
complex1.setReal(1.0);
complex1.setVir(1.0);
System.out.println(complex1);
System.out.println(complex1.equals(complex1));
System.out.println("调用单参构造方法");
Complex complex2 = new Complex(2.2);
complex2.setVir(2.2);
System.out.println(complex2.toString());
System.out.println(complex2.equals(complex1));
System.out.println("调用双参构造方法");
Complex complex3 = new Complex(3.3, 3.3);
System.out.println(complex3.toString());
System.out.println(complex3.equals(complex3));
System.out.println("调用类参数构造方法");
Complex complex4 = new Complex(complex2);
System.out.println(complex4.toString());
System.out.println(complex4.equals(complex2));
System.out.println("加法,一个参数");
System.out.println(complex1.add(complex2));
System.out.println("加法,两个参数");
Complex complex5;
complex5 = Complex.add(complex3, complex4); //add应为静态方法
System.out.println(complex5);
System.out.println("减法,一个参数");
Complex c1 = new Complex(6.0, 4.1);
Complex c2 = new Complex(4.0, 5.1);
System.out.println(c1.subtraction(c2));
System.out.println("减法,两个参数");
Complex c3 = new Complex(7.0, 7.1);
Complex c4 = new Complex(4.0, 5.1);
System.out.println(Complex.subtraction(c3, c4));
System.out.println("乘法,一个参数");
Complex c5 = new Complex(2.0, 3.0);
Complex c6 = new Complex(3.0, 4.0);
System.out.println(c5.multiplication(c6));
System.out.println("乘法,两个参数");
System.out.println(Complex.multiplication(c5, c6));
System.out.println("除法,一个参数");
Complex c7 = new Complex(7.0, 1.0);
Complex c8 = new Complex(3.0, 4.0);
System.out.println(c7.divide(c8));
System.out.println("除法,两个参数");
Complex c77 = new Complex(7.0, 1.0);
Complex c88 = new Complex(0.0, 0.0);
System.out.println(Complex.divide(c77, c88));
}
}