Sign in to follow this  

comparing data

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my program I have class called Bridge, that takes two x,y coordinates and 'connects' them. How do I check if a bridge has already made between 2 coordinates? I was thinking that I could have accessors in the Bridge class that returned its 'from' coordinates and its 'to' coordinates. If the 'from' and 'to' coordinates of a given bridge "match" a given set of coordinates, then a bridge has been made at that location. The problem occurs in the "match" part: //******************************* public boolean isBridgeThere(double fromX, double fromY, double toX, double toY) { java.awt.Point from = new java.awt.Point(fromX, fromY); java.awt.Point to = new java.awt.Point(toX, toY); if (_bridge.getToCoords() = to && _bridge.getFromCoords() = from) { return true; } else { return false; } } //******************************** I see the problem, because the new instance of the point that I create will never equal the getToCoords and getFromCoords of the _bridge, right? So how do I make a more accurate comparison? I hope this all makes sense, thanks all. Gbizzle

Share this post


Link to post
Share on other sites
In Java, operator == compares for object identity. To test for object equivalence (i.e. do these two Points correspond to the same coordinate), use the '.equals()' method from java.lang.Object. Classes from the library implement this method in an appropriate way; for example, for Point it will compare the two coordinate values, and for String it will do string comparison. (Your own classes will inherit the default Object implementation unless you override it yourself, which means it will still compare object identity.)

This suggests a way to clean up your approach to the code, since those accessors are kind of ugly: instead of writing "isBridgeThere", implement the .equals() method (such that two Bridges are equal iff they have the same endpoints) and then compare the _bridge to a temporary new Bridge(from, to).

There is one caveat, Java's rule of two (contrast the rule of three in C++): If you implement .equals() you should also implement .hashcode(), and vice versa. The general contract is that "equal" objects have the same hash code; this makes sure that hashing containers like HashMap will work properly.

Share this post


Link to post
Share on other sites

This topic is 4745 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this