Java returns a logical error

I am new to java language and I am confused about why an error occurs here. This is a very short piece of code that seems to have a mental block. Any suggestions?

public class Rigidbody { public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ if(Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)){ return true; } } } 

Does anyone know what I'm missing here? (This is probably really obvious).

+4
source share
6 answers

Well, firstly, you forgot to have an else clause :

 public boolean checkCircleCollision(float x1, float y1, float r1, float x2, float y2, float r2) { if (Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2)){ return true; } else { return false; } } 

Someone else pointed out , this can be shortened as follows:

 public boolean checkCircleCollision(float x1, float y1, float r1, float x2, float y2, float r2) { return Math.sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2)) <= (r1 + r2); } 

(make sure you specify them to indicate :-)


However, your code is still wrong.

As stated here , the Java ^ operator is intended for exceptional bitwise OR, and not exponentiation. Perhaps you want Math.pow() ?

Returns the value of the first argument raised to the power of the second argument.

 public boolean checkCircleCollision(float x1, float y1, float r1, float x2, float y2, float r2) { return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2)) <= (r1 + r2); } 

Or you can also just use Math.hypot instead of riding yourself!

Returns sqrt (x ^ 2 + y ^ 2) without intermediate overflow or downstream.

 public boolean checkCircleCollision(float x1, float y1, float r1, float x2, float y2, float r2) { return Math.hypot(x2 - x1, y2 - y1) <= (r1 + r2); } 
+14
source

You can make the method body much easier ...

 public class Rigidbody { public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ return Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2) } } 

The result <= always Boolean.

+11
source

You forgot the else if part.

 public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ if (Math.sqrt(Math.pow(x2-x1, 2)+ Math.pow(y2-y1,2)) <= (size1+size2)) { return true; } else { return false; } } 

What can be simplified to:

 public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ return Math.sqrt(Math.pow(x2-x1, 2)+ Math.pow(y2-y1,2)) <= (size1+size2); } 

EDIT . Also, as @veer noted, you use ^2 when you should use Math.pow , since the ^ operator in Java is a bitwise XOR, not a power operator. So go up and accept his answer , as this is the main cause of the error.

+9
source

Try it, I used Math.pow for the square. A return statement has also been added for the else condition.

  public class Rigidbody { public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ return (Math.sqrt(Math.pow((x2-x1), 2) + Math.pow((y2-y1), 2)) <= (size1+size2)); } } 
+2
source

The OP has a problem in his / her code due to the use of the ^ character, which should be replaced with Math#pow() . But he / she asks that he is lacking . The answer is very simple: each method should return a value, with the exception of void methods.

If you

 public boolean aBooleanMethod(int x) { //some fancy and nice code! return true; } 

The compiler will be happy. But if you do something like this:

 public boolean anotherBooleanMethod(int x) { if (x < 2) { return true; } } 

The compiler will throw an exception:

This method should return a result of type boolean.

Why is this? Since at the end of the method the compiler finds a path that cannot return a value, so instead of crashing your program or returning an unexpected result (for example, false when there is no return), the Java compiler will throw an exception.

How to solve it? Easy: don't forget to return a value for each path in your method.

Ways to solve the problem in the last piece of code:

  • Adding the default return value at the end of the method. This is a common approach used by almost all programmers.

     public boolean anotherBooleanMethod(int x) { if (x < 2) { return true; } //if the method has never returned true, then it must return false... return false; } 
  • For boolean results, you can return the condition:

     public boolean anotherBooleanMethod(int x) { //your method always return a value, no compiler problems //the value depends if x is less than 2 (true) or 2 or more (false) return (x < 2); } 

The exclusion of void methods does not mean that the void method cannot use the return keyword, it just means that it should return "nothing." To publish a sample:

 public void theVoidMethod(int x) { System.out.println("Welcome to theVoidMethod!"); if (x < 2) { //return nothing! End of the method return; } System.out.println("You've reached the bottom of theVoidMethod!"); //here, the Java compiler will add a return sentence for you, no need to code it //return } 

If you test this method using 1 and 2 as parameters, you will get different results:

theVoidMethod (1):

 Welcome to theVoidMethod! 

theVoidMethod (2):

 Welcome to theVoidMethod! You've reached the bottom of theVoidMethod! 

Now, to solve other problems in your code, you should use the Math#pow method instead of the ^ character, but this is strongly explained in other answers.

+2
source

when this condition is false, it does not return any thing why it is wrong.

 public class Rigidbody { public boolean checkCircleCollision(float x1,float y1,float size1,float x2,float y2,float size2){ if(Math.sqrt(((x2-x1)^2)+((y2-y1)^2))<=(size1+size2)){ return true; } else{ return false } } } 
0
source

All Articles