Advertisement
PadmaJS

MyPoint

Oct 1st, 2022
838
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 1.06 KB | None | 0 0
  1. //MyPoint.java
  2. public class MyPoint {
  3.   double x;
  4.   double y;
  5.  
  6.   MyPoint() {
  7.     x = y = 0;
  8.   }
  9.  
  10.   MyPoint(double x, double y) {
  11.     this.x = x;
  12.     this.y = y;
  13.   }
  14.  
  15.   public double getX() {
  16.     return x;
  17.   }
  18.  
  19.   public double getY() {
  20.     return y;
  21.   }
  22.  
  23.   public double distance(double x2, double y2) {
  24.     double distance = Math.sqrt(Math.pow((x2 - x), 2) + Math.pow((y2 - y), 2));
  25.     return distance;
  26.   }
  27.  
  28.   public double distance(MyPoint obj) {
  29.     double distance = Math.sqrt(Math.pow((obj.getX() - x), 2) + Math.pow((obj.getY() - y), 2));
  30.     return distance;
  31.   }
  32. }
  33.  
  34. //TestMyPoint.java
  35. public class TestMyPoint {
  36.   public static void main(String[] args) {
  37.     MyPoint point1 = new MyPoint();
  38.     MyPoint point2 = new MyPoint(-5, -5);
  39.     MyPoint point3 = new MyPoint(10, 3);
  40.     double distance1 = point1.distance(point2);
  41.     System.out.println(distance1);
  42.     double distance2 = point1.distance(10, 0);
  43.     System.out.println(distance2);
  44.     double distance3 = point2.distance(point3);
  45.     System.out.println(distance3);
  46.   }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement