// Ayaz Latif // TA: Grace Hopper // CSE 142 // A client program using the Point class (creating multiple Point instances and // asking for their distance from the origin). public class PointClient { public static void main(String[] args) { // construct a point p1 at coordinates (3, 5) // construct a point p2 at coordinates (12, 4) Point p1 = new Point(); p1.x = 3; p1.y = 5; Point p2 = new Point(); p2.x = 12; p2.y = 4; System.out.println("p1: x = " + p1.x + ", y = " + p1.y); System.out.println("p2: x = " + p2.x + ", y = " + p2.y); // translate p1 by (-1, 4) // translate p2 by (6, 3) p1.translate(-1, 4); // new OOP way! // translate(p1, -1, 4); // verb(p1) old style! p2.translate(6, 3); System.out.println("p1: x = " + p1.x + ", y = " + p1.y); System.out.println("p2: x = " + p2.x + ", y = " + p2.y); // formula: square root of x^2 + y^2 // print p1 distance to origin double distanceOfP1 = p1.distanceFromOrigin(); System.out.println("distance from origin p1 = " + distanceOfP1); // formula: square root (x2 - x1)^2 + (x2 - x1)^2 // distance to another point double distanceBetween = p1.distanceTo(p2); System.out.println("distance between p1 and p2 = " + distanceBetween); } // bad style! Having translate here would mean having to reimplement // this method everytime the Point class is used. It is better to // have this in the point class as an instance method! public static void translate(Point p, int dx, int dy) { p.x = p.x + dx; p.y = p.y + dy; } } // Notes from lecture: // procedural programming: static methods // object oriented programming (OOP) // 1970s: Xerox PARC, Alan Kay, OOP // command line interface (like procedural) // rm hi.txt // verb noun // Graphical User Interface: GUI (like OOP) // clicked on the icon, action (dragging something to trash) // noun verb // Random, DrawingPanel, Scanner // Clients: Scanner input = new Scanner(new File("hi.txt")); // input.hasNextLine(); // noun.verb()