/* Point: a class whose objects represent 2d points */ class Point extends Object { private double x; private double y; // method: two-argument constructor // in: two doubles, x and y // out: a new Point public Point(double x_in, double y_in) { this.x = x_in; this.y = y_in; } // method: zero-argument constructor // in: no inputs // out: a new Point (at the origin) public Point() // a zero-input constructor! { this.x = 0.0; this.y = 0.0; } // method: nudgeBy // in: two doubles, the amount to nudge the point // out: nothing (a void function) // ** But, this point itself has changed ** public void nudgeBy(double delta_x, double delta_y) { this.x = this.x + delta_x; this.y += delta_y; // shorter way return; } // method: equals // in: a Point, other // out: a boolean, whether or not this and p are at // the same coordinates.the same Point public boolean equals(Point other) { if ( this.x == other.x && this.y == other.y ) { return true; } else { return false; } // Alternatively, we could say it in one line: // return ( this.x == other.x && this.y == other.y ); } // method: toString // in: none // out: a String representation of this Point public String toString() { String s = "(" + this.x + "," + this.y + ")"; return s; } // method: magnitude // in: nothing (no arguments) // out: a double, the distance from the origin to 'this'. public double magnitude() { // An example of calling Math (static) methods // The Java API lists them all (Google for "Java 1.6 API") return Math.sqrt( this.x*this.x + this.y*this.y ); } // static method: add // in: two Points, p1 and p2 // out: a new Point that is the sum of p1 and p2 static public Point add( Point p1, Point p2 ) { return new Point( p1.x + p2.x, p1.y + p2.y ); } // static method: displaySmaller // in: Two points // out: nothing (a void function) public static void displaySmaller(Point a, Point b) { double aMag = a.magnitude(); double bMag = b.magnitude(); if (aMag < bMag) { System.out.println(a + " is smaller than " + b); System.out.println("Smallest magnitude is " + aMag); } else { System.out.println(a + " is not smaller than " + b); System.out.println("Smallest magnitude is " + bMag); } } // static method: main // in: any parameters specified with the run command (ignored) // out: nothing (a void function) // ** printing stuff is quite different from returning stuff! ** public static void main(String[] args) { Point p1 = new Point(30, 40); Point p2 = new Point(12, 2); System.out.println( "p1 is " + p1.toString() ); System.out.println( "p2 is " + p2 ); p1.nudgeBy( 30, 20 ); System.out.println("now p1 is " + p1); Point p3 = Point.add( p1, p2 ); System.out.println( "p3 (their sum) is " + p3 ); Point.displaySmaller(p1, p2); Point p4 = new Point(); p4.nudgeBy( 60, 60 ); System.out.println("p1 == p4 is " + (p1 == p4) ); System.out.println("p1.equals(p4) is " + p1.equals(p4)); Point p5 = p2; System.out.println("Before nudge:"); System.out.println("p2 is " + p2); System.out.println("p5 is " + p5); p2.nudgeBy(5, 5); System.out.println("After nudge:"); System.out.println("p2 is " + p2); System.out.println("p5 is " + p5); System.out.println("p2 == p5 is " + (p2 == p5) ); return; } }