0

I'm not sure how to write an equation in Netbeans. The equation is supposed to be: (5−x)^2 +(5−y)^2 all under a square root.

This is what I have tried:

public static int getScore(int x, int y){
   return ( (((5-x)^2 + (5-y)^2))^(1/2) );
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
Jordon
  • 91
  • 1
  • 9
  • 3
    Look into Math.pow and Math.sqrt. Also, consider using double variables and return type instead of int. – Eran Mar 02 '16 at 06:17
  • 2
    ^ performs XOR in Java. See this: http://stackoverflow.com/questions/1991380/what-does-the-operator-do-in-java – bane Mar 02 '16 at 06:18

2 Answers2

8

This is one of those cases where there's a specialized library function:

return Math.hypot(5-x, 5-y);

This avoids the overflow and underflow issues in computing the square root of the sum of squares directly

Joni
  • 108,737
  • 14
  • 143
  • 193
1

The carat ^ performs exclusive or operator in java, which is a bits thing. Don't use it for exponents.

The expression you are looking for is

return Math.sqrt(Math.pow(5 - x, 2) + Math.pow(5 - y, 2)));