/* CSE 142, Autumn 2007, Marty Stepp This file defines a new class of objects called Point. Each Point object contains two ints named x and y, as well as its own copy of each method and constructor written below. */ public class Point { // fields: the data inside of each object int x; int y; // constructor: initializes each object when 'new' is used in client // This constructor initializes the new Point object to use the given x/y values. public Point(int initialX, int initialY) { x = initialX; y = initialY; } // methods: the behavior inside of each object // Each of these methods executes "inside" a particular Point object, // so it knows about and can refer to that object's fields directly by name. // Returns the distance between this Point object and the given Point p2. public double distance(Point p2) { int dx = x - p2.x; int dy = y - p2.y; return Math.sqrt(dx * dx + dy * dy); } // Returns the distance between this Point object and the origin, (0, 0). public double distanceFromOrigin() { Point origin = new Point(0, 0); // (0, 0) return distance(origin); } // Changes this Point object's (x, y) location to the given coordinates. public void setLocation(int newX, int newY) { x = newX; y = newY; } // Shifts this Point object's (x, y) location by the given amounts. public void translate(int dx, int dy) { setLocation(x + dx, y + dy); } }