CS 535 Object-Oriented Programming & Design Fall Semester, 2000 Assignment 2 |
||
---|---|---|
© 2000, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 23-Sep-00 |
/** * Represents a point in a two dimensional plane. */ public class Point { private float x; private float y; public Point( float x, float y) { this.x = x; this.y = y; } public Point add( Point aPoint) { return new Point( x + aPoint.x, y + aPoint.y); } public Point subtract( Point aPoint) { return new Point( x - aPoint.x, y - aPoint.y); } public Point divide(float scalar) { return new Point( x/scalar, y/scalar); } public double distance( Point aPoint) { float deltaX = x - aPoint.x; float deltaY = y - aPoint.y; return Math.sqrt( deltaX* deltaX + deltaY* deltaY); } /*** Returns true if the receiver is above and to the right of aPoint. */ public boolean greaterThan(Point aPoint) { return (x > aPoint.x ) && (y > aPoint.y); } /*** Returns true if the receiver is below and to the left of aPoint. */ public boolean lessThan(Point aPoint ) { return (x < aPoint.x ) && (y < aPoint.y); } public String toString() { return "(" + x + ", " + y + ")"; } public int hashCode() { long bits = java.lang.Double.doubleToLongBits( x ); bits = bits ^ java.lang.Double.doubleToLongBits( y ) * 31; return (((int) bits) ^ ((int) (bits >> 32))); } public boolean equals( Point p) { return x == p.x && y == p.y; } }