0

I'm trying to implement an extension of the equals method in Java. Problem is, for one reason or the other it gives out the wrong response.

  public class Cordes{

  private static double x;
  private static double y;

  public Cordes(double x, double y) {
      this.x = x;
      this.y = y;
  }
  public boolean equals(Object somethingelse) {
      if (this == somethingelse) {
          return true;
      }   
      if (somethingelse instanceof Cordes) {
          Cordes theother = (Cordes) somethingelse;
          return (this.x == theother.x)  && (this.y == theother.y);
      }
          return false;
  }

public static void main(String[] args) {
    Cordes p = new Cordes(2.0, 4.0);
    Cordes p2 = new Cordes(3.0, 4.0);
    System.out.println(p.equals(p2));
}

So this comes out as true, even though it should obviously be false. I'm not sure what to try here, the first part of the code tries to match the memory address, which should be false. The other if statement tries to see if its a part of our class, if it is, it will try and see if these coordinates are equal which should return false.

In addition to this, I have a small side question: When we are casting Cordes on "somethingelse", what does this actually mean, isn't other object already an object, why do we have to cast it, and why does that action enable us to go "theother.x / theother.y".

Thanks in advance!

Eli S.
  • 146
  • 1
  • 1
  • 10
  • Time to learn debugging. – Jens Jan 26 '19 at 07:33
  • Your fields shouldn't be static. – Mark Rotteveel Jan 26 '19 at 07:45
  • As you have discovered, `static` is not your friend – MadProgrammer Jan 26 '19 at 07:45
  • @MadProgrammer Hah, I did. Btw, would love an answer to my side question ^^ – Eli S. Jan 26 '19 at 07:48
  • @EliS. Side question - yes `Cordes` is an `Object`, but since the only thing you've been provided is a reference to another `Object`, you need to determine if that object is an instance of `Cordes`, then you need to cast it so you can gain access to the properties. This is all part of polymorphism, where a object can appear as a number of different types of objects - for example, I might extend your `Cordes` to implement some of my own functionality, but I can still compare my instance of `Cordes` with yours, because my instance is a "type" of `Cordes` ... simple – MadProgrammer Jan 26 '19 at 08:43

5 Answers5

3

You are declaring the member variables as static:

  private static double x;
  private static double y;

This means they are shared by all instances of Cordes. When you construct the second one, it changes the values for both. If you remove static from these two lines, it should be fine.

Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • Hey Ryan, thanks for the answer. I was wondering if you had any idea about my side question regarding the use of the casting in "Cordes theother = (Cordes) somethingelse", how does this casting affect anything in this case? – Eli S. Jan 26 '19 at 07:55
  • @EliS casting is your way of telling the compiler that you know more than it does. Casting tells the compiler that you know for sure that it is an instance of `Cordes`, so it shouldn't stop you accessing the fields of a `Cordes`. – Andy Turner Jan 26 '19 at 08:00
  • Thanks Andy, but how does that affect my specific code in this case? – Eli S. Jan 26 '19 at 08:19
  • @EliS. try removing it. See what happens. – Andy Turner Jan 26 '19 at 08:21
1

Remove static from x and y. static variables are the same for all instances of these class.

private double x;
  private double y;
Jens
  • 67,715
  • 15
  • 98
  • 113
1

These two fields shouldn't be static,

private static double x;
private static double y;

Those should be instance variables, since you are creating different objects and comparing them. Remove the static modifier and it should work.

Sandeepa
  • 3,457
  • 5
  • 25
  • 41
1

By declaring x and y static :

private static double x;
private static double y;

you make it a class variable rather than an instance variable, which means all instances have the same x and y values.
That is why equals true for every instance of Cordes.
To fix remove the static modifier.

Edit: As for the side question: you are passing an argument that is of type Object to equals. You cast it to Cordes, meaning tell the compiler that is not only an Object but an object of type Cordes and can be treated like one.

c0der
  • 18,467
  • 6
  • 33
  • 65
1

You have declared x and y as static variables. And so when you create object p2 with x=3.0, it automatically changes the previous x value of object p.

So just remove static keyword from

private static double x;
private static double y;

You will get the desired result.

S.Mandal
  • 172
  • 1
  • 10