Equals() not working

Hi this is what my PieceComponent visualisation looks like while I have no equals method overridden,
I don’t see mistake in it.
Screenshot 2022-06-30 175657

but once I added my equals method things doesn’t make sense and I can’t find the error.
Screenshot 2022-06-30 175759

I wish I could show my equals() code to only tutors isn’t there a way to show code without others seeing it?

I think you also have to edit the “hashCode()” method of your Piece implementation.
Because there is this relation:

A a = new A();
A b = new A();

a.equals(b) => (a.hashCode() == b.hashCode())

Also have you checked if your equals() method is working in all possible cases?


it seems that there is some bug in the overriding implementation of equal method. Make sure that you have relized the deep equality. For example, you should compare the row / column coordinates of the rotation point and not the point object (the latter is just a reference to the point object). Moreover, you do not really need to override the method hashCode() to get the correct visualization of PieceComponents.

1 Like

i am comparing rows and columns of rotation points and i can’t find the mistake in my equals that’s why i’m confused.

In which way are you comparing the bodies of the pieces ?

Did you try to debug using PieceComponents?

i did Array.deepequals function

i did not , i will try thanks

There are several things wrong with your project.

As a general rule, if at any time in this project, you are doing a switch-case over the piece type, you are doing something wrong. You will almost always get a shorter, more correct, faster to write, easier to debug implementation by not doing this. For example, your getCounterClockwiseRotation method is about 200 lines. Instead of manually typing in all the rotation points, think about how you would compute the rotation point for a rotated piece. This would also spare you from accidentally typing the wrong numbers in some places, which you did several times (I think – I did not check them all).

Similarly, you do not need to track the rotation explicitly. There simply is no need for this.

Further, your equals method also has some typos. The proper recommendation is to use a proper :tm: IDE that can auto-generate your equals() method, like eclipse or IntelliJ. VSCode also has a plugin that does this, however you will most likely not be able to install it in the VM, since Microsoft hates you.¹

Instead, have a look at the equals method already given to you (e.g. in Point), and more or less copy it. In particular, you should not

  • simply cast the given Object to Piece
  • use == or != to compare non-primitive objects

Once you do all of this, your equals seems to work (mostly, I can not guarantee correctness).

Hope this helps,

1) The background is that while VSCode is open source, M$ does not allow you to use their marketplace, which contains all the cool plugins, for reasons only Microsoft can tell. If you install the version distributed by Microsoft, you can. However, they put a license on their version which says that others (i.e. the Prog2 team) are not allowed to redistribute it, which means that we can not provide you a VM where that version is installed. So we stick to the version build from the open source, but this then does not have access to the Microsoft plugin market place, which means that many plugins are not available.


Hi , thanks for all this, i am not sure when did i last push so i don’t know exactly what are you looking at, if my piececomponents visualisation is correct doesn’t that means that i have all rotation points correct? (with my switch cases), so maybe in that push i had it wrong . anyways i will try to do as i said in equals thanks