Magic Square Lab

A magic square is a square with 9 positions (like a tic-tac-toe board). Each position contains a number. The sum of the numbers in each row, column, and diagonal are the same. Given the numbers in the first row, you can compute the remaining numbers by using established formulas. When picking the numbers in the first row, it is best to pick them so that their sum is a multiple of 3. This lab uses the Magic Square to introduce you to programming on the SGI computers. Good Luck.

1. Double click on CosmoCode which will open the Cosmo Code window. This is your graphical program development environment. Get to know it well.

2. Choose the New option from the Project menu. Change the name field to MagicSquare. Click on Application as your type of project. Click the option "Don't Use Visual Builder". Click OK which closes the New Project window.

3. This creates and builds a "template" for a Java application named "MagicSquare". It will take a minute or two to complete. You will see the Java source code for the template in the upper part of the window. Remove the line "MagicSquare app = new MagicSquare()" by highlighting the line and then using the cut option in the Edit menu. Then use the Save option under the File to save your changes. (Note, get into the habit of saving your files regularly.)

4. You will now begin the process of writing, compiling (called building), and executing (also called running) a Java application program. All of your program should be written after the left curly bracket appearing after the declaration of main and before the right curly bracket which indicates the end of main.

5. First declare three integer variables called "row1_col1", "row1_col2", and "row1_col3". (Remember from class the syntax for writing a variable declaration is "TypeName variableName [=initializer];". Initialize the variables to 7, 27, and 50 respectively. Go ahead and save your changes again. (Save option under File menu.)

6. Now before you do anything more, click on the Build card (your card options are on the lower half of the Cosmo Code window). Now click on the Build button. You will see messages from the compiler as it compiles your code. If you get no errors, that's great but it doesn't tell you what to expect when you do get errors. SO REMOVE A SEMI-COLON from one of your declarations, save the file, and build again. You should get a nice error message from the compiler. If you double click on the error in the lowermost window, it will highlight the line in your source with the problem. Fix the error and save again.

7. Open Preferences under the File menu and choose Source under the preferences category. Note that you can click an option to show line numbers in your source code. You might like to use that option at this point. Click on the Apply button. Now click the OK button.

8. Let's go back and add some code to our program. Use System.out.println to output the three numbers on separate lines. For example the statement "System.out.println(row1_col1);" will print the contents of the variable "row1_col1" and then move to a new line.

9. Now save your file and do another build. Correct any compile errors and then click on the execution card. To run your program, click on the run button which is the leftmost icon button (under the File menu). When you put the cursor over the button, the word Run will appear.

CHECKPOINT: If you have done things correctly, you should get these results output in your lower window. 7 27 50

10. Declare 3 more integer variables (without initializers) for the next row in the magic square. Name these row2_col1, row2_col2, and row2_col3.

11. Now you are going to add some computations to your program. Add these three assignment statements before your System.out.println statements.

12. Add three more System.out.println statements to the end of your program that output these three variables. (Hint: cut, paste, edit!)

13. Build and Run your program again. You should get these results: 7 27 50 71 28 -15

14. Open the Desktop tools Icon catalog (the Find option under the ToolChest menu). Drag the xcalc icon to your desktop and use it to verify your output results. The sum of the numbers in the first row should equal the numbers in the second row.

15. You have computed and printed two rows of a Magic Square. Now it's time to calculate and print the last row. If you do things correctly all rows, columns, and the two diagonals will add up to the same number. Here are the formulas for computing the final row (not the Java code, the formulas). You write the Java code and print out all 9 values when finished.

Assuming that:

A stands for the position row 1,column 1
B stands for the position row 1, column 2
C stands for the position row 1, column 3:

The formulas are:

16. If you still have time, try formatting your output to look like a magic square.

17. Try other values to test your squares out (HINT! The sum of the three initial numbers that you pick should be a multiple of 3).