public class Point { /* insert your name here */ /* * The Point class is used to identify a point in 2D space. * A point has a X coordinate and a Y coordinate. */ private double x; private double y; private final double epsilon = 10e-6; /** * * pre: none * post: the x value of the point is returned */ public double getx(){ return x; } /** * * pre: none * post: the x value of the point is set, * the return value is set to true if the value has been updated */ public boolean setx(double x){ this.x = x; return true; } /** * *pre: None *post: The Y value of a point is returned */ public double gety(){ return y; } /** * *pre: None *post: The Y value of a point is set * The return value is true if the value is set successfully */ public boolean sety(double y){ this.y = y; return true; } /** * pre: x and y set to valid points on the x and y axis * post: a Point is created with the x and y values set to * the incoming parameters. * * For this constructor, "this.x" refers to the instance variable of the object * needed here to disambiguate from the parameter "x" */ public Point(double x, double y){ setx(x); sety(y); } /** * pre: none * post: Point created at the origin * * * origin created by Point(), use this to call the general Point constructor */ public Point(){ this(0.0,0.0); } /** * pre: none * post: String in the form (x,y) is returned * * A point object is represented by a string in the following format: * (x,y). A point whose x value is 2.0 and y value is -1.0 would be represented * by the following: * (2.0,-1.0) * */ public String toString(){ return "(" +x+ "," +y+ ")"; } /** * pre: p is set to a valid point * post: true or false returned, true if the x and y values of this point * are within epsilon of the x and y values of the argument * * This is test for equality using epsilon, because we * are dealing with doubles,so roundoff can occur */ public boolean equals (Point p){ return (Math.abs(x - p.getx()) < epsilon) && (Math.abs(y - p.gety()) < epsilon); } /** * * pre: a valid Point * post: The Euclidean distance from this point to the argument point * p is returned * * Euclidean distance of this point to point p is defined as the square root * of the sum of the distances squared. Calculate the distance of (x-x') and * (y-y'), square each and add them together. The Euclidean distance is * the square root of this value. */ public double euclidDist(Point p){ double xDiff = getx() - p.getx(); double yDiff = gety() - p.gety(); return Math.sqrt(xDiff*xDiff + yDiff*yDiff); } /** * @param args: no args */ public static void main(String[] args) { // TODO test harness for Point // test all methods Point origin = new Point(); Point p1 = new Point(0.0,4.0); Point p2 = new Point(3.0000001,3.9999999); Point p3 = new Point(3.0,4.0); System.out.println("origin: " + origin); System.out.println("p1: " + p1); System.out.println("p2: " + p2); System.out.println("p3: " + p3); if(p2.equals(p3)) System.out.println(p2 + " equals " + p3); else System.out.println(p2 + " does not equal " + p3); System.out.println("Euclidean distance between " + origin + " and " + p1 +": " + origin.euclidDist(p1)); System.out.println("Euclidean distance between " + p1 + " and " + p3 +": " + p1.euclidDist(p3)); System.out.println("Euclidean distance between " + p3 + " and " + origin +": " + p3.euclidDist(origin)); } }