My personal preference would be:
1 2 3 4 5 6
| public boolean hitTarget (int x, int y) { return x >= this.boxX && x < this.boxX +this.boxW && y >= this.boxY && y < this.boxY + this.boxH; } |
No nasty parentheses, and (assuming no runtime compiler optimisation) more efficient too.
More importantly though, i've corrected your boundary logic.
By definition ObjectA.X+ObjectA.Width is the first coordinate falling
outside ObjectA.
Therefore your right edge and bottom edge comparison operators should be "<" not "<=".
To be honest if this problem is anything more than a learning excercise you are already doing it wrong, as you are reinventing the wheel.
Use java.awt.Rectangle (abstracted through either java.awt.Rectangle2D or java.awt.Shape)