public class Point2{ private int a; private int b; private Point startingPoint; public Point2(int a, int b, Point p){ this.a = a; this.b = b; startingPoint = p; } // Computes the distance from starting point to this public double measureDistance(){//it takes no parameter. return startingPoint.distanceTo(a, b); } /* if you can't edit distanceTo() it gets a little verbose but you must create a Point with Point2 coordinates - remember this example when you will study Inheritance public double measureDistance() { Point endingPoint = new Point(a, b); return startingPoint.distanceTo(endingPoint); } */ }
/** * Validates this Bounds and returns true if the distance between the two points is bigger than 0.0 * and at least one of the x and y values is different than 0.0. */ public boolean isValid() { // upperLeft and lowerRight must exist if (upperLeft == null || lowerRight == null) { return false; } // either of the x values must be different than 0.0 if (upperLeft.x == 0.0 && lowerRight.x == 0.0) { return false; } // either of the y values must be different than 0.0 if (upperLeft.y == 0.0 && lowerRight.y == 0.0) { return false; } // the distance between the two points must be bigger than 0.0 if (upperLeft.distanceTo(lowerRight) == 0.0) { return false; } return true; }
/** * Validates this Bounds and returns true if the distance between the two points is bigger than 0.0 * and at least one of the x and y values is different than 0.0. */ public boolean isValid() { // upperLeft and lowerRight must exist if (upperLeft == null || lowerRight == null) { return false; } // either of the x values must be different than 0.0 if (upperLeft.x == 0.0 && lowerRight.x == 0.0) { return false; } // either of the y values must be different than 0.0 if (upperLeft.y == 0.0 && lowerRight.y == 0.0) { return false; } // the distance between the two points must be bigger than 0.0 if (upperLeft.distanceTo(lowerRight) == 0.0) { return false; } return true; }
@Test public void testDistanceTo() { Point p1 = Point.of(10, 10); Point p2 = Point.of(13, 14); double distance = p1.distanceTo(p2); assertEquals(5.0, distance, 0.0); distance = p2.distanceTo(p1); assertEquals(5.0, distance, 0.0); distance = p1.distanceTo(p1); assertEquals(0.0, distance, 0.0); p1 = Point.of(-10, 10); p2 = Point.of(-13, 14); distance = p1.distanceTo(p2); assertEquals(5.0, distance, 0.0); } }