How do you test if two points are equivalent? (i.e., that they indicate the same location in the 2D plane?)
The x
value and the y
value of each point is the same.
equals()
Method
The equals()
method is defined for class Point to perform this test:
pointA.equals( pointB ) ----- returns true if the two points contain equivalent data
Here is an example program showing this:
import java.awt.*;
class EqualsDemo
{
public static void main ( String arg[] )
{
Point pointA = new Point( 7, 99 ); // first Point
Point pointB = new Point( 7, 99 ); // second Point with equivalent data
if ( pointA.equals( pointB ) )
System.out.println( "The two objects contain the same data: " + pointA );
else
System.out.println( "The two objects are not equivalent: " + pointA +
" differs from" + pointB);
}
}
|