Advertisement
calcpage

APCS_CH8_Point.java

Feb 14th, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.64 KB | None | 0 0
  1. //Point.java        MrG     2013.0208
  2. public class Point implements Comparable
  3. {
  4.     private double x;
  5.     private double y;
  6.  
  7.     public Point(double x, double y)
  8.     {
  9.         this.x=x;
  10.         this.y=y;
  11.     }
  12.  
  13.     private double distance()
  14.     {
  15.         return Math.sqrt(x*x+y*y);
  16.     }
  17.  
  18.     public int compareTo(Object other)
  19.     {
  20.         Point temp = (Point)other;
  21.         return (int)(this.distance()-temp.distance());
  22.     }
  23.  
  24.     public boolean equals(Object other)
  25.     {
  26.         Point temp = (Point)other;
  27.         return this.compareTo(temp)==0;
  28.     }
  29.  
  30.     public String toString()
  31.     {
  32.         return "(" + x + ", " + y + ")";
  33.     }
  34.  
  35.     public double getX()
  36.     {
  37.         return x;
  38.     }
  39.  
  40.     public double getY()
  41.     {
  42.         return y;
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement