0

Java の 2 つの複素数を加算するために、この方程式を設定する方法を理解しようとしています。2 つの方法がありますが、最初の方法が何を求めているのか正確にはわかりません。(real1 + real2) の代わりに (real1 + imag1) を実行する必要があると言っていますか? その場合、どうすればその結果をc1に入れることができますか? また、合計を保持して返す 2 番目のメソッドの結果にも問題があります。

    /*Method for adding the real and imaginary parts of two complex numbers,
 *which returns the result in a new complex number
 */
public static ComplexNumber addComplexNumbers(double real1, double imag1, double real2, double imag2){ 
    ComplexNumber result = ComplexNumber.addComplexNumbers(real1, imag1, real2, imag2);
    result.setReal(real1 + real2);
    result.setImag(imag1 + imag2);
    return result;
    }

//Method for adding two complex numbers
public static ComplexNumber addComplexNumbers(ComplexNumber c1, ComplexNumber c2){ 
    ComplexNumber result = new ComplexNumber();
    result = (c1.real + c2.real) + (c1.imag + c2.imag);

}
4

3 に答える 3

5
final public class Complex {

    private final double real; 
    private final double imag; 

    public Complex() {
        this(0.0, 0.0);
    }

    public Complex(double r) {
        this(r, 0.0);
    }

    public Complex(double r, double i) {
         this.real = r;
         this.imag = i;
    }

    public Complex add(Complex addend) {
        return new Complex((this.real + addend.real), (this.imag + addend.imag));
    }
}
于 2012-06-01T00:59:41.193 に答える