Advertisement
mmayoub

Point, 12.06.2021

Jun 12th, 2021
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package prj12062021;
  2.  
  3. public class Point {
  4.     private double x;
  5.     private double y;
  6.  
  7.     // constructor 1
  8.     public Point() {
  9.         this.x = 0;
  10.         this.y = 0;
  11.     }
  12.  
  13.     // constructor 2
  14.     public Point(double x, double y) {
  15.         this.x = x;
  16.         this.y = y;
  17.     }
  18.  
  19.     // getters
  20.     public double getX() {
  21.         return this.x;
  22.     }
  23.  
  24.     public double getY() {
  25.         return this.y;
  26.     }
  27.  
  28.     // setters
  29.     public void setX(double x) {
  30.         this.x = x;
  31.     }
  32.  
  33.     public void setY(double y) {
  34.         this.y = y;
  35.     }
  36.  
  37.     public void print() {
  38.         System.out.println("(" + x + ", " + y + ")");
  39.     }
  40.  
  41.     public double distance(Point other) {
  42.         double dx = this.x - other.x;
  43.         double dy = this.y - other.y;
  44.         return Math.sqrt(dx * dx + dy * dy);
  45.     }
  46.  
  47.     @Override
  48.     public boolean equals(Object obj) {
  49.         Point p = (Point) obj;
  50.  
  51.         return (this.x == p.x && this.y == p.y);
  52.     }
  53.  
  54.     @Override
  55.     public String toString() {
  56.         return "(" + x + ", " + y + ")";
  57.     }
  58.  
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement