Create a class called MagicSquarejava It includes the follow

Create a class called MagicSquare.java.

It includes the following main method:

public static void main(String[] args) {
int[][] square = {
{2, 7, 6},
{9, 5, 1},
{4, 3, 8}};
System.out.printf(\"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.

2 7 6 15 9 5 1 15 4 3 8 15 15 15 15 15 15 magic square

Solution

MagicSquare.java


public class MagicSquare {
   public static void main(String[] args) {
       int[][] square = {
       {2, 7, 6},
       {9, 5, 1},
       {4, 3, 8}};
       System.out.printf(\"Square %s a magic square. %n\",
       (isMagicSquare(square) ? \"is\" : \"is not\"));
       }
  
   public static boolean isMagicSquare(int[][] square){
       int rowSum[] = new int[square.length];
       int colSum[] = new int[square.length];
       int diagonalSum1 = 0;
       int diagonalSum2 = 0;
             
       for(int i=0; i<square.length; i++){
           for(int j=0; j<square[i].length; j++){
               rowSum[i] = rowSum[i]+square[i][j];
           }
       }
       for(int i=0; i<rowSum.length; i++){
           System.out.println(\"Row \"+i+\" sum is \"+rowSum[i]);
       }
       for(int i=0; i<square.length; i++){
           for(int j=0; j<square[i].length; j++){
               colSum[i] = colSum[i]+square[j][i];
           }
       }
       for(int i=0; i<colSum.length; i++){
           System.out.println(\"Column \"+i+\" sum is \"+colSum[i]);
       }
         
       for(int i=0; i<square.length; i++){
           diagonalSum1 = diagonalSum1+square[i][i];
       }
       for(int i=0; i<square.length; i++){
           diagonalSum2 = diagonalSum2+square[i][square.length-1-i];
       }
       System.out.println(\"Diagonal Sum1: \"+diagonalSum1);
       System.out.println(\"Diagonal Sum2: \"+diagonalSum2);
       for(int i=0; i<rowSum.length-1; i++){
           if(rowSum[i] != rowSum[i+1])
               return false;
       }
       for(int i=0; i<colSum.length-1; i++){
           if(colSum[i] != colSum[i+1])
               return false;
       }
       if(diagonalSum1 != diagonalSum2)
           return false;
         
       return true;
       }

}

Output:

Row 0 sum is 15
Row 1 sum is 15
Row 2 sum is 15
Column 0 sum is 15
Column 1 sum is 15
Column 2 sum is 15
Diagonal Sum1: 15
Diagonal Sum2: 15
Square is a magic square.

Create a class called MagicSquare.java. It includes the following main method: public static void main(String[] args) { int[][] square = { {2, 7, 6}, {9, 5, 1},
Create a class called MagicSquare.java. It includes the following main method: public static void main(String[] args) { int[][] square = { {2, 7, 6}, {9, 5, 1},

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site