s3a
April 3rd, 2010, 10:24 PM
Here is the driver code:
package assignment_3a;
public class MagicSquareDemo
{
public static void main(String[] args)
{
MagicSquare m1 = new MagicSquare(5);
System.out.println(m1);
MagicSquare m2 = new MagicSquare(-5); // negative order
System.out.println(m2);
MagicSquare m3 = new MagicSquare(10); // even order
System.out.println(m3);
}
}
Here is the (full) MagicSquare class (I bolded the line that is causing my troubles):
// Author:
// Purpose: To generate magic squares of a certain desired order
package assignment_3a;
public class MagicSquare
{
private int order; // the order of this magic square
private int[][] msArray; // reference to array storage // me: magic square array
// A constructor that accepts an int n n as the order of the magic square.
// Sets order to 3 if n < 3; otherwise, sets order to n+1 if n is even;
// otherwise, sets order to n.
// Finally, allocates an int array of order rows and order columns.
public MagicSquare(int n)
{
if (n < 3) // Is n bellow 3?
{
System.out.println("This program cannot generate magic squares of negative orders like: " + n);
System.out.println("The default order 3 is assumed for this magic square.");
order = 3;
}
else if (n % 2 == 0) // Is n even?
{
System.out.println("This program cannot generate magic squares of even orders like: " + n);
System.out.println("The order " + (n+1) + " is assumed for this magic square.");
order = n + 1;
}
else // n >= 3 where n is an odd number
{
order = n;
}
// We now have a valid order n. Let's allocate space for a square array
msArray = new int[order][order];
doMagic();
}
public void doMagic()
{
int row = 0;
int column = order/2;
int k = 1;
while(k <= order*order)
{
msArray[row][column] = k;
k+=1;
--row;
++column;
if(row == -1 && column == order)
{
row = 1;
column = order - 1;
}
if(row == - 1)
{
row = order - 1;
}
if(column == order)
{
column = 0;
}
if(msArray[row][column] != 0) // Checks if it's occupied
{
row+=2;
--column;
}
}
}
// Returns the order of this magic square.
public int getOrder()
{
return order;
}
public int magicNumber()
{
return (order * order * order + order)/2; // (n^3 + n)/2 formula
}
// Returns the sum of the values (of the columns) in the iˆth row cell(s).
private int rowSum(int row)
{
int total = 0;
for(int column = 0; column < msArray[row].length; column++)
{
total += msArray[row][column];
}
return total;
}
// Determines whether the row sums are each equal to the magic number.
private boolean checkRowSums()
{
if(rowSum(order) == magicNumber())
{
return true;
}
else
{
return false;
}
}
// Returns the sum of the values (of the rows) in the jˆth column cells.
private int columnSum(int column)
{
int total = 0;
for(int row = 0; row < msArray.length; row++)
{
total += msArray[row][column];
}
return total;
}
// Determines whether the column sums each are equal to the magic number.
private boolean checkColumnSums()
{
if(columnSum(order) == magicNumber())
{
return true;
}
else
{
return false;
}
}
//Returns sum of the values in the primary diagonal cells.
private int primaryDiagonalSum() // Could have a bug but I can't see one as of now
{
int row = 0;
int total = 0;
for(int column = 0; column < msArray[row].length; column++)
{
total+=msArray[row][column];
row+=1;
}
return total;
}
//Returns sum of the values in the secondary diagonal cells.
private int secondaryDiagonalSum()
{
int total = 0;
int row = (msArray.length - 1);
for(int column = row; column >= 0; column--)
{
total+=msArray[row][column];
row-=1;
}
return total;
}
// Determines whether the diagonal sums each are equal to the magic number.
private boolean checkDiagonalSums()
{
if(magicNumber() == primaryDiagonalSum() && magicNumber() == secondaryDiagonalSum())
{
return true;
}
else
{
return false;
}
}
// Determines whether the contents of the array represents a magic square.
public boolean isMagic()
{
if(checkRowSums() && checkColumnSums() && checkDiagonalSums())
{
return true;
}
else
{
return false;
}
}
public String toString()
{
String result="Magic square of order " + order + "\n" +
"=========================\n\n";
for(int row = 0; row < msArray.length; ++row) // scans rows
{
for(int column = 0; column < msArray[row].length; ++column) // scan columns
{
result = result + String.format("%5d", msArray[row][column]);
}
result = result + "\n"; // prepare to skip line
}
//if(this.isMagic())result += "\nStatus: Valid";
result += "\nMagic number: " + magicNumber() + "\n";
return result;
}
public boolean equals(Object obj)
{
boolean result = false;
if(this == obj) return true;
if(obj == null) return false;
if(this.getClass() != obj.getClass()) return false;
MagicSquare otherObject = (MagicSquare)obj;
if(otherObject.isMagic() == this.isMagic() && otherObject.order == this.order)
{
result = true;
}
return result;
}
}
Any input would be greatly appreciated!
Thanks in advance!
package assignment_3a;
public class MagicSquareDemo
{
public static void main(String[] args)
{
MagicSquare m1 = new MagicSquare(5);
System.out.println(m1);
MagicSquare m2 = new MagicSquare(-5); // negative order
System.out.println(m2);
MagicSquare m3 = new MagicSquare(10); // even order
System.out.println(m3);
}
}
Here is the (full) MagicSquare class (I bolded the line that is causing my troubles):
// Author:
// Purpose: To generate magic squares of a certain desired order
package assignment_3a;
public class MagicSquare
{
private int order; // the order of this magic square
private int[][] msArray; // reference to array storage // me: magic square array
// A constructor that accepts an int n n as the order of the magic square.
// Sets order to 3 if n < 3; otherwise, sets order to n+1 if n is even;
// otherwise, sets order to n.
// Finally, allocates an int array of order rows and order columns.
public MagicSquare(int n)
{
if (n < 3) // Is n bellow 3?
{
System.out.println("This program cannot generate magic squares of negative orders like: " + n);
System.out.println("The default order 3 is assumed for this magic square.");
order = 3;
}
else if (n % 2 == 0) // Is n even?
{
System.out.println("This program cannot generate magic squares of even orders like: " + n);
System.out.println("The order " + (n+1) + " is assumed for this magic square.");
order = n + 1;
}
else // n >= 3 where n is an odd number
{
order = n;
}
// We now have a valid order n. Let's allocate space for a square array
msArray = new int[order][order];
doMagic();
}
public void doMagic()
{
int row = 0;
int column = order/2;
int k = 1;
while(k <= order*order)
{
msArray[row][column] = k;
k+=1;
--row;
++column;
if(row == -1 && column == order)
{
row = 1;
column = order - 1;
}
if(row == - 1)
{
row = order - 1;
}
if(column == order)
{
column = 0;
}
if(msArray[row][column] != 0) // Checks if it's occupied
{
row+=2;
--column;
}
}
}
// Returns the order of this magic square.
public int getOrder()
{
return order;
}
public int magicNumber()
{
return (order * order * order + order)/2; // (n^3 + n)/2 formula
}
// Returns the sum of the values (of the columns) in the iˆth row cell(s).
private int rowSum(int row)
{
int total = 0;
for(int column = 0; column < msArray[row].length; column++)
{
total += msArray[row][column];
}
return total;
}
// Determines whether the row sums are each equal to the magic number.
private boolean checkRowSums()
{
if(rowSum(order) == magicNumber())
{
return true;
}
else
{
return false;
}
}
// Returns the sum of the values (of the rows) in the jˆth column cells.
private int columnSum(int column)
{
int total = 0;
for(int row = 0; row < msArray.length; row++)
{
total += msArray[row][column];
}
return total;
}
// Determines whether the column sums each are equal to the magic number.
private boolean checkColumnSums()
{
if(columnSum(order) == magicNumber())
{
return true;
}
else
{
return false;
}
}
//Returns sum of the values in the primary diagonal cells.
private int primaryDiagonalSum() // Could have a bug but I can't see one as of now
{
int row = 0;
int total = 0;
for(int column = 0; column < msArray[row].length; column++)
{
total+=msArray[row][column];
row+=1;
}
return total;
}
//Returns sum of the values in the secondary diagonal cells.
private int secondaryDiagonalSum()
{
int total = 0;
int row = (msArray.length - 1);
for(int column = row; column >= 0; column--)
{
total+=msArray[row][column];
row-=1;
}
return total;
}
// Determines whether the diagonal sums each are equal to the magic number.
private boolean checkDiagonalSums()
{
if(magicNumber() == primaryDiagonalSum() && magicNumber() == secondaryDiagonalSum())
{
return true;
}
else
{
return false;
}
}
// Determines whether the contents of the array represents a magic square.
public boolean isMagic()
{
if(checkRowSums() && checkColumnSums() && checkDiagonalSums())
{
return true;
}
else
{
return false;
}
}
public String toString()
{
String result="Magic square of order " + order + "\n" +
"=========================\n\n";
for(int row = 0; row < msArray.length; ++row) // scans rows
{
for(int column = 0; column < msArray[row].length; ++column) // scan columns
{
result = result + String.format("%5d", msArray[row][column]);
}
result = result + "\n"; // prepare to skip line
}
//if(this.isMagic())result += "\nStatus: Valid";
result += "\nMagic number: " + magicNumber() + "\n";
return result;
}
public boolean equals(Object obj)
{
boolean result = false;
if(this == obj) return true;
if(obj == null) return false;
if(this.getClass() != obj.getClass()) return false;
MagicSquare otherObject = (MagicSquare)obj;
if(otherObject.isMagic() == this.isMagic() && otherObject.order == this.order)
{
result = true;
}
return result;
}
}
Any input would be greatly appreciated!
Thanks in advance!