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

One interesting application of two-dimensional arrays is magic squares. A magic

ID: 3762911 • Letter: O

Question

One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and both diagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code to determine whether a square is magic. File Square.java contains the shell for a class that represents a square matrix. It contains headers for a constructor that gives the size of the square and methods to read values into the square, print the square, find the sum of a given row, find the sum of a given column, find the sum of the main (or other) diagonal, and determine whether the square is magic. The read method is given for you; you will need to write the others. Note that the read method takes a Scanner object as a parameter. File SquareTest.java contains the shell for a program that reads input for squares from a file named magicData and tells whether each is a magic square. Following the comments, fill in the remaining code. Note that the main method reads the size of a square, then after constructing the square of that size, it calls the readSquare method to read the square in. The readSquare method must be sent the Scanner object as a parameter. You should find that the first, second, and third squares in the input are magic, and that the rest (fourth through seventh) are not. Note that the -1 at the bottom tells the test program to stop reading.

Explanation / Answer

Square.JAVA

import java.util.Scanner;

/**
* @author xinthe
*
*/
public class Square {

   int arr[][];

   // constructor that gives the size of the square and

   public Square(int size) {
       arr = new int[size][size];

   }

   // methods to read values into the square
   public int[][] readSquare(int size) {

       Scanner in = new Scanner(System.in);
       try {
           System.out.println("Enter the elements of Square matrix :");

           for (int c = 0; c < size; c++)
               for (int d = 0; d < size; d++)
                   arr[c][d] = in.nextInt();

       } catch (Exception e) {
       } finally {
           in.close();
       }
       return arr;
   }

   // print the square,

   public void printSquare() {

       final int size = arr.length;

       for (int c = 0; c < size; c++) {
           for (int d = 0; d < size; d++) {
               System.out.print(arr[c][d] + " ");
           }
           System.out.println();
       }

   }

   // find the sum of a given row,

   public int sumOfRow(int rowNum) {

       int sumOfRow = 0;

       final int n = arr.length;

       for (int i = 0; i < n; i++) {

           sumOfRow += arr[rowNum][i];

       }

       return sumOfRow;
   }

   public int sumOfColumn(int colNum) {

       int sumOfColumn = 0;

       final int n = arr.length;
       for (int i = 0; i < n; i++) {

           sumOfColumn += arr[i][colNum];

       }

       return sumOfColumn;
   }

   public int sumOfDiagnal() {

       int sumOfDiagnal = 0;

       final int n = arr.length;

       for (int i = 0; i < n; i++) {
           sumOfDiagnal += arr[i][i];

       }

       return sumOfDiagnal;
   }

   public int sumOfSecondaryDiagnal() {

       int sumOfSecondaryDiagonal = 0;

       final int n = arr.length;

       for (int i = 0; i < n; i++) {
           sumOfSecondaryDiagonal += arr[i][n - i - 1];

       }

       return sumOfSecondaryDiagonal;
   }

   public boolean isMagicSquare() {

       final int n = arr.length;
       final int M = (n * n * (n * n + 1) / 2) / n;
       for (int i = 0; i < n; i++) {

           if (sumOfRow(i) != M || sumOfColumn(i) != M) {
               return false;

           }

       }

       if (sumOfDiagnal() != M || sumOfSecondaryDiagnal() != M) {
           return false;
       }

       return true;

   }
}

SquareTest.java:

import java.util.Scanner;

/**
* @author xinthe
*
*/
public class SquareTest {

   /**
   * @param args
   */
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner scanner = new Scanner(System.in);
       try {
           System.out.print("Enter the size of a square :");
           int sizeOfSquare = scanner.nextInt();
           Square square = new Square(sizeOfSquare);
           square.readSquare(sizeOfSquare);
           System.out.println("Matrix is :");
           square.printSquare();
           if (square.isMagicSquare()) {
               System.out.println("Magic Square ");

           } else {
               System.out.println("Not Magic Square ");
           }
       } catch (Exception e) {
       } finally {
           scanner.close();
       }

   }

}

OUTPUT :

Test1:
Enter the size of a square :3
Enter the elements of Square matrix :
4
9
2
3
5
7
8
1
6
Matrix is :
Square matrix :
4   9   2  
3   5   7  
8   1   6  

Magic Square

Test2 :

Enter the size of a square :3
Enter the elements of Square matrix :
4
9
2
3
5
7
8
1
5
Matrix is :
4   9   2  
3   5   7  
8   1   5  
Not Magic Square

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