// CSE 142 Lecture 18 // PrintStreams, Objects // This class represents a Point object. import java.awt.*; public class Point { // Each Point stores its x and y position in fields int x; int y; // A constructor that lets us set the x and y in one call public Point(int initX, int initY) { x = initX; y = initY; } // A zero-argument constructor that gives us a point at the origin public Point() { x = 0; y = 0; } // Draws this point using the passed Graphics object public void draw(Graphics g) { g.fillOval(x, y, 3, 3); g.drawString(toString(), x, y); } // Prints the coordinates of this Point to System.out public String toString() { return "(" + x + ", " + y + ")"; } // Find the distance from this point to the passed Point public double distance(Point p) { return Math.sqrt(Math.pow(x - p.x, 2) + Math.pow(y - p.y, 2)); } // Set the location of this point public void setLocation(int newX, int newY) { x = newX; y = newY; } // Translate this point by (dx, dy) public void translate(int dx, int dy) { x = x + dx; y = y + dy; } }