Programming Exercise 1 – Magic Square (100 points)

Programming Exercise 1 – Magic Square
(100 points)
May 24, 2015
1 Objectives
-
Create and manipulate two­dimensional arrays
Generate Java code from pseudo­code
Read and write information to files
2 Tasks
For this project, you will write a program that checks whether a given n x n matrix of integers values forms a magic square. Your program will also generate a magic square for a given n, assuming n is an odd number.
An n x n matrix of integers filled with the values 1, 2, 3, …, n2 is a magic square if the sum of all of the elements in each row, in each column, and along the two diagonals is
the same. For instance, the following is a 4 x 4 magic square with a sum of 34:
16 3
2 13
5 10 11 8
9
6
7 12
4 15 14 1
For the first part of the project, you will read in the values for matrix from a file and check to determine whether the values constitute a magic square. The file will also contain the dimensions of the matrix, so you will know its size before reading in the values. To test whether the matrix is a magic square, you will have to test two attributes of the values:
1. Do each of the integers 1, 2, 3, …, n2 occur in the matrix?
2. Are the sums of each of the rows, columns, and the two diagonals equal to the same value, which is equal to n (n2 + 1) / 2?
For the next part of the project, you will construct a magic square for a given n. If n is odd, the following algorithm will construct a valid n x n magic square:
Set row = n-1, column = n/2
For k = 1 … n*n
Place k at [row][column].
Increment row and column.
If the row or column is n, replace it with 0.
If the element at [row][column] is already filled
Set row and column to their previous values.
Decrement row.
Here is the 3 x 3 square that you will get if you follow this method:
4
9
2
3
5
7
8
1
6
Once you have created the magic square, write it to a file in same format used in the first part of the project. Note: this algorithm will only work with dimensions that are odd. 3 Files
For this project, you will create two classes. The first is the MagicSquare class. At a minimum, your class should include the following methods: readMatrix, writeMatrix, checkMatrix, createMagicSquare. It's up to you to determine the return types and parameters for these functions. You should include any instance variables and other methods that you think are necessary for the implementation of the class. The other class you need to create is the MagicSquareTester class. This is a driver class for the MagicSquare class. It should include the main function and be able to read command­line arguments. To operate correctly, it should run using the following format:
$ java MagicSquareTester [-check | -create] [filename] [
|size]
where the -check flag indicates that your program will determine whether the file called filename contains a magic square and will print out the appropriate message. For instance, to determine whether the file called myMatrix.txt contains the magic square from the above example, you would use the following command­
line syntax:
$ java MagicSquareTester -check myMatrix.txt
and it would print out the following message, because it contains a valid magic square:
The matrix:
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
is a magic square.
For an invalid matrix, your program should print out the same output, except it should state that the matrix is not a magic square.
With the -create flag, your program will create a magic square with the dimensions size x size and will write that square to the file called filename.
For instance, to create a 5 x 5 magic square that is written to the file called myMagicSquare.txt, you would use the following command­line syntax:
$ java MagicSquareTester -create myMagicSquare.txt 5
and your program would print no output. Because the given algorithm will only work for odd dimensions, your program should verify this and write an error message to the
file if the requested square has an even number of dimensions. The format of the files with the matrices that will checked by your program and those that your program will write magic squares to is the same. The first line of the file should include the size of the matrix, and the rest of the file should contain the values
of the matrix using that number of rows and columns. For instance, the file myMagicSquare.txt from the example above would be written as:
5
11 18 25 2 9
10 12 19 21 3
4 6 13 20 22
23 5 7 14 16
17 24 1 8 15
In order to test your program, you will be provided with test files with both valid and invalid matrices that are in the correct format. 4 Programming Hints
4.1 Reading from a file
Review Section 2.6 in the textbook. It covers the Scanner class and how to read basic data from files. 4.2 Writing to a file
To open a write stream to a file, you can use the following code:
File file = new File(filename);
PrintWriter outFile = new PrintWriter(new FileWriter(file));
…
outfile.close();
where filename is a String variable with the name of the file you're writing to. Note that this code will overwrite the contents of the file if it already exists. The missing code should be filled in with the appropriate method calls on the PrintWriter variable.
You can use println but printf, a legacy method from C, is better at writing formatted
data. Here's a link that's discusses how to use printf: www.cplusplus.com/reference/cstdio/printf/
5 Notes
•
•
•
Be sure to validate all inputs, including the command­line arguments. The arguments may be in the wrong order or there may not be enough of them. Check for this. Also, files may not contain data in the correct format. For instance, the data may not be integers. Check for this too. However, you can assume that if the first value (i.e. the size) in the file is an integer, the matrix will have these dimensions.
Your programs will be tested with test files other than the ones provided. Create
your own and make sure it works with other values. Points will be awarded according to the following break­down:
◦ README file, comments – 10 pts
◦ Reads, executes command­line arguments – 10 pts ◦ Correctly reads, identifies matrices in eight different test files – 40 pts ◦ Correctly creates, writes magic squares to a file – 40 pts 6 Required Files
All the files should be well documented, to include program and function headers that use common Javadoc notation, as well as appropriate in­line comments. At a minimum, submit the following files:
o MagicSquare.java
o MagicSquareTester.java
o README o Any other files required to compile and run your program
6 Submitting the Project
Submit all files from the same directory. Do not include any unnecessary files. Use this command: submit mhthomas cs221 p1