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

A magic square is a grid of unique numbers (i.e. no number can appear more than

ID: 3861237 • Letter: A

Question

A magic square is a grid of unique numbers (i.e. no number can appear more than once) in which the sum of all the rows, columns, and diagonals are the same. Here is an example in which all of the rows, columns and diagonals (upper left corner, center, lower right corner and upper right corner, center, and lower left corner) sum to 15. Generally, the numbers within a magic square range between 1 and n2, where n is the size of the side of the square. 4 9 2 3 5 7 8 1 6 Your goal for this project is to write a program that helps a user to create a magic square. Your program should meet the following requirements: Initialization: A Magic square is a grid of numbers with n numbers along each side. For example, in the magic square above, n = 3. The value for n can technically be any positive number except 2 (because then there would be no diagonals), but due to hardware limitations it is probably a good idea to put an upper bound on n. For this program we will allow n to be at most 8. When your program begins its execution it should ask the user how big the magic square should be. For example: Let’s make a Magic Square! How big should it be? 2 That would violate the laws of mathematics! Let’s make a Magic Square! How big should it be? -4 That would violate the laws of mathematics! Let’s make a Magic Square! How big should it be? 12 That’s huge! Please enter a number less than 9. Let’s make a Magic Square! How big should it be? 4 Great! Display: After the size has been chosen and in between each user input, your program should display the current state of the square. Use zeros to indicate empty places. For example, after the initialization above, your program should display: The square currently looks like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Value entry: Allow the user to enter or change a value in the puzzle by providing the row and column of the value they want to change, along with the new value. Keep in mind that a user might want to overwrite an existing value. Also, remember that all numbers must be between 1 and n2. For example: Where do you want to put a new value? Row: 0 Column: 3 What value should go there? 2 The square currently looks like this: 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 Where do you want to put a new value? Row: 0 Column: 3 What value should go there? 4 The square currently looks like this: 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 Where do you want to put a new value? Row: 0 Column: 3 What value should go there? 191 You can only use numbers between 1 and 16 for this square. The square currently looks like this: 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 Solution Recognition: When the magic square is complete, congratulate the user. For example: The square currently looks like this: 7 12 1 14 2 13 8 11 16 3 10 5 9 6 15 4 Victory! This is what I have so far. I need help finishing and debugging my program public static void main(String[] args) { int size = getSize(); printSquare(); playSquare(); } public int getSize() { Scanner input = new Scanner(System.in); int size = -1; boolean inputtaken = false; while (!inputtaken) { System.out.printf("Lets make a Magic Square! How big should it be?"); size = input.nextInt(); if (size < 3) { System.out.println("That would violate the laws of mathematics!"); } else if (size > 9) { System.out.println("Thats huge! Please enter a number less than 9."); } else { System.out.println("Great"); inputtaken = true; } } return size; } public void printSquare(int size) { System.out.println("The square currently looks like this:"); int i, j; for (i = 0; i < size; i++) { for (j = 0; j < size; j++) { Object[][][] array = null; System.out.printf("%3d ", array[i][j]); } System.out.println(); } } public void playSquare(int size, int[][] array) { Scanner input = new Scanner(System.in); boolean done = false; while (!done) { int result = printSquare(); if (checkSquare(array)) { done = true; } else { System.out.println("Where do you want to put a new value?"); System.out.printf("Row:"); int row = input.nextInt(); System.out.printf("COl:"); int col = input.nextInt(); System.out.println("What value should go there?"); int val = input.nextInt(); if (val >= 1 && val <= size * size) { array[row][col] = val; } else { System.out.println("You can only use numbers between " + 1 + " and " + size * size + " for this square."); } System.out.println("victory"); } public static boolean checkSquare(int[][] array, int size) { for (int row = 0; row < size; row++) { int sumOfRow = 0; int sumOfColoumns = 0; int nSquare = size * size; int M = (size * size * (size * size + 1) / 2) / size; int sumOfPrimaryDiagonal = 0; int sumOfSecondaryDiagonal = 0; boolean[] flag = new boolean[size * size]; for (int col = 0; col < size; col++) { if (array[row][col] < 1 || array[row][col] > nSquare) { return false; } if (flag[array[row][col] - 1] == true) { return false; } flag[array[row][col] - 1] = true; sumOfRow += array[row][col]; sumOfColoumns += array[col][row]; } sumOfPrimaryDiagonal += array[row][row]; sumOfSecondaryDiagonal += array[row][size - row - 1]; if (sumOfRow != M || sumOfColoumns != M) { return false; } if (sumOfPrimaryDiagonal != M || sumOfSecondaryDiagonal != M) { return false; } } return true; } }

Explanation / Answer

You did an awesome job. I just tweaked it little bit to make it work and refactored a little bit.

import java.util.Scanner;

public class MagicSquare {
   public static void main(String[] args)
   {
       int size = getSize();
       Integer[][] squareArray = new Integer[size][size];
       initiliaze(squareArray);
       printSquare(squareArray);
       do
       {
           playSquare(squareArray);
           printSquare(squareArray);
       }while(!isMagicSquare(squareArray));
      
       System.out.println("Victory!");
   }
  

// Initilize all numbers to 0
   private static void initiliaze(Integer[][] squareArray) {
       for(int i =0; i<squareArray.length;i++)
           for(int j =0; j<squareArray.length;j++)
               squareArray[i][j] = 0;
   }

// Get the squar size

   public static int getSize()
   {
       Scanner input = new Scanner(System.in);
       int size = -1;
       boolean inputtaken = false;
       while (!inputtaken)
       {
           System.out.printf("Lets make a Magic Square! How big should it be?");
           size = input.nextInt();
           if (size < 2)
           {
               System.out.println("That would violate the laws of mathematics!");
           }
           else if (size > 9)
           {
               System.out.println("Thats huge! Please enter a number less than 9.");
           }
           else
           {
               System.out.println("Great");
               inputtaken = true;
           }
       }
      
       return size;
   }
  

// Print the square
   public static void printSquare(Integer[][] squareArray)
   {
       System.out.println("The square currently looks like this:");
       int i, j;
      
       for (i = 0; i < squareArray.length; i++)
       {
           for (j = 0; j < squareArray.length; j++)
           {
               System.out.printf("%3d ", squareArray[i][j]);
           }
           System.out.println();
       }
   }
  

// Take user input for updating square
   public static void playSquare(Integer[][] array)
   {
       Scanner input = new Scanner(System.in);
       boolean done = false;
       while (!done)
       {
           System.out.println("Where do you want to put a new value?");
           System.out.printf("Row:"); int row = input.nextInt();
           if(row<0 || row>=array.length)
           {
               System.out.println("Row value should be between 0 and" + (array.length-1));
               continue;
           }
           System.out.printf("COl:"); int col = input.nextInt();
           if(col<0 || col>=array.length)
           {
               System.out.println("Col value should be between 0 and" + (array.length-1));
               continue;
           }
           System.out.println("What value should go there?");
          
           int val = input.nextInt();
           if (val >= 1 && val <= array.length * array.length)
           {
               array[row][col] = val;
               done = true;
           }
           else
           {
               System.out.println("You can only use numbers between " + 1 + " and " + array.length * array.length + " for this square.");
           }
       }
   }
  

// Verify whether it is a magic square or not
   public static boolean isMagicSquare(Integer[][] array)
   {

       /*
       array[0][0] = 4;
       array[0][1] = 9;
       array[0][2] = 2;
       array[1][0] = 3;
       array[1][1] = 5;
       array[1][2] = 7;
       array[2][0] = 8;
       array[2][1] = 1;
       array[2][2] = 6;*/
       boolean ret = true;
       int size = array.length;
       int[] rowSums = new int[size];
       int[] colSums = new int[size];
      
       int sumOfPrimaryDiagonal = 0;
       int sumOfSecondaryDiagonal = 0;
       for (int row = 0; row < size; row++)
       {
           for (int col = 0; col < size; col++)
           {

// simple approach to add the current element to the respect row and column
               rowSums[row] += array[row][col];
               colSums[col] += array[row][col];
  

// Simple approach to determine whether it is part of primary(row == col) or secondary diagonals(row+col == length -1)
               if(row == col)
               {
                   sumOfPrimaryDiagonal += array[row][col];
               }
              
               if((row+col) == (size-1))
               {
                   sumOfSecondaryDiagonal += array[row][col];
               }
            }
       }
      
       if(sumOfPrimaryDiagonal == sumOfSecondaryDiagonal)
       {
           for(int i =0;i<size;i++)
           {
               if(sumOfPrimaryDiagonal == rowSums[i] && sumOfPrimaryDiagonal == colSums[i])
                   continue;
               else
               {
                   ret = false;
                   break;
               }
           }
       }
       else
       {
           ret = false;
       }
      
       return ret;          
   }
}

//Sample output.

Lets make a Magic Square! How big should it be?3
Great
The square currently looks like this:
0 0 0
0 0 0
0 0 0
Where do you want to put a new value?
Row:0
COl:0
What value should go there?
4
The square currently looks like this:
4 0 0
0 0 0
0 0 0
Where do you want to put a new value?
Row:0
COl:1
What value should go there?
9
The square currently looks like this:
4 9 0
0 0 0
0 0 0
Where do you want to put a new value?
Row:0
COl:2
What value should go there?
2
The square currently looks like this:
4 9 2
0 0 0
0 0 0
Where do you want to put a new value?
Row:1
COl:0
What value should go there?
3
The square currently looks like this:
4 9 2
3 0 0
0 0 0
Where do you want to put a new value?
Row:1
COl:1
What value should go there?
5
The square currently looks like this:
4 9 2
3 5 0
0 0 0
Where do you want to put a new value?
Row:1
COl:2
What value should go there?
7
The square currently looks like this:
4 9 2
3 5 7
0 0 0
Where do you want to put a new value?
Row:2
COl:0
What value should go there?
8
The square currently looks like this:
4 9 2
3 5 7
8 0 0
Where do you want to put a new value?
Row:2
COl:1
What value should go there?
1
The square currently looks like this:
4 9 2
3 5 7
8 1 0
Where do you want to put a new value?
Row:2
COl:2
What value should go there?
6
The square currently looks like this:
4 9 2
3 5 7
8 1 6
Victory!

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