Jump to content
  • Advertisement
Sign in to follow this  
gbizzle

comparing data

This topic is 5089 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
Advertisement
the answer that jumps out at me is to give each point a coresponding bool variable and when you run the connect funciton change the 2 sets of points to which the line is being draw to true

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!