Class MagicSquare


  • class MagicSquare
    extends java.lang.Object
    Generates a magic square of odd size.
    Author:
    Kabir Kanha Arora
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static int expectedSum  
      private int mainCnt  
      private int[][] matrix  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private MagicSquare​(int n)
      Private constructor for the Magic square.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static int getExpectedSum()
      Returns the expected sum of every diagonal, row and column of the Magic Square.
      (package private) static int[][] getMagicSquare​(int n)
      Calculates and returns a new magic square object.
      private void placeNextOdd​(int r, int c, int[][] matrix, int n, int max)
      Recursive method for placing digits in the magic square.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mainCnt

        private int mainCnt
      • matrix

        private int[][] matrix
      • expectedSum

        private static int expectedSum
    • Constructor Detail

      • MagicSquare

        private MagicSquare​(int n)
        Private constructor for the Magic square.
        Parameters:
        n - the size of the magic square to be generated.
    • Method Detail

      • getExpectedSum

        static int getExpectedSum()
        Returns the expected sum of every diagonal, row and column of the Magic Square.
        Returns:
        the expected sum of every diagonal, row and column of the Magic Square.
      • getMagicSquare

        static int[][] getMagicSquare​(int n)
        Calculates and returns a new magic square object. The objects produced by this methods are isomorphic to one another but are not eht same objects.
        Parameters:
        n - the size of the magic square to be generated. Even values do not produce expected results.
        Returns:
        a new magic square
      • placeNextOdd

        private void placeNextOdd​(int r,
                                  int c,
                                  int[][] matrix,
                                  int n,
                                  int max)
        Recursive method for placing digits in the magic square.
        Parameters:
        r - the row index
        c - the column index
        matrix - matrix that will eventually become a magic square
        n - the size of the magic square to be constructed
        max - the maximum possible value in the magic square.