使用 HashSet 的复数

Complex numbers with using HashSet

学校给了任务:实现对象的加减乘除方法。从复数坐标创建一个维度为 n 的集合 (Hashset)。将它转移到 completes/multiplies 其元素的方法。 我自己实现了操作方法,但我无法理解为什么需要 hashset 以及如何在代码中实现它。看了文章,还是不明白如何应用class实现

抱歉我的英语不好

import java.lang.Math;

public class Complex {
    double dReal, dImaginary;

    // Constructor methods 

    public Complex() {}

    public Complex( double dReal, double dImaginary ) {
        this.dReal      = dReal;
        this.dImaginary = dImaginary; 
    }

    // Convert complex number to a string 

    public String toString() {
        if (dImaginary >= 0)
            return dReal + "+" +  dImaginary + "i";
        else
            return dReal + "-" + -dImaginary + "i";
    }


    // ================================================================  
    // Complex number arithmetic 


    // Compute sum of two complex numbers cA + cB
    public Complex Add(Complex cB ) {
        Complex sum = new Complex();

        sum.dReal      = dReal      + cB.dReal;
        sum.dImaginary = dImaginary + cB.dImaginary;

        return (sum);
    }


    // Compute difference of two complex numbers cA - cB
    public Complex Sub( Complex cB ) {
        Complex diff = new Complex();

        diff.dReal      = dReal      - cB.dReal;
        diff.dImaginary = dImaginary - cB.dImaginary;

        return (diff);
    }

    // Compute product of two complex numbers cA * cB

    public Complex Mult( Complex cB ) {
        Complex prod = new Complex();

        prod.dReal      = dReal*cB.dReal      - dImaginary*cB.dImaginary;
        prod.dImaginary = dImaginary*cB.dReal + dReal*cB.dImaginary;

        return (prod);
    }

    // Compute divisor of two complex numbers cA / cB

    public Complex Div( Complex cB ) {
        Complex div = new Complex();
        double dR, dDen;

        if(Math.abs( cB.dReal ) >= Math.abs( cB.dImaginary )) {
            dR   = cB.dImaginary/cB.dReal;
            dDen = cB.dReal + dR*cB.dImaginary;
            div.dReal      = (dReal      + dR*dImaginary)/dDen;
            div.dImaginary = (dImaginary - dR*dReal)/dDen;
        } else {
            dR   = cB.dReal/cB.dImaginary;
            dDen = cB.dImaginary + dR*cB.dReal;
            div.dReal      = (dR*dReal      + dImaginary)/dDen;
            div.dImaginary = (dR*dImaginary - dReal)/dDen;
        }

        return (div);
    }
    // ================================================================  


   
    // Exercise methods in Complex class

    public static void main (String args[]) {

        // Setup and print two complex numbers 

        Complex cA = new Complex( 1.0, 2.0 );
        Complex cB = new Complex( 3.0, 4.0 );

        System.out.println("cA = " + cA.toString() );
        System.out.println("cB = " + cB.toString() );

        // Output

        Complex cC = cA.Add( cB );
        System.out.println("Complex   cA + cB = " + cC.toString() );
        Complex cD = cA.Sub( cB );
        System.out.println("Complex   cA - cB = " + cD.toString() );
        Complex cE = cA.Mult( cB );
        System.out.println("Complex   cA * cB = " + cE.toString() );
        Complex cF = cA.Div( cB );
        System.out.println("Complex   cA / cB = " + cF.toString() );

    }
}

Create a set (Hashset) of dimension n from complex coordinates.

就是存储一组n个复数

Transfer it to a method that completes/multiplies its elements.

使用您拥有的 Mult 方法将所有 n 复数相乘。

Set<Complex> complexNumbers = new HashSet<>();
//add numbers to it

Optional<Complex> result = complexNumbers.stream()
   .reduce((c1, c2) -> c1.Mult(c2)); //or .reduce(Complex::Mult)

注意:Java 命名约定是以小写字母开头的命名方法(multmultiply 将是公平的名称)