0.0 General‎ > ‎Assignments‎ > ‎Magic Squares‎ > ‎

Magic Squares: OOPs

This is a framework you can use to implement magic squares in an OOPs environment:

Class: Cell

Cell Attributes:

 AttributeData Type Scope Purpose 
 row integer - private  the row the cell is in
 column integer - private  the column the cell is in
 isOccupied boolean - private indicates if the cells contains a value.
set to false when created
 value integer - private  the integer value of the cell in the magic square

Cell Methods:

 MethodReturn Type  Scope Purpose
 Cell( int r,
    int c)
  + public Constructor: sets isOccuppied to false, row to r, col to c  
 isOccupied() boolean + public return the current value of isOccupied
    int value)
 void + public set the value of the cell
 getValue() int + public return the current cell value

Class: MagicSquare

Square Attributes:

 AttributeData Type Scope Purpose 
 mSquareCell [][] - privatea 2D array to hold the magic square
 sizeint- privatethe size of the magic square   
 startValueint- private the first number start value in the square
 stepint - privatethe amount to change each new value in the magic square



Square Methods:

MethodReturn Type  Scope Purpose
Square()  + public Constructor: set the default values
    int size,
    int start,
    int step)
void - private  called by the constructors to create the magic square
 initsquare()void used by: square()
 creates mSquare full of empty cells
setValues()void - private used by: square()
  sets the values in magic square
displaySquare()void - privateused by: square()
  will display the square neatly on the screen 
  Cell currentCell)
Cell - privatereturn the next cell to be occupied
used by: setValues()
    int x)
int - private returns the next value to be placed in a cell
used by: setValues()