/* * Point.java * * A blueprint class representing a point on the Cartesian plane. */ public class Point { // the fields inside every Point object private int x; private int y; /* * constructor that takes values for both coordinates */ public Point(int initX, int initY) { this.x = initX; this.y = initY; } /* * getX - accessor method for this Point object's x coordinate */ public int getX() { return this.x; } /* * getY - accessor method for this Point object's y coordinate */ public int getY() { return this.y; } /* * setX - mutator method for this Point object's x coordinate */ public void setX(int x) { this.x = x; } /* * getY - mutator method for this Point object's y coordinate */ public void setY(int y) { this.y = y; } /* * equals - returns whether two Point objects are equal */ public boolean equals(Point other) { return (this.x == other.x && this.y == other.y); } /* * toString - returns a String representation of a Point object */ public String toString() { return "(" + this.x + ", " + this.y + ")"; } /* * distanceFromOrigin - an accessor method that returns the * distance of this Point from the origin */ public double distanceFromOrigin() { return Math.sqrt(this.x*this.x + this.y*this.y); } /* * quadrant - returns the number of the quadrant (if any) in which * this Point object falls, or 0 if it lies on an axis. */ public int quadrant() { if (this.x > 0 && this.y > 0) { return 1; } else if (this.x < 0 && this.y > 0) { return 2; } else if (this.x < 0 && this.y < 0) { return 3; } else if (this.x > 0 && this.y < 0) { return 4; } else { return 0; } } /* * closestToOrigin - a *static* method that takes two Point objects * and returns the one that is closest to the origin. * * We make it static because it does not need direct access to the * fields of either Point object, and there is not an obvious called object. */ public static Point closestToOrigin(Point p1, Point p2) { if (p1.distanceFromOrigin() < p2.distanceFromOrigin()) { return p1; } else { return p2; } } }