Question
2D Arrays A magic square is a square matrix (N rows by N columns) with the following properties: 1. It contains the numbers 1 through
2D Arrays
A magic squareis a square matrix (N rows by N columns) with the following properties:
1. It contains the numbers 1 through N2, with each number appearing exactly once.
2. The sums of each row, each column, and both main diagonals are equal.
For example, here is a "3 by 3" magic square:
4 9 2
3 5 7
8 1 6
The assignment is to create a class to model a magic square and a test class for it.
I. The MagicSquare Class
- The only instance variable is a 2D array of ints
- The class constructor will create an "N by N" 2D array, where the size is passed as a parameter. It will then call a private utility methods to fill the array
- To get credit, your fillmethod must use the following algorithm:
1. Place a 1 in the "middle" column of the last row.
2. Place the integers from 2 to N2into the matrix in succession, according to these rules:
- Attempt to place each number in a position that is one row belowand one column to the rightof the previous number.
- If rule (a.) leads to a non-existentrow and a non-existent column, then the number is placed instead in the position directly above the previous number (i.e., same column, previous row).
- If rule (a.) leads to a non-existentrow, then the number is placed instead in the firstrow, one column to the right.
- If rule (a.) leads to a non-existentcolumn, then the number is placed instead in the firstcolumn, one row down.
- If rule (a.) leads to a position that is not empty(i.e., already contains a value), then the number is placed instead in the position directly above the previous number.
- Your MagicSquareclass will also have a toString()method that returns the square in table form, as shown above
- Finally, a method that returns a value of trueor falseindicating whether the square created is truly magic. That is, whether allthe sums those of every row and of every column and of both diagonals are equal. All. Every. Both.
II. The Test Class
Your main method will:
- Get the size of the square, N, from the user. Use a loop so that only positive, odd values for N are accepted
- Create a MagicSquareobject
- Call the method that tests whether the square is truly magic. If so, call toStringand print the square returned. Else, print an error message
- After each square (or error message) is printed, the user should be asked whether he/she wants to create another
III. Due Date: Tuesday, February 19th
IV. What to Upload to Canvas
- A zip file of your NetBeans project folder, including a file with the output from two runs
- An empty Word doc to get feedback
Both files will be uploaded via the same assignment link
- No need to generate the html pages, but your MagicSquareclass should adhere to all the style and documentation standards Javadoc comments and internal documentation covered in Unit 1.
!!!!!PLEASE BE UNIQUE AND DO NOT GET ANSWERS FROM ONLINE TO ANSWER THIS CHEGG QUESTION PLEASE!!!!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started