Answer to Monday's Doubly-Even Magic Square Question
Monday's programming question was a continuation of the Java program used for the odd magic squares question. I posted a method to create doubly-even magic squares and the task was to modify the old program. It needed to only accept odd or doubly-even numbers from the user, make either kind of magic square, and display it to the user along with its magic constant number.
More specifically the program had to be able to make a 8x8 magic square as shown below:
1 2 62 61 60 59 7 8
9 10 54 53 52 51 15 16
48 47 19 20 21 22 42 41
40 39 27 28 29 30 34 33
32 31 35 36 37 38 26 25
24 23 43 44 45 46 18 17
49 50 14 13 12 11 55 56
57 58 6 5 4 3 63 64
Here's my version of the program:
import java.util.Scanner;
public class MagicSquare {
private static int[][] magicSquare;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean isAcceptableNumber = false;
int size = -1;
while (isAcceptableNumber == false)
{
System.out.println("Enter in size of square: ");
String sizeText = input.nextLine();
size = Integer.parseInt(sizeText);
if ((size % 2 == 0) && (size % 4 !=0))
{
System.out.println("The size must be an odd number or an even number divisible by 4");
isAcceptableNumber = false;
}
else
{
isAcceptableNumber = true;
}
}
if (size % 4 == 0)
{
createDoublyEvenSquare(size);
}
else
{
createOddSquare(size);
}
displaySquare();
}
private static void createOddSquare(int size)
{
magicSquare = new int[size][size];
int row = 0;
int column = size/2;
int lastRow = row;
int lastColumn = column;
magicSquare[row][column]= 1;
int matrixSize = size*size;
for (int k=2;k < matrixSize+1;k++)
{
//check if we need to wrap to
//opposite row
if (row - 1 < 0)
{
row = size-1;
}
else
{
row--;
}
//check if we need to wrap
//to opposite column
if (column + 1 == size)
{
column = 0;
}
else
{
column++;
}
if (magicSquare[row][column] == 0)
{
magicSquare[row][column] = k;
}
else
{
row = lastRow;
column = lastColumn;
if (row + 1 == size)
{
row=0;
}
else
{
row++;
}
magicSquare[row][column] = k;
}
lastRow = row;
lastColumn= column;
}
}
private static void createDoublyEvenSquare(int size)
{
magicSquare = new int[size][size];
int countUpNumber = 1;
StringBuilder usedNumbers = new StringBuilder();
int miniSquareNumber = size/4;
for (int j=0;j < magicSquare.length;j++)
{
//populate the corner mini-squares
if ((jmagicSquare.length-1-miniSquareNumber))
{
for (int k=0;k < miniSquareNumber;k++)
{
magicSquare[j][k] = countUpNumber;
usedNumbers.append("/"+countUpNumber+"/");
countUpNumber++;
}
countUpNumber = countUpNumber+(miniSquareNumber+miniSquareNumber);
for (int k=magicSquare.length-miniSquareNumber;k{
magicSquare[j][k] = countUpNumber;
usedNumbers.append("/"+countUpNumber+"/");
countUpNumber++;
}
}
else //populate the large center square
{
countUpNumber = countUpNumber+miniSquareNumber;
for (int k=miniSquareNumber;k{
magicSquare[j][k] = countUpNumber;
usedNumbers.append("/"+countUpNumber+"/");
countUpNumber++;
}
countUpNumber = countUpNumber+miniSquareNumber;
}
}
int countDownNumber = size*size - miniSquareNumber;
//populate remaining squares
for (int l=0;l<(magicSquare.length);l++)
{
for (int m=0;m<(magicSquare[l].length);m++)
{
if (magicSquare[l][m] == 0)
{
while (usedNumbers.indexOf("/"+countDownNumber+"/") != -1)
{
countDownNumber--;
}
magicSquare[l][m] = countDownNumber;
countDownNumber--;
}
}
}
}
private static void displaySquare()
{
int magicConstant = 0;
for (int j=0;j<(magicSquare.length);j++)
{
for (int k=0;k<(magicSquare[j].length);k++)
{
System.out.print(magicSquare[j][k] + " ");
}
System.out.print("\n");
magicConstant = magicConstant + magicSquare[j][0];
}
System.out.print("The magic constant is " + magicConstant);
}
}


computer’s brain will get puzzled running this program
No one notice? jajaj
for (int k=0;k
{
magicSquare[j][k] = countUpNumber;
usedNumbers.append(”/”+countUpNumber+”/”);
countUpNumber++;
}
Good spot Jose. I’d like to claim that was a deliberate mistake to see if anyone was paying attention, but the truth is that for loops can get truncated by wordpress if I don’t format them a certain way. It’s fixed now. Thanks