import java.awt.*; // a simple class for representing points on an integer grid public class Point { // fields (best to be private, but that's next lecture) int x; int y; // constructor for points at the origin public Point() { x = 0; y = 0; } // constructor that takes initial position as arguments public Point(int initialX, int initialY) { x = initialX; // alternately could call setLocation(initialX,initialY) y = initialY; } public int getX() { return x; } public int getY() { return y; } public void setLocation(int newX, int newY) { x = newX; y = newY; } // "object content" -- relative to the object it's called upon // move the object a certain amount public void translate(int dx, int dy) { x += dx; y += dy; } // tells me the distance between 'this' point and the given other point p2 public double distance(Point p2) { int dx = x - p2.x; int dy = y - p2.y; return Math.sqrt(dx*dx + dy*dy); } // this object's distance to the orighm public double distanceToOrigin() { return distance(new Point(0,0)); } // Returns a text representation of this Point object. // e.g. "(5, -2)" public String toString() { return "(" + x + ", " + y + ")"; } // put a description of the point in the right place on a canvas public void draw(Graphics g) { g.fillOval(x, y, 2, 2); g.drawString(toString(), x, y); } }