3.3 Java OOPs - Magic Squares

OOPs Implementation of Magic Squares

This program will impletment the general Magic Squares problem with objects.

At least two classes will be required. A Cell class, and a MagicSquare class.  Cell class is responsible for keeping track of what, if anything is in any one square that makes up the magicSquare.  The MagicSquare class is responsible for correctly filling the all of the individual squares that make up the magic square.

You will need to look over the note on Java: 2D Arrays

Things to notice:
in the instruction for magic square constructor you are told to set this.size with size 
notice that there are two variables size available to the constructor. One is a parameter the other is an instance variable of the class (aka an attribute).  The this. after size forces Java to use the instance variable, while just size will use value of the parameter within the method.

Cell class

HAS-A:  attributes
    value:int    the number to display in the magic square
    isOccupied: boolean    is true when the number has been set, otherwise false
    row:int    the row of the cell in the magic square
    col:int    the column of the cell in the magic square
RESPONDS-TO: methods or actions
constructor, two parameters are required, the row and column the cell to indicate the position in the magic square.
Cell(int r,int c)
    sets row to r
    sets col to c
    sets isOccupied to false

public boolean isOccupied()
    returns the value isOccupied.

public void setValue(int n)
    sets value to n
    set isOccupied to true

public int getRow()
    returns the row

pubic int gerCol()
    returns the col

public String toString()
    returns a string with length of 7.  It will include the number followed by 2 spaces.  Extra spaces will be added to the front to make a String with a length of 7

MagicSquare class

HAS-A:  attributes
    size:int    the size of the magic square; must be odd
    startNumber: int    the starting number 
    increment:int    the amount to add each time.
    magicSq[][]:cells    a 2-d array of Cells

RESPONDS-TO:  methods or actions
MagicSquare(size, start, step)
  • sets startNumber with start
  • sets increment with step
  • sets this.size with size
  • initMagicSq()
You will need to ensure that the value of size is an odd number and that the size is 5 or larger.
this.size = (size/2)*2 +1;     // ensure that the size is odd.
Why does this work?

    creates a 2-D array of Cells of any given size, 
    creates a cell for each location in the magicSq 

    Local variables required

  1. set range to size * size
  2. set the curentCell to the first cell required.
  3. for each number x in the range
    1. set the currentCell's value to the nextValue(x)
    2. set the currentCell to the nextCell(currentCell)

helper methods for setMagicSquare()

nextValue(int x)
    parameter required:  the current position in the range.
    returns the next value to put in the cell ( startNumber + increment *x)

nextCell(Cell current)
    returns the next current cell
    Local variables
    r: int -- working field for the row
    c: int -- working field for the col

  1. set r and c to the cells row and col
  2. decrease r by 1
  3. increase c by 1
  4. if r is less than 0 set r to size -1
  5. if c is greater or equal to size set c to 0
  6. if the cell at the r,c isOccupied set r to current.getRow() +1 and set c to current.gerCol()
  7. if r greater than or equal to the size of the square, sets r to 0
  8. return magicSq[r][c]