Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Paul Clapham
• Bear Bibeault
• Jeanne Boyarsky
Sheriffs:
• Ron McLeod
• Tim Cooke
• Devaka Cooray
Saloon Keepers:
• Tim Moores
• Tim Holloway
• Jj Roberts
• Stephan van Hulst
• Carey Brown
Bartenders:
• salvin francis
• Scott Selikoff
• fred rosenberger

# Check if one rectangle is completely inside the other Rectangle

Greenhorn
Posts: 15
So Both rectangles are formed by this test

There is aditional class called Point that is used in rectangle generation, it works completly fine, and i am adding it just for clarity
package net.thumbtack.school.figures.v1;

And there is main class

Method that i need should start like this: public boolean isInside(Rectangle rectangle)
I already have a simmilar class that tested if two rectangles Overlap in any part
return true;
}
I think they should be pretty simmilar but i dont know how to modify it so it only retirn true when one rectangle completly inside the other.

Bartender
Posts: 1200
22

I think they should be pretty simmilar but i dont know how to modify it so it only retirn true when one rectangle completly inside the other.

Try looking at just the x dimension first.  If one rectangle is completely inside another, what does that imply for the x values?

On a side note...
If 2 rectangles share an edge, does that count as inside.  e.g. Is new Rectangle(12, 20, 2, 10) "inside" new Rectangle(20, 20, 10, 15)?  Is a Rectangle "inside" itself?

Ryan McGuire
Bartender
Posts: 1200
22

I'm not sure I'd make Point mutatable.  Since you reimplemented hashCode() and equals(), I suspect that you're going to use the hashCode for something - perhaps storing Point objects in a Collection that uses the hashCode in some way.  For example, let's say you instantiate a new Point(3,4) and store it in a HashSet.  If you then call moveTo(5,6) on that Point to modify the x and y values, and then search for a new Point(5,6) in the HashSet, you won't find it because it'll have a different hashCode() value than when it was inserted.

I'm not saying making Point mutatable is necessarily "wrong" - just that you have to be careful.

Ryan McGuire
Bartender
Posts: 1200
22