// 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 // We made the fields private so clients can't directly access them private int x; private int y; // A constructor that lets us set the x and y in one call // Our points are restricted from (0, 0) to (199, 199) public Point(int x, int y) { this.x = x % 200; this.y = y % 200; } // A zero-argument constructor that gives us a point at the origin public Point() { // This line calls the other constructor so we don't have redundant code this(0, 0); } // We added x and y accessors so that clients can get the x an y coordinates // but can't change the fields directly. If clients could access x and y // directly, they could break our (0, 0) to (199, 199) restriction. public int getX() { return x; } public int getY() { return y; } // 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 x, int y) { this.x = x % 200; this.y = y % 200; } // Translate this point by (dx, dy) public void translate(int dx, int dy) { x = x + dx; y = y + dy; x %= 200; y %= 200; } }