2

I'm trying to form a method to call on a object to check if it contains some of the attributes in the class

This is the code that I've tried so far

public class BoundingBox {

    public int x, y, width, height;

    public BoundingBox() {

    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer Y) {
        this.y = y;
    }

    public boolean contains(int x, int y) {
        if (x == getX() & y == getY()) {
            return true;
        } else {
            return false;
        }
    }
}

However when I created the object with all attributes holding the value of 10 and tested it using object.contains(15,15), it does not return false

Louis
  • 3,592
  • 2
  • 10
  • 18
hh_vv
  • 33
  • 4

4 Answers4

2

In Java the operator & can assume different meanings:

  • & : Bitwise operator
  • && : Logical operator

In the if statement if (x == getX() & y == getY()) { you used the & bitwise operator instead of the && logical operator.
Plus there was a typo in the setY method and, as stated by @alvinalvord, comparing int with Integer can give unexpected results.

Change your code like this and it will work:

public class BoundingBox {

    public int x, y, width, height;

    public BoundingBox() {

    }

    public Integer getX() {
        return x;
    }

    public void setX(Integer x) {
        this.x = x;
    }

    public Integer getY() {
        return y;
    }

    public void setY(Integer y) {
        this.y = y;
    }

    public boolean contains(int x, int y) {
        return getX().equals(x) && getY().equals(y);
    }
}

Alternative Code

Or, if there's no particular reason to keep the Integer variables, you can change the code like this:

public class BoundingBox {

    public int x, y, width, height;

    public BoundingBox(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean contains(int x, int y) {
        return getX() == x && getY() == y;
    }
}
Louis
  • 3,592
  • 2
  • 10
  • 18
  • Why are there Integer arguments in the methods instead of just int, if the fileds are declared as int? – Pavel Smirnov Mar 23 '19 at 15:54
  • Well, he didn't speciefied if they're there for a reason or not, so I kept them. But for me personally, I don't think they're needed. – Louis Mar 23 '19 at 16:03
  • I've changed my code to this but I still get a false result on the method even when I input the correct x and y values. It only returns true for 0? – hh_vv Mar 24 '19 at 11:03
  • 1
    @hh_vv My guess is that `x` and `y` are not initialized correctly in the code. I've updated the alternate code. I've personally tested it and it works. Let me know if it solve the issue for you. – Louis Mar 24 '19 at 13:01
1

Comparing int and Integer also has some problems see here. Best return int for your getters.

alvinalvord
  • 394
  • 2
  • 11
-1

Your of test in contains is missing an ampersand, it should be && instead of &. Single ampersand is bitwise AND, not logical AND.

Joe
  • 239
  • 3
  • 8
-1

Your setY method has a bug:

 public void setY(Integer Y) {
        this.y = y;
 }

Y is capital, that's why this.y = y does not work. This line of code makes no sense, that's why you get false when youc compare y in contains() method. Change it to:

 public void setY(Integer y) {
            this.y = y;
 }
Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
  • This doesn’t explain why his intended test returns true. – Joe Mar 23 '19 at 14:57
  • @Joe, his test always returns false, not true, (only if y is equal to 0 and this.x = x ); Bitwise & operator works in this case as the author expects. – Pavel Smirnov Mar 23 '19 at 15:11