// Helene Martin, CSE 142 // Blueprint for creating objects of type Point public class Point { // fields (state) int x; // each Point object has own x and y int y; // constructors don't have explicit return types // and have the same name as the class. They get // called when a client creates a new object of // this type using the new keyword. public Point(int initialX, int initialY) { System.out.println("Building point: " + initialX + ", " + initialY); x = initialX; y = initialY; } // a class can have multiple constructors. public Point() { x = 0; y = 0; // also: this(0,0); } // moves x and y coordinates of a particular point. // mutator - changes state (generally void; generally has parameters) public void translate(int dx, int dy) { x += dx; // x is interpreted as this.x where 'this' y += dy; // is the implicit parameter (object 'translate' is called on) } // calculates and returns the distance from this point to (0, 0) // accessor - return something based on object's state public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); } // sets the point's location to the specified x and y coordinates // mutator public void setLocation(int newX, int newY) { x = newX; // assign to fields (on left) y = newY; } /* Another way to avoid shadowing: public void setLocation(int x, int y) { this.x = x; // assign to fields (on left) this.y = y; } */ // returns a String representation of this point in the form (x, y) // (called by println) public String toString() { return "(" + x + ", " + y + ")"; } }