// CSE 142, Spring 2010, Marty Stepp // This class defines a new type of objects called Point. // Each Point object represents a particular x/y coordinate position such as // (42, 173) and has behavior related to that position, such as being able // to compute its distance from other points, draw it on the screen, etc. import java.awt.*; public class Point { // fields private int x; private int y; // constructor // - runs when a client asks for a "new Point()" public Point(int startX, int startY) { // set up the initial values of the fields for 'this' current object x = startX; y = startY; } // read-only access to the fields public int getX() { return x; } public int getY() { return y; } // Returns the distance between this point and the given other point. public double distance(Point otherPoint) { int dx = otherPoint.x - x; int dy = otherPoint.y - y; double dist = Math.sqrt(dx*dx + dy*dy); return dist; } // Draws the current Point object on screen using the given graphical pen. public void draw(Graphics g) { g.fillOval(x, y, 3, 3); g.drawString("(" + x + ", " + y + ")", x, y); } // Returns a String representation of this point, // so that it can be printed by client code. public String toString() { return "(" + x + ", " + y + ")"; } // Shifts this point's x/y position by the given amounts. public void translate(int dx, int dy) { x = x + dx; y = y + dy; } }