Guest User

Untitled

a guest
Apr 26th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. public class Point {
  2. int x;
  3. int y;
  4. public Point(int x0, int y0) {
  5. x = x0;
  6. y = y0;
  7. }
  8. @Override
  9. public String toString() {
  10. return "(" + x + "/" + y + ")";
  11. }
  12. public double distance(Point p) {
  13. int x = this.x;
  14. int y = this.y;
  15. int a;
  16. int b;
  17. double abs;
  18.  
  19. a = x - p.x;
  20. if (a < 0) {
  21. a = -1 * a;
  22. }
  23.  
  24. b = y - p.y;
  25. if (b < 0) {
  26. b = -1 * b;
  27. }
  28.  
  29. abs = Math.pow((a*a + b*b), 0.5);
  30.  
  31. return abs;
  32. }
  33. public static void main(String[] args) {
  34. Point p1 = new Point(1, 1);
  35. Point p2 = new Point(4, 4);
  36.  
  37. System.out.println("Abstand: " + p1.distance(p2));
  38. }
  39. }
Add Comment
Please, Sign In to add comment