Java

  1. Home
  2. Computing & Technology
  3. Java
photo of Paul Leahy

Paul's Java Blog

By Paul Leahy, About.com Guide to Java

Answer to Monday's Odd Magic Square Question

Sunday November 9, 2008

On Monday the programming question was about odd magic squares (e.g., 3x3, 5x5, 7x7). The Java program had to position sequential numbers in a square so that the rows, columns and diagonals all add up to the same number.

More specifically it needed to take an input from the user for an odd number and use two methods – one to create the magic square and one to display it. All being well your program should be able to display the 5x5 magic square:

17 24  1   8 15
23  5   7 14 16
 4   6 13 20 22
10 12 19 21  3
11 18 25  2   9

Here's my version:

import java.util.Scanner;
public class MagicOddSquare {

  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[][] magicSquare;
    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)
      {
        System.out.println("The size must be an odd number");
        isAcceptableNumber = false;
      }
      else
      {
        isAcceptableNumber = true;
      }
    }

    magicSquare = createOddSquare(size);
    displaySquare(magicSquare);
  }

  private static int[][] createOddSquare(int size)
  {
    int[][] magicSq = new int[size][size];
    int row = 0;
    int column = size/2;
    int lastRow = row;
    int lastColumn = column;
    int matrixSize = size*size;

    magicSq[row][column]= 1;
    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 (magicSq[row][column] == 0)
      {
        magicSq[row][column] = k;
      }
      else
      {
        row = lastRow;
        column = lastColumn;
        if (row + 1 == size)
        {
          row=0;
        }
         else
        {
          row++;
        }
        magicSq[row][column] = k;
      }
      lastRow = row;
      lastColumn= column;
    }
    return magicSq;
  }

  private static void displaySquare(int[][] magicSq)
  {
    int magicConstant = 0;
    for (int j=0;j<(magicSq.length);j++)
    {
      for (int k=0;k<(magicSq[j].length);k++)
      {
        System.out.print(magicSq[j][k] + " ");
      }
      System.out.print("\n");
      magicConstant = magicConstant + magicSq[j][0];
    }
     System.out.print("The magic constant is " + magicConstant);
  }
}

Comments

No comments yet. Leave a Comment

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss

Community Forum

Explore Java

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Java

  1. Home
  2. Computing & Technology
  3. Java

©2009 About.com, a part of The New York Times Company.

All rights reserved.