In this code snippet my main class calls isPerfectSquare() and if it returns true, getM() is called.
when getM() is called, if I remove the (int) casting where the int m is assigned, I get a compiler error (Incomparable Types: possible lossy conversion from double to int).
My question is; how is it that the compiler thinks that the code can return a double when the former method checks if the int total passed to it is a perfect square?
(if this is not considered a good question please tell me why)
private static boolean isPerfectSquare(int total)
{
int square = (int) Math.sqrt(total);
//test
System.out.print(square);
return square * square == total;
}
/*Returns the sum value that should be returned by adding
the magic square sides and diagonals*/
//m is global var
private static void getM(int total)
{
int n = (int) Math.sqrt(total);
m = (int) (n*(Math.pow(n, 2) + 1)/2);
}