Two-Dimensional Arrays Assignment

See this link for Assignment Policies, Submission Instructions, and Grading Guidelines.

Instructions

1.      Write a Java application that does the following.

2.      Determine if the input file contains a magic square of size 9 x 9. Display appropriate output.

3.      Display the magic square.

4.      If it is a magic square, then simply state “this is a magic square”.

5.      If it is NOT a magic square, then explain why it is not a magic square. For example, “because column X has sum Y, but the other columns have sum Z”. Or “because row X has sum Y, but the other rows have sum Z”. Or, “the forward diagonal has sum Y, but the backward diagonal has sum Z”. Or, “the forward diagonal has sum X, but row Y has sum Z”. Or something similar to this.

6.      A magic square has the property that all the numbers in the columns, all the numbers in the rows, and all the numbers in the two diagonals add up to the same number.  See Wikipedia for more details.

7.      Use the 1st command-line argument as the name of the input file. Output will be to the screen (not to an output file).

8.      See the slides on File I/O on more details about command-line arguments. Basically, you need to click “Build”, then “Run Arguments”. A text box will appear at the top of jGRASP. Type the name of your input file, then type the name of your output file.  The name of your input file is stored within “args[0]”.

9.      Use an “if statement” to do error checking to see if the user has entered command-line arguments or not.

10.  Use the File and the Scanner class to create a connection to the input file. You also need a try/catch block.  See the example ReadFromFile.java from the File I/O section on how to do this.

11.  Declare a two-dimensional 9 by 9 array of Integer (or int).

12.  Use nested loops to loop through the rows & columns of the 2-dimensional array, read the numbers in the input file, and store the numbers in a two-dimensional array.

13.  Loop through one row (or one column) of the 2-dimensional array, and add up all the numbers in one row (or one column). This will be the sum of the magic square.

14.  Instantiate a Boolean flag to “true”.  This will be used to determine if the file contains a magic square or not.

15.  Loop through the two-dimensional array and add up the elements in each row, column, and the two diagonals to determine if is a magic square or not. You need to compare the sum of each row, column, and diagonal to the original sum that you calculated. If the numbers are different, then it is not a magic square.

16.  To add up all the columns, use nested loops, with the outer loop looping through the rows. The outer loop should create a variable to store the total of each column. Then the inner loop will loop through all the columns. The inner loop will add up the numbers in the columns, and store it in the total variable. Then the outer loop will have an “if statement” to see if the total of each column is equal to the original sum. If not, you should have a Boolean flag that will be set to “false”.

17.  To add up all the rows, use nested loops, with the outer loop looping through the columns. The outer loop should create a variable to store the total of each row. Then the inner loop will loop through all the rows. The inner loop will add up the numbers in the rows, and store it in the total variable. Then the outer loop will have an “if statement” to see if the total of each row is equal to the original sum. If not, you should have a Boolean flag that will be set to “false”.

18.  To add up the left to right diagonal, you need a single loop. It should loop through all the rows. In the loop, you should add up all the diagonal elements in the 2-dimensional array. You also need to increment the column in each loop as well. After the loop, then compare the diagonal sum to the original sum. After the loop, use an “if statement” to compare the diagonal sum to the original sum. If they are not equals, then set the Boolean flag to “false”.

19.  To add up the right to left diagonal, you need a single loop. It should loop through all the rows. In the loop, you should add up all the diagonal elements in the 2-dimensional array. You also need to decrement the column in each loop as well. After the loop, use an “if statement” to compare the diagonal sum to the original sum. If they are not equals, then set the Boolean flag to “false”.

20.  Note that you should use the method “equals()” if you are comparing Integers (object Integer). You should use “!=” or “==” to compare ints (primitive data type int).

21.  Use an “if statement” to determine if the Boolean flag is “true” or “false”. “true” means that it is a magic square, and “false” means that it is NOT a magic square.

22.  If the input file contains a magic square, display appropriate output. If the input file DOES NOT contains a magic square, display appropriate output. Use either the System or JOptionPane classes for output.

23.  For displaying errors and giving feedback, use either the System or JOptionPane classes.  For example, if the user does not enter enough command-line arguments, display appropriate output. If the file cannot be found, display appropriate output. For any other errors, display appropriate output.

24.  Your program should not crash, no matter what the input file contains.  Even if there is not an input file, then your program should not crash. Use try/catch blocks and exception classes java.util.InputMismatchException and java.util.NoSuchElementException.

25.  Here are some input files with magic squares to test your program: square1.txt, square2.txt

1.      Here are some input files which are NOT magic squares to test your program: square3.txt, square4.txt, square5.txt, square6.txt

2.      WARNING: DO NOT try to write the whole program at once. Make sure the most basic part works first. Then, gradually add to it until your program is complete.

 

 

 

© 2007 William Albritton