Point2Dp1=newPoint2D(x1, y1); Point2Dp2=newPoint2D(x2, y2); System.out.println("p1 is " + p1.toString()); System.out.println("p2 is " + p2.toString()); System.out.println("The distance between pi and p2 is" + p1.distance(p1); } }
/** * The <code>Point2D</code> class defines a point representing a location * in {@code (x,y)} coordinate space. * <p> * This class is only the abstract superclass for all objects that * store a 2D coordinate. * The actual storage representation of the coordinates is left to * the subclass. * * @author Jim Graham * @since 1.2 */ publicabstractclassPoint2DimplementsCloneable {
/** * The <code>Float</code> class defines a point specified in float * precision. * @since 1.2 */ publicstaticclassFloatextendsPoint2DimplementsSerializable { /** * The X coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ publicfloat x;
/** * The Y coordinate of this <code>Point2D</code>. * @since 1.2 * @serial */ publicfloat y;
/** * Constructs and initializes a <code>Point2D</code> with * coordinates (0, 0). * @since 1.2 */ publicFloat() { }
/** * Constructs and initializes a <code>Point2D</code> with * the specified coordinates. * * @param x the X coordinate of the newly * constructed <code>Point2D</code> * @param y the Y coordinate of the newly * constructed <code>Point2D</code> * @since 1.2 */ publicFloat(float x, float y) { this.x = x; this.y = y; }
Point2Dp1=newPoint2D.Double(x1,y1); Point2Dp2=newPoint2D.Double(x2, y2); System.out.println("p1 is " + p1.toString()); System.out.println("p2 is " + p2.toString()); System.out.println("The distance between pi and p2 is" + p1.distance(p1)); } }
运行结果如下
Enter point1’s x-, y-coordinates: 2.5 7 Enter point2’ s x-, y-coordinates:3.5 12 p1 is Point2D.Double[2.5, 7.0] p2 is Point2D.Double[3.5, 12.0] The distance between p1 and p2 is 5.0990195135927845