The Square
class works with an n
× n
matrix of integers, numbers 1, 2, 3, ..., n^2
and identifies whether or not the matrix is a "magic square."
A square is magic if the sum of the elements in each row, in each column, and in the two diagonals is the same value.
Consider this matrix of values, for example. The sum of each column, each row, and each diagonal is 34
.
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Your MagicSquare
class will need to test three features:
Did the user enter n^2
numbers for some n
?
Do each of the numbers 1, 2, ..., n^2
occur exactly once in the user input?
When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?
If the size of the input is a square, test whether all numbers between 1 and n^2 are present. Then compute the row, column, and diagonal sums.
The Square
class you implement will have:
public void add(int i)
- Called sequentially to add numbers into a list that will eventually be used to create a square of data.public boolean isMagic()
- Returns true
if the square created from the values is magic, otherwise returns false
.You may wish to write additional "helper" methods if they would be useful in identifying if a square is magic.