-1

Add the following method to the Point class:

public double distance(Point other)

Returns the distance between the current Point object and the given other Point object. The distance between two points is equal to the square root of the sum of the squares of the differences of their x- and y-coordinates. In other words, the distance between two points (x1, y1) and (x2, y2) can be expressed as the square root of (x2 - x1)2 + (y2 - y1)2. Two points with the same (x, y) coordinates should return a distance of 0.0.

public class Point {
    int x;
    int y;

    // // your code goes here

}

This works:

public double distance(Point other){
    return Math.sqrt((this.x-other.x)*(this.x-other.x) + (this.y-other.y)*(this.y-other.y));
}

This doesn't:

public double distance(Point other){
    return Math.sqrt((this.x-other.x)^2 + (this.y-other.y)^2);
}

May I ask why? TY

  • 3
    Because `^` is not "power of". – Yunnosch Sep 08 '18 at 07:08
  • Is there any language in which `^` actually IS power-of? I see it so often (and often use it myself in prose) that there must be a common source somewhere... – Yunnosch Sep 08 '18 at 07:10
  • Caret operator in java is used for XOR, https://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java – The Scientific Method Sep 08 '18 at 07:12
  • 1
    @Yunnosch [Wikipedia lists a few](https://en.wikipedia.org/wiki/Exponentiation#In_programming_languages) but I guess it's just "learned" from mathematics and assumed to work in programming the same way. – tkausl Sep 08 '18 at 07:15
  • @tkausl I was thinking along similar paths, but where has any math ever used any similar notation? All my math teachers would fry me alive... Most likely it is the quasi-monopolist twisting minds again... – Yunnosch Sep 08 '18 at 07:16
  • @Yunnosch With a pen on paper, yes. But when writing equations on a computer I regularly use `^` as power, since I've no clue how to do anything other than ² and ³ on my keyboard. Math-programs such as wolframalpha also understand it, so I'm used to it – tkausl Sep 08 '18 at 07:20

1 Answers1

0

To get this out of the list of unanswered questions and because I think it is not a typo:

^ is not "power-of", it is XOR.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54