Java创建一个复数类创建一个执行复数运算的Complex类.复数具有如下格式:realPart+imaginary*j

1个回答

  • package table;

    public class Complex

    {

    x09double real;

    x09double imaginary;

    x09public static final Complex ZERO = new Complex (0, 0);

    x09public static final Complex ONE = new Complex (1, 0);

    x09public static final Complex I = new Complex (0, 1);

    x09public Complex ( double real, double imaginary )

    x09{

    x09x09this.real = real;

    x09x09this.imaginary = imaginary;

    x09}

    x09public double magnitude ()

    x09{

    x09x09return Math.sqrt (this.real * this.real + this.imaginary * this.imaginary);

    x09}

    x09public Complex negative ()

    x09{

    x09x09return new Complex (-real, -imaginary);

    x09}

    x09public double valueOf ()

    x09{

    x09x09return this.real;

    x09}

    x09public Complex add ( Complex a, Complex b )

    x09{

    x09x09return new Complex (a.real + b.real, a.imaginary + b.imaginary);

    x09}

    x09public Complex subtract ( Complex a, Complex b )

    x09{

    x09x09return new Complex (a.real - b.real, a.imaginary - b.imaginary);

    x09}

    x09public Complex multiply ( Complex a, Complex b )

    x09{

    x09x09return new Complex (a.real * b.real - a.imaginary * b.imaginary, a.real * b.imaginary + a.imaginary * b.real);

    x09}

    x09@Override

    x09public String toString ()

    x09{

    x09x09StringBuilder builder = new StringBuilder ();

    x09x09builder.append ("Complex [real=").append (real).append (", imaginary=").append (imaginary).append ("]");

    x09x09return builder.toString ();

    x09}

    }