Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a class called MagicSquare.java. public static void main(String[] args) {

ID: 3826015 • Letter: C

Question

Create a class called MagicSquare.java.

public static void main(String[] args) {
int[][] square = {
{ 8, 11, 14, 1},
{13, 2, 7,12},
{ 3, 16, 9, 6},
{10, 5, 4, 15}
};
System.out.printf("The square %s a magic square. %n",
(isMagicSquare(square) ? "is" : "is not"));
}

It is your job to write the method isMagicSquare:

public static boolean isMagicSquare(int[][] square) {
// TODO
}

How do you know whether a 2-dimensional array is a magic square?
First of all it needs to be a square. To keep the scope of the lab manageable you may assume that the 2-dimensional arrays passed have the same number of rows and columns and that there is at least one row and one column.
The square is 'magic' if the numbers in each of the rows, columns, and diagonals add up to the same value (see image above)

Recommendation: use private methods tp structure your code.

Explanation / Answer

Here is the code for you:

import java.util.*;
class MagicSquare
{
    public static boolean checkRows(int theSquare[][])
    {
       int n = theSquare.length;
       int sum = n * (n*n + 1) / 2;
       for(int i = 0; i < theSquare.length; i++)
       {
          int newSum = 0;
          for(int j = 0; j < theSquare[i].length; j++)
               newSum += theSquare[i][j];
           if(sum != newSum)
               return false;              
       }
       return true;
    }
    public static boolean checkColumns(int theSquare[][])
    {
       int n = theSquare.length;
       int sum = n * (n*n + 1) / 2;
       for(int i = 0; i < theSquare.length; i++)
       {
          int newSum = 0;
          for(int j = 0; j < theSquare[i].length; j++)
               newSum += theSquare[j][i];
           if(sum != newSum)
               return false;              
       }
       return true;
    }
    public static boolean checkDiagonals(int theSquare[][])
    {
       int n = theSquare.length;
       int sum = n * (n*n + 1) / 2;
       int newSum = 0;
       for(int i = 0; i < n; i++)
           newSum += theSquare[i][i];
       return newSum == sum;  
    }
    public static boolean checkRange(int theSquare[][])
    {
       int[] range = new int[theSquare.length * theSquare.length + 1];
       for(int i = 1; i < range.length; i++)
           range[i] = 0;
       for(int i = 0; i < theSquare.length; i++)
           for(int j = 0; j < theSquare[i].length; j++)
               if(theSquare[i][j] < range.length && theSquare[i][j] >= 0)
                   range[theSquare[i][j]] = 1;
       int sum = 0;
       for(int i = 1; i < range.length; i++)
           sum += range[i];
       if(sum == theSquare.length * theSquare.length)
           return true;
       return false;                  
    }
    public static void main(String[] args)
    {
       int[][] square = new int[4][4];
       Scanner sc = new Scanner(System.in);
       System.out.println("The magic value for your square is 34, which means that every row, column");
       System.out.println("and diagonal of your square must add up to that number. ");
       for(int i = 0; i < 4; i++)
       {
          System.out.print("Please enter the 4 values for row " + i + ", separated by spaces: ");
          for(int j = 0; j < 4; j++)
              square[i][j] = sc.nextInt();
       }
       System.out.println(" Checking square for problems:");
       boolean diagonalValidity = checkDiagonals(square);
       boolean rowValidity = checkRows(square);
       boolean columnValidity = checkColumns(square);
       boolean rangeValidity = checkRange(square);
       boolean magicValidity = diagonalValidity && rowValidity && columnValidity && rangeValidity;
       if(diagonalValidity)
           System.out.println("DIAG: VALID");
       else
           System.out.println("DIAG: INVALID");
       if(diagonalValidity)
           System.out.println("ROWS: VALID");
       else
           System.out.println("ROWS: INVALID");
       if(diagonalValidity)
           System.out.println("COLS: VALID");
       else
           System.out.println("COLS: INVALID");
       if(diagonalValidity)
           System.out.println("RANG: VALID");
       else
           System.out.println("RANG: INVALID");
       if(diagonalValidity)
           System.out.println("MAGIC: YES");
       else
           System.out.println("MAGIC: NO");                  
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote