Magic Square ( Due 12 Feb 2010 )

A n x n matrix that is filled with the numbers 1, 2, 3, ..., n² is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

Implement the following algorithm to construct the magic n-by-n squares. This algorithm works only if n is odd.

Write two classes called MagicSquare and TestMagicSquare. The skeleton of the classes will look like this:

class MagicSquare
{
  private int n;
  private int[][] square;

  // Constructor that assigns space for the 2-dimensional array
  public MagicSquare ( int num ) { ... }

  // Populates the array with numbers from 1 to n2
  public void makeSquare () { ... }

  // Prints the magic square in a neat format where the numbers
  // are right justified
  public void printSquare () { ... }

  // Checks that the array generated is indeed a magic square
  public void checkSquare () { ... }
}

public class TestMagicSquare
{
  public static void main ( String [] args ) { ... }
}

In your method main() you will prompt the user to enter an odd number. You MUST use a Scanner object for input. You must check that the input is a positive odd number greater than or equal to 3. If it is not, you will prompt the user to re-enter the number and check again.

Then you will create a MagicSquare object. You will call the makeSquare() method to create the magic square. You will then print out the magic square in a neat format by calling the method printSquare(). In the method printSquare() you MUST use System.out.printf() for output.

You will then call the method checkSquare(). This method checks that the sum of all the rows have the same value and prints out that sum. It checks that the sum of all the columns have the same value and prints out that sum. It sums the two main diagonals and prints out the sum. For a magic square of size n, the sum is n * (n2 + 1) / 2.

This is a sample of what the program will output:

Please enter an odd number: 5

Here is a 5 x 5 magic square:

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

Sum of row = 65
Sum of column = 65
Sum diagonal (UL to LR) = 65
Sum diagonal (UR to LL) = 65

The file that you will be turning in will be called TestMagicSquare.java. The file will have a header of the following form:

/*
  File: TestMagicSquare.java

  Description:

  Student's Name:

  Student's UT EID:
 
  Course Name: CS 313E 

  Unique Number: 54090

  Date Created:

  Date Last Modified:

*/

You will follow the standard Java Coding Conventions. You can either view the HTML page or download the PDF or Postscript and print it out. There is a modification that I would like to make to the standard coding conventions. Please align the opening and closing braces vertically so that you can easily make out the blocks of code. For example:

Do this:
if ( x > 5 )
{
  a = b + c;
}

Not this:
if ( x > 5 ) {
  a = b + c;
}

Use the turnin program to submit your TestMagicSquare.java file. The TAs should receive your work by 5 PM on Friday, 12 February 2010. We will be looking for clean logic and good documentation. There will be substantial penalties if you do not adhere to the guidelines.

References

  1. Magic Square Article in Wolfram MathWorld
  2. Article on Magic Square in Wikipedia