Advertisement
skity

Point.java

Nov 27th, 2020
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.12 KB | None | 0 0
  1.  
  2. public class Point {
  3.     /**
  4.      * X coordinate.
  5.      */
  6.     private int x;
  7.    
  8.     /**
  9.      * Y coordinate.
  10.      */
  11.     private int y;
  12.  
  13.     /**
  14.      * Default constructor.
  15.      */
  16.     public Point() {
  17.         this(0, 0);
  18.     }
  19.  
  20.     /**
  21.      * Constructor.
  22.      * @param x Coordinate for x-axis.
  23.      * @param y Coordinate for y-axis.
  24.      */
  25.     public Point(int x, int y) {
  26.         this.x = x;
  27.         this.y = y;
  28.     }
  29.  
  30.     /**
  31.      * Copy constructor.
  32.      * @param p Object to copy.
  33.      */
  34.     public Point(Point p) {
  35.         this(p.x, p.y);
  36.     }
  37.    
  38.     /**
  39.      * @return the distance of p from this.
  40.      */
  41.     public double distanceFrom(Point p) {
  42.         int dx = this.x - p.x;
  43.         int dy = this.y - p.y;
  44.         return Math.sqrt(dx*dx + dy*dy);
  45.     }
  46.    
  47.     /**
  48.      * @param point to compare to.
  49.      * @return true if the ginen point equals this point.
  50.      */
  51.     public boolean equals(Point p) {
  52.         return this.x == p.x && this.y == p.y;
  53.     }
  54.  
  55.     public String toString() {
  56.        
  57.         return String.format("(%d, %d)", getX(), getY());
  58.     }
  59.    
  60.     public int getX() {
  61.         return x;
  62.     }
  63.  
  64.     public void setX(int x) {
  65.         this.x = x;
  66.     }
  67.  
  68.     public int getY() {
  69.         return y;
  70.     }
  71.  
  72.     public void setY(int y) {
  73.         this.y = y;
  74.     }
  75.  
  76.  
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement