public abstract class MagicSquare
{

private int startValue;
private int n;
private Integer[][] cells;

public MagicSquare(int startValue, int n) throws IllegalArgumentException {

        if (n < 3)
            throw new IllegalArgumentException();

        this.n = n;
        this.startValue = startValue;
        cells = new Integer[n][n];

}

public int getStartValue()
{

return startValue;
}

public int getOrder()
{

return n;
}

public boolean isCellFilled(int row, int col) {

return (cells[row][col] != null); }

public boolean isCellFilled(int index) {

return isCellFilled(index/n, index%n); }

public int getCell(int row, int col) {

return cells[row][col].intValue(); }

public int getCell(int index)
{

return getCell(index/n, index%n); }

public int getComplementaryCell(int row, int col) {

return getCell(n-row, n-col);
}

public int getComplementaryCell(int index) {

return getComplementaryCell(index/n, index%n); }

public void setCell(int row, int col, int value) {

cells[row][col] = new Integer(value); }

public void setCell(int index, int value) {

setCell(index/n, index%n, value); }

public String toString()
{

StringBuffer sb = new StringBuffer();

        for (int row=0; row<n; row++)
        {
            for (int col=0; col<n; col++)
            {
                sb.append(getCell(row, col));
                sb.append(" ");
            }
            sb.setCharAt(sb.length()-1, '\n');
        }
        return sb.toString();

}
}