/********************************************************** Main Class File: PointMaker.java This file: Point.java Author: CS302 Section 5 Date: 9-24-03 **********************************************************/ /** * The Point class is a wrapper for (x,y) coordinates. */ class Point { private int x; // x-coordinate private int y; // y-coordinate /** * The constructor sets initial values for (x,y). * @param xVal The value of the x-coordinate. * @param yVal The value of the y-coordinate. */ public Point(int xVal, int yVal) { x = xVal; y = yVal; } /** * Accessor for the x-coordinate. * @return The x-coordinate. */ public int getX() { return x; } /** * Accessor for the y-coordinate. * @return The y-coordinate. */ public int getY() { return y; } /** * Switches the x and y coordinates. */ public void invert() { int temp = x; x = y; y = temp; } /** * Adds another Point to this one. * @param p The other Point. */ public void add(Point p) { x = x + p.x; y = y + p.y; } /** * Computes distance from another Point to this one. * @param p The other Point. */ public double getDistanceFrom(Point p) { int dx = x – p.x; // distance along x-axis int dy = y – p.y; // distance along y-axis // This is the distance formula. We are casting // (dx*dx + dy*dy) into a double because the sqrt // method requires a parameter of type double. // Since sqrt returns a double, the return type // of this whole method is also double. return Math.sqrt( (double) dx*dx + dy*dy); } /** * Returns a new Point, the inverse of this one. * @return The inverse Point. */ public Point getInvert() { Point invert = new Point(y,x); return invert; } /** * Returns a new Point, halfway between another * Point and this one. * @param p The other point. * @return The halfway Point. */ public Point getHalfwayPoint() { int newX = (x + p.x) / 2; int newY = (y + p.y) / 2; return new Point(newX, newY); } }