A magic square is a square matrix (N rows by N columns) with the following prope
ID: 3587189 • Letter: A
Question
A magic square is a square matrix (N rows by N columns) with the following properties:
1. It contains the numbers 1 through N2, with each number appearing exactly once.
2. The sums of each row, each column, and both main diagonals are equal.
For example, here is a "3 by 3" magic square:
4 9 2
3 5 7
8 1 6
The assignment is to create a class to model a magic square and a class that tests it.
I. The MagicSquare Class
Your magic square class constructor will have a parameter for N (the number of rows and columns) and will call a private “utility” method that fills the square with the integers 1 to N.
Your class will contain 2 additional methods:
A method that returns a value of true or false indicating whether the square created is indeed magic. That is, whether all the sums – those of each individual row and column and of both diagonals – are equal. (Hint: for an N by N matrix, how many sums must be tested?)
A toString method that returns the magic square as a multi-line string, as shown above.
II. The Test Class
Your test class will get the number of rows and columns from the user, create a magic square object, and call the method that tests whether the square is truly magical. If so, it will call the toString method and print the square; otherwise, an error message should be printed.
Use a loop to ensure that your program accepts only positive, odd-numbered values for N
III. Algorithm
Fortunately, there is a straightforward algorithm to create an "N by N" magic square, where N is an odd number:
1. Place a 1 in the middle column of the last row.
2. Place the integers from 2 to N2 into the matrix in succession, according to these rules:
Attempt to place each number in a position that is one row below and one column to the right of the previous number.
If the attempt leads to a non-existent row, then the number is placed instead in the first row, one column to the right.
If the attempt leads to a non-existent column, then the number is placed instead in the first column, one row down.
If the attempt leads to a non-existent row and a non-existent column, then the number is placed instead in the position directly above the previous number (i.e., same column, previous row).
If the attempt leads to a position that is not empty (i.e., already contains a value), then the number is placed instead in the position directly above the previous number.
No credit will be given for programs that use any algorithm to generate the square other than the one described above
Explanation / Answer
Sorry. Running out of time. Here is the code for MagicSquareClass.java:
class MagicSquareClass
{
int size;
int[][] square;
public MagicSquareClass(int size)
{
this.size= size;
square = new int[size][size];
}
private void initSquare()
{
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
square[i][j] = 0;
square[size-1][size/2] = 1;
}
private boolean isSquareNotFull()
{
for(int i = 0; i < size; i++)
for(int j = 0; j < size; j++)
if(square[i][j] == 0)
return true;
return false;
}
private boolean isInvalidPos(int pos)
{
return pos < 0 || pos >= size;
}
private boolean isEmptyCell(int row, int col)
{
return square[row][col] == 0;
}
private void fillSquare()
{
initSquare();
int currentRowPos = size-1;
int currentColPos = size/2;
int currentValue = 1;
while(isSquareNotFull())
{
int nextRowPos = currentRowPos++;
int nextColPos = currentColPos++;
currentValue++;
if(isInvalidPos(nextRowPos) && isInvalidPos(nextColPos))
{
nextRowPos = currentRowPos-1;
}
if(isInvalidPos(nextRowPos))
{
nextRowPos = 0;
}
if(isInvalidPos(nextColPos))
{
nextColPos = 0;
}
if(isEmptyCell(nextRowPos, nextColPos))
{
square[nextRowPos][nextColPos] = currentValue;
currentRowPos = nextRowPos;
currentColPos = nextColPos;
}
}
}
public boolean isMagicSquare()
{
int sum = 0;
for(int i = 0; i < size; i++) //First column sum.
sum += square[i][0];
for(int i = 1; i < size; i++) //For each other columns.
{
int newSum = 0;
for(int j = 0; j < size; j++) //Sum of column i.
newSum += square[j][i];
if(sum != newSum) //If the column sum is not equal to should be sum.
return false; //Conclude is not magic square.
}
for(int i = 0; i < size; i++) //For each row.
{
int newSum = 0;
for(int j = 0; j < size; j++) //Sum of row i.
newSum += square[i][j];
if(sum != newSum) //If the row sum is not equal to should be sum.
return false; //Conclude is not magic square.
}
int newSum = 0;
for(int i = 0; i < size; i++) //Leading diagonal sum.
newSum += square[i][i];
if(sum != newSum) //If leading diagonal sum is not equal to should be sum.
return false; //Conclude is not magic square.
newSum = 0;
for(int i = size-1; i >= 0; i--) //Trialing diagonal sum.
newSum += square[i][size-i-1];
if(sum != newSum) //If trialing diagonal sum is not equal to should be sum.
return false; //Conclude is not magic square.
return true; //Conclude is magic square.
}
public String toString()
{
String temp = "";
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
temp += " " + square[i][j];
temp += " ";
}
temp += " ";
return temp;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.