# Why does this equal that?

• November 27th, 2011, 04:43 PM
colerelm
Why does this equal that?
Why is it that this method returns true? I'm supposed to write an equals method that returns true if both rectangles are similar (My assumption of a "similar rectangle" is one which has the same ratio of height:length as the other rectangle).

Code :

```public class Rectangle {   public int width; public int length;   public Rectangle(int width, int length){ this.width = width; this.length = length; } public static void main(String[] args) { Rectangle r = new Rectangle(2,4); Rectangle s = new Rectangle(6,13); boolean n = s.equals(r); System.out.println(n); }   public boolean equals(Rectangle rectangle){ if((this.width / this.length) == (rectangle.width / rectangle.length)) return true; else return false; }```

Shouldnt s.equals(r) return false?
• November 27th, 2011, 04:56 PM
copeg
Re: Why does this equal that?
Add some System.out.println in your equals method to check what the values of each division calculation. This will give you an indication as to why this is the case - and why are 2 rectangles equal when their width/length ratio are equal? Does that mean 6x1 is equal to 12x2?
• November 27th, 2011, 05:20 PM
helloworld922
Re: Why does this equal that?
In addition to Copeg's answer, you're doing integer division, which truncates to the next smallest integer (1 / 2 = 0, 4 / 3 = 1, etc.). That's why it's returning true for a 2x4 and a 6x13 rectangle.