//import MagicSquare;public class OddOrderMagicSquare extends MagicSquare {
public OddOrderMagicSquare(int startValue, int n) {
super(startValue, n);
// start from the middle cell in row 0 int index = n/2; setCell(index, startValue); for (int i=startValue+1; i<startValue+n*n; i++) { index = getIndex(index); setCell(index, i); }}
public int getIndex(int lastIndex)
{int index; int n = getOrder(); int offset; if (lastIndex % n == n-1) { // last cell in last column offset = n*n-2*n+1; } else { // last cell not in last column offset = n*n-1*n+1; } index = (lastIndex+offset) % (n*n); if (isCellFilled(index)) { // cell is directly below last cell index = (lastIndex+n) % (n*n); } return index;}
public static void main(String[] args) {
MagicSquare magicSquare; for (int i=1; i<10; i++) { int n = 2*i+1; System.out.println/(n+" by "+n+" magic square:"); magicSquare = MagicSquareFactory.create(1, n); System.out.println/(magicSquare); }}
}