0

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?

ps1nk4
  • 3
  • 2
  • 2
    You'll need to use `.equals(…)` for comparing with your `equals` function. Also note that the method signature of the overridden `.equals` should be different. It's a single method with an `Object` argument and a primitive `boolean` return. – qqilihq Oct 12 '21 at 11:16
  • your equals method is pointless. you need to override the equals method, not overload it – Stultuske Oct 12 '21 at 11:16
  • 1
    when comparing objects you can't use == you need to use .equals so a.equals(b) etc. however need to be careful that the instance that u are calling equals on isn't null. – George R Oct 12 '21 at 11:16
  • Hi, if you are on a recent version of Java, have you thought about simply using `record Complex(double re, double im) {}`? – Axel Oct 12 '21 at 12:50

3 Answers3

2

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);
}
Christopher
  • 411
  • 3
  • 7
  • You can omit the `if (c != null ) ...` in this case since `instanceof` will return false if null is passed. – Axel Oct 12 '21 at 12:46
1

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);
    }
}
MikeFHay
  • 8,562
  • 4
  • 31
  • 52
  • 1
    If you can use Java 16 or later, you can do `record Complex(double re, double im) {}`. –  Oct 12 '21 at 13:06
0

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?