java Magic Square

Click Here to Download this Answer Instantly

import objectdraw.*;
import java.awt.*;
public class MagicSquareClient extends WindowController {
public static void main(String[] args) {
new MagicSquareClient().startController( 550, 100 );
}
private int odd=1;
private int magic[][];
public void begin() {
Text msg = new Text( “Press anywhere to see some magic…
over in terminal!”
, 10, 10, canvas
);
msg.setFontSize(20);
msg.setColor( new Color(0x9933CC) );
magic = new int [ odd ][ odd ];
// fill entire array with -1

// ?0 write loop(s) to print out all values in array magic
// one row per line. format your output so it all “lines
up” in columns
// middle of **TOP** row
int row= ?3? ;
int col= ?4? ;
// “current cell” is always specified by row & col indices
for ( int num= ?5? ; num <= ?6? ; num ++ ){
// ?7 place current num value in current cell

// ?8 set loop local variable to row number **UP** from
current row
// ?9 set loop local variable to col number **RIGHT**
from current col
// ?10 if possible cell is still negative
// update row & col
// else
// update row to row **BELOW** where last number went
}
// ?12 write loop(s) to print out all values in array magic
}
public void onMousePress( Location p ) {
System.out.printf(“\nBehold the MAGIC:”);
// comment out loops you wrote for 0? & 12?
// put one of them here instead!
// DO NOT MODIFY!!!
odd = (odd+2) % 8;
begin();
}

}
Magic Squares – Alternative Rules for Building
• 2D arrays and initializing a ‘magic square’
• uses WindowController only to
… step through magic squares of various size on terminal
In your textbook is a section which discusses how to build a ‘magic square’ worth of integers.
Here’s a link to a summary of both this method and the Dale ‘Variant Rules’.
You will use the ‘Variant Rules’ so… read instructions in download code and
do not get over-invested in trying to remember and recreate code from the
book! The code above is the starting code
Here is the Eventful’s Rules
One method for constructing magic squares of odd order involves placing the next number of the magic square
diagonally down and to the right of the previous number, with the following caveats:
1. Begin by placing the number 1 in the middle of the bottom row.
2. Always assign the next number down and to the right from the current number.
3. UNLESS YOU CAN’T IN WHICH CASE:
a. One row “down” from the bottom row lands you in the top row.
b. One column “right” from the rightmost column lands you in the leftmost column.
c. If the desired cell is already occupied, then instead place the next
number above the previous one.
But we need to do it in the Variant Rules
Switches up & down, bottom & top, above & below:
One method for constructing magic squares of odd order involves placing the next number of the magic square diagonally up and to the left of the previous number, with the following caveats:
1. Begin by placing the number 1 in the middle of the top row.
2. Always assign the next number up and to the left from the current number.
3. UNLESS YOU CAN’T IN WHICH CASE:
a. One row “up” from the top row lands you in the bottom row.
b. One column “left” from the leftmost column lands you in the rightmost column.
c. If the desired cell is already occupied, then instead place the next
number below the previous one.
The expert’s code that needs to actually fit the other code:
package javatest;