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

Write a Java class called MagicSquare to construct an n x n magic square for any

ID: 3641017 • Letter: W

Question

Write a Java class called MagicSquare to construct an n x n magic square for any odd
input value of n . A magic square is an n x n matrix in which each of the integers 1, 2, 3, …, n2 appears exactly once and all column sums, row sums, and diagonal sums are equal. For example, the following is a 5 x 5 magic square in which all the rows, column, and diagonals add up to 65:

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

The following is a procedure for constructing an n x n magic square for any odd integer n. Place 1 in the middle of the top row. Then, after integer k has been placed, move up one row and one column to the right to place the next integer k + 1, unless one of the following occurs. If a move takes you above the top row in the jth column, move to the bottom of the jth column and place k + 1 there.
? If a move takes you outside to the right of the square in the ith row, place k + 1 in the ith row at the left side. If a move takes you to an already filled square or if you move out of the square at the upper right-hand corner, place k + 1 immediately below k. The following presents a sample run of your program. In this program, you can assume that the input number n is less than or equal to 21.

Enter input number: 5

***** 5 x 5 Magic Square *****
| 17 | 24 | 1 | 8 | 15 |
| 23 | 5 | 7 | 14 | 16 |
| 4 | 6 | 13 | 20 | 22 |
| 10 | 12 | 19 | 21 | 3 |
| 11 | 18 | 25 | 2 | 9 |

Explanation / Answer

import java.util.Scanner;

public class MagicSquare {
    //Class MagicSquare
  
    //constructor
    public MagicSquare(int n) {
        size = n;
        grid = new int[n][n];
        fill();
    }
  
    //display magic square
    public void display() {
        for (int r = 0; r < size; r++) {
            System.out.print("|");
            for (int c = 0; c < size; c++) {
                if (size * size <= 10) {
                    System.out.print(grid[r][c] + "|");
                }
                else if (size * size <= 100) {
                    System.out.printf("%2d|", grid[r][c]);
                }
                else {
                    System.out.printf("%3d|", grid[r][c]);
                }
            }
            System.out.println();
        }
    }
  
    //private method to fill the magic square
    private void fill() {
        int row = 0;
        int col = size / 2;
        grid[row][col] = 1;
        for (int i = 2; i <= size * size; i++) {
            int nextRow = (row - 1 + size) % size;
            int nextCol = (col + 1) % size;
            if (grid[nextRow][nextCol] != 0 || (row == 0 && col == size - 1)) {
                row++;
            }
            else {
                row = nextRow;
                col = nextCol;
            }
            grid[row][col] = i;
        }
    }
  
    //private fields
    private int size;
    private int [][] grid;
  
    //End of class MagicSquare
  
  
    //main
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = 0;
      
        while (n < 1 || n % 2 == 0) {
            System.out.print("Enter n: ");
            n = in.nextInt();
            if (n < 1 || n % 2 == 0) {
                System.out.println("n must be an odd number");
            }
        }
      
        MagicSquare ms = new MagicSquare(n);
      
        System.out.printf(" ***** %d x %d Magic Square ***** ", n, n);
        ms.display();
      
    } //End of main
}

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