binibiningtinamoran

Point.java

Jan 1st, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. public class Point {
  2.  
  3.     private int x, y;
  4.  
  5.     public Point() {
  6.         this.x = 0;
  7.         this.y = 0;
  8.     }
  9.  
  10.     public Point(int x, int y) {
  11.         this.x = x;
  12.         this.y = y;
  13.     }
  14.  
  15.     public int getX() {
  16.         return x;
  17.     }
  18.  
  19.     public void setX(int x) {
  20.         this.x = x;
  21.     }
  22.  
  23.     public int getY() {
  24.         return y;
  25.     }
  26.  
  27.     public void setY(int y) {
  28.         this.y = y;
  29.     }
  30.     public double distance(Point anotherPoint) {
  31.         double xCoordSq = Math.pow((x - anotherPoint.x), 2);
  32.         double yCoordSq = Math.pow((y - anotherPoint.y), 2);
  33.  
  34.         return Math.sqrt(xCoordSq + yCoordSq);
  35.     }
  36.  
  37.     public static void main(String[] args) {
  38.         Point first = new Point(6, 5);
  39.         Point second = new Point(3, 1);
  40.         System.out.println("distance(0,0)= " + first.distance(new Point(0,0)));
  41.         System.out.println("distance(second)= " + first.distance(second));
  42.         System.out.println("distance(2,2)= " + first.distance(new Point(2,2)));
  43.         Point point = new Point();
  44.         System.out.println("distance()= " + point.distance(new Point()));
  45.  
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment