Magic Squares - Lab 9 | Object Oriented Programming | CS 201, Lab Reports of Computer Science

Material Type: Lab; Class: Intro Obj Orient Programming; Subject: Computer Science; University: University of Alabama - Birmingham; Term: Fall 2005;

Typology: Lab Reports

Pre 2010

Uploaded on 04/12/2010

koofers-user-c1w
koofers-user-c1w 🇺🇸

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Magic Squares
One interesting application of two-dimensional arrays is magic squares. A magic square
is a square matrix in which the sum of every row, every column, and both diagonals is the
same. Magic squares have been studied for many years, and there are some particularly
famous magic squares. In this exercise you will write code to determine whether a square
is magic.
File Square.java contains the shell for a class that represents a square matrix. It contains
headers for a constructor that gives the size of the square and methods to read values into
the square, print the square, find the sum of a given row, find the sum of a given column,
find the sum of the main (or other) diagonal, and determine whether the square is magic.
The read method is given for you; you will need to write the others.
File SquareTest.java contains a program that reads input for squares from a file and tells
whether each square is a magic square.
Run your program on file magicData.txt. You should find that the first, second, and third
squares in the input are magic, and that the rest (fourth through seventh) are not. Note
that the -1 at the bottom of the test file tells the test program to stop reading.
After coding your own solution, compare it to the solution given on the web page.
The output for the first square read from file magicData.txt should be as follows:

Partial preview of the text

Download Magic Squares - Lab 9 | Object Oriented Programming | CS 201 and more Lab Reports Computer Science in PDF only on Docsity!

Magic Squares

One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and both diagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code to determine whether a square is magic.

File Square.java contains the shell for a class that represents a square matrix. It contains headers for a constructor that gives the size of the square and methods to read values into the square, print the square, find the sum of a given row, find the sum of a given column, find the sum of the main (or other) diagonal, and determine whether the square is magic. The read method is given for you; you will need to write the others.

File SquareTest.java contains a program that reads input for squares from a file and tells whether each square is a magic square.

Run your program on file magicData.txt. You should find that the first, second, and third squares in the input are magic, and that the rest (fourth through seventh) are not. Note that the -1 at the bottom of the test file tells the test program to stop reading.

After coding your own solution, compare it to the solution given on the web page.

The output for the first square read from file magicData.txt should be as follows: