Home > Mobile >  How do I define an equals function for a custom complex numbers class in Java?
How do I define an equals function for a custom complex numbers class in Java?

Time:10-12

Here's a piece of code from my complex numbers class:

public class complex {
    private double re;
    private double im;

    public complex(double real, double imag) {
        re = real;
        im = imag;
    }

    public Boolean equals(complex b) {
        complex a = this;
        return a.re==b.re && a.im==b.im;
    }

    public Boolean equals(double alpha){
        complex a = this;
        return a.re==alpha && a.im==0;
    }
    ...
}

And here's what I'm trying to use it for:

public class cubic_cardano {
    public static void solveCubic(complex a, complex b, complex c, complex d){
        if (a==b && b==c && c==d && d==0) {
            ...
        }
    }
    ...
}

Comparing a complex number to a complex number works just fine while comparing a complex number to a double gives an error:

Incompatible operand types complex and int

What could be the reason and how can I make it work?

CodePudding user response:

You need to define the signature so it properly overloads the Object methods equals, and hashCode. Also, your classname "complex" should be capitalized as "Complex".

Your implementation defines equals only for parameters of type "complex" but it needs to accept Object parameters. This is a bit confusing but the way Java is defined you need to do it this way.

It is also very important to override the hashCode method in a way that is compatible with your equals method. Otherwise, things that depend on equals, like Map, won't work correctly.

@Override
public boolean equals (Object c)
{
    if (c != null)
    {
        if (c instanceof complex)
        {
            final complex cc = (complex)c;
            return cc.re == re && cc.im == im;
        }
    }
    return false;
}

@Override
public int hashCode ()
{
    return Double.hashCode (re)   Double.hashCode(im);
}

CodePudding user response:

You cannot use equals or == to compare two objects with unrelated types. Instead you should compare to a Complex with value zero:

    public static void solveCubic(Complex a, Complex b, Complex c, Complex d){
        if (a.equals(b) && b.equals(c) && c.equals(d) && d.equals(new Complex(0, 0))) {
            //...
        }
    }

p.s. there are several other issues with your Complex class, as others have noted in the comments. Here is the version I am using:

import java.util.Objects;

public class Complex {
    private double re;
    private double im;

    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Complex other = (Complex) o;
        return re == other.re && im == other.im;
    }

    @Override
    public int hashCode() {
        return Objects.hash(re, im);
    }
}

CodePudding user response:

Basicly: no, you can't. Java does not support "operator overloading", which is changing how operators work for your own classes. You will just have to go with complexA.equals(complexB);

about operator overloading Why doesn't Java offer operator overloading?

  • Related