// This class defines a blueprint for Point objects. // Points have an x and y value and have methods for // translating, finding the distance to the origin and // converting to Strings. public class Point { //fields: data / information // every Point object will have an x and y value int x; int y; //Constructors: These are called when a client // makes a new Point() call // constructor that takes an x and y parameter public Point(int newX, int newY){ //Note that the passed in parameters were named differently // from the fields so Java wouldn't get confused. "x" is short // for "this.x". x = newX; y = newY; } // constructor that doesn't take any parameters // but just sets the default values public Point(){ this.x = 0; this.y = 0; } //instance methods: behavoir / actions // every Point object can perform these methods // Translates this point by the given dx and dy public void translate(int dx, int dy) { // "this" refers to the object that translate was called on this.x += dx; this.y += dy; } // Returns this Point's distance from origin public double distanceFromOrigin() { // Note that in this case, "x" is just shorthand for "this.x" return Math.sqrt(x * x + y * y); } // Returns the String representation of this Point. // Since this is "public String toString()" Java will // use it whenever a Point object is used as a string. // Eg. p1 + "", or System.out.println(p1) public String toString(){ return this.x + ", " + this.y; } }