Here is a subcode of calculating if a given matrix is a magic square. I'm only confused with the test[element -1] that is duplicated and have two different returns.
public static boolean testNormal(int[][] matrix, int dim){
int magicConstant = dim * (dim * dim +1) / 2;
// checks if all the numbers are present
// the default value is false
boolean[] test = new boolean[dim*dim];
int max = dim*dim;
int element;
for (int row = 0; row < dim; row++){
for (int col = 0; col < dim; col++){
element = matrix[row][col];
if ((element > max)|| (element <= 0))
return false;
if (test[element -1])
return false;
test[element -1] = true;
}
}