ITEC 120 Back to Assignment index page Assignment 4 - Magic Square Due Wed, April 14, midnight. Submit your program to the RU09 folder in your directory on neelix.

Description:  You'll write a class to represent an n x n matrix (square) of numbers, and inlcude methods to determine if the matrix qualifies as a magic square.

A magic square is an arrangement of the numbers from 1 to n^2 (n-squared) in an n x n matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same.

The simplest magic square is the 1x1 magic square whose only entry is the number 1.

The next simplest is the 3x3 magic square

Notice that numbers 1 through 9 appear once and only once in each cell of the square. Add up any row, column, or diagonal and the sum is 15.

There are 4x4 magic squares and higher.

The Square class:
This class represents a square which will be filled with numbers.

 Square - square : int[ ][ ] - n : int + Square(int n) + readsquare() : void + isMagic() : boolean - nums1toNsquared() : boolean - sumNW_SE_Diag() : int - sumNE_SW_Diag() : int - sumRow( r : int) : int - sumCol( c : int) : int + toString() : String

The instance data:

• square is a 2d array of ints representing the matrix.
• n is the size of one side of the matrix (for n=3, the matrix will be 3x3. numbers 1 thru 9 will be in each cell of the matrix.)

The methods:

• constructor - creates the 2d array of size n x n. n is a parameter, passed in.
• isMagic - returns true if the square is magic, false otherwise.
• nums1toNsquared - returns true if the square contains each of the numbers 1 to n squared, once
• sumNW_SE_Diag - returns the integer sum of the northwest/southeast diagonal
• sumNE_SW_Diag - returns the integer sum of the northeast/southwest diagonal
• sumRow - returns the integer sum of row r
• sumCol - returns the integer sum of column c
• toString () - returns the square for printing

The driver:
The driver will ask the user what size (n) the matrix will be, create a square, then call the readsquare method to read in all the elements. The driver will print out the square, tell whether it is a magic square or not, then ask the user if they'd like to enter another square. (Use y or n so that the input file will work.)

Study the following run carefully. This input for this example is in this file: p4in.txt.

Here's a sample run:

 Program outputs: User inputs: What size square? 1 Enter num for row 1 col 1: 1 |-------| |   1   | |-------| It's a magic square! Enter another? y/n y What size square? 3 Enter num for row 1 col 1: Enter num for row 1 col 2: Enter num for row 1 col 3: Enter num for row 2 col 1: Enter num for row 2 col 2: Enter num for row 2 col 3: Enter num for row 3 col 1: Enter num for row 3 col 2: Enter num for row 3 col 3: 8 1 6 3 5 7 4 9 2 |-------|-------|-------| |   8   |   1   |   6   | |-------|-------|-------| |   3   |   5   |   7   | |-------|-------|-------| |   4   |   9   |   2   | |-------|-------|-------| It's a magic square! Enter another? y/n y What size square? 2 Enter num for row 1 col 1: Enter num for row 1 col 2: Enter num for row 2 col 1: Enter num for row 2 col 2: 4 2 3 1 |-------|-------| |   4   |   2   | |-------|-------| |   3   |   1   | |-------|-------| Sorry, not a magic square! Enter another? y/n y What size square? 3 Enter num for row 1 col 1: Enter num for row 1 col 2: Enter num for row 1 col 3: Enter num for row 2 col 1: Enter num for row 2 col 2: Enter num for row 2 col 3: Enter num for row 3 col 1: Enter num for row 3 col 2: Enter num for row 3 col 3: 2 2 2 2 2 2 2 2 2 |-------|-------|-------| |   2   |   2   |   2   | |-------|-------|-------| |   2   |   2   |   2   | |-------|-------|-------| |   2   |   2   |   2   | |-------|-------|-------| Sorry, not a magic square! Enter another? y/n n

NOTES : This is a non-trivial program which cannot be done at the last minute. It's also a lot of fun if you like problem solving. Advice: Use incremental design. Write a class with a Constructor, the readSquare method, and the toString(); Write a driver which instantiates a square and prints it out. Get this to work before starting on the isMagic stuff. The isMagic stuff is the most difficult part of the program, which is why it is broken down into several private methods. Work on it bit by bit, and put temporary printlns inside your methods so you can see if the methods are calculating sums correctly. The hardest method to write is nums1toNsquared.

Grading : The highest grade a non-compiling program can earn will be 30.
A program which reads and prints a square may earn up to 50.
The hardest method, nums1toNsquared, will be worth 10 points.

TURNING IN YOUR PROGRAM: You will turn in two files. You'll have the driver program and your Square class file. Submit both files to neelix in the appropriate folder.