import java.awt.*; // I am creating a new type of objects, named Point. public class Point { private int x; // every Point has a variable inside of it, named x. private int y; // every Point has a variable inside of it, named y. // I'm a Point object, being freshly created // and the user wants me to have the given x/y values public Point(int x, int y) { // this.x = x; // this.y = y; this.setLocation(x, y); } public Point() { // this.x = 0; // this.y = 0; // this.setLocation(0, 0); // call the (x, y) constructor with 0 for the parameter values this(0, 0); } // providing encapsulation to our Point objects // outside classes can read x,y values, but not directly change them public int getX() { return this.x; } public int getY() { return this.y; } public void draw(Graphics g) { g.drawRect(this.x, this.y, 2, 2); g.drawString(this.toString(), this.x, this.y); } // double distance = p1.distance(p2); public double distance(Point otherPoint) { // I'm a Point, I have an x, and a y. // You're asking me, how far apart am I from this other Point b, // who also has an x and a y? int dx = this.x - otherPoint.x; int dy = this.y - otherPoint.y; double distance = Math.sqrt(dx*dx + dy*dy); return distance; } public double distanceFromOrigin() { // double result = this.distance(this); // return result; Point origin = new Point(0, 0); return this.distance(origin); // return this.distance(new Point(0, 0)); } public boolean equals(Object o) { /* if (this.x == other.x && this.y == other.y) { return true; } else { return false; } if (!(o instanceof Point)) { return false; } */ Point other = (Point) o; return (this.x == other.x && this.y == other.y); } public void setLocation(int x, int y) { this.x = Math.max(0, x); this.y = Math.max(0, y); // new Point(x, y) } // so that Java can println a Point object public String toString() { String result = "(" + this.x + ", " + this.y + ")"; return result; } public void translate(int changeInX, int changeInY) { // I'm a Point, and they want to change my x/y by the given amounts. this.x += changeInX; this.y += changeInY; } }