Search code examples
javahashsetcomplex-numbers

Complex numbers with using HashSet


The university has given the task: To implement methods of adding, subtracting, multiplying and dividing objects. Create a set (Hashset) of dimension n from complex coordinates. Transfer it to a method that completes/multiplies its elements. I realize the methods of operations themselves, but I cannot understand why hashset is needed and how to implement it in code. I read the articles, but I still didn’t understand how to apply the class for realization.

Sorry for my broken english

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() );

    }
}


Solution

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

    It is just to store a set of n complex numbers.

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

    Multiply all the n complex numbers using the Mult method you have.

    Set<Complex> complexNumbers = new HashSet<>();
    //add numbers to it
    
    Optional<Complex> result = complexNumbers.stream()
       .reduce((c1, c2) -> c1.Mult(c2)); //or .reduce(Complex::Mult)
    

    Note: Java naming convention is to name methods start with lower case (mult or multiply would be fair names)