Ensure that you read all instructions carefully. Do not include any additional methods that are not specified in this quiz description. Think carefully about your solution before writing any code. Use meaningful variable names and proper indentation - take pride in your work!
Task: In the MagicSquare class you will implement five methods.
- checkSum: This method takes a 2D array of integers and returns the sum of the first row of the 2D array.
- magicRows: This method takes a 2D array of integers and a checkSum and returns returns a boolean value depending on whether the sum of each row in the 2D array is equal to checkSum or not.
- magicColumns: This method takes a 2D array of integers and a checkSum and returns returns a boolean value depending on whether the sum of each column in the 2D array is equal to checkSum or not.
- magicDiagonals: This method takes a 2D array of integers and a checkSum and returns a boolean value depending on whether the sum of each of the main two diagonals in the 2D array is equal to checkSum or not.
- isMagic: This method takes a 2D array of integers and returns a boolean value depending on whether the 2D array is magic or not.
Description: A magic square is one such that every row, every column, and both main diagonals add to the same value.
0,0 | 0,1 | 0,2 |
---|---|---|
1,0 | 1,1 | 1,2 |
2,0 | 2,1 | 2,2 |
2 | 7 | 6 |
---|---|---|
9 | 5 | 1 |
4 | 3 | 8 |
The sum of each row and each column and the main two diagonals all add to the value 15.
Row 1: [2,7,6] → 2 + 7 + 6 = 15
Row 2: [9,5,1] → 9 + 5 + 1 = 15
Row 3: [4,3,8] → 4 + 3 + 8 = 15
Column 1: [2,9,4] → 2 + 9 + 4 = 15
Column 2: [7,5,3] → 7 + 5 + 3 = 15
Column 3: [6,1,8] → 6 + 1 + 8 = 15
Main diagonal 1: [2,5,8] → 2 + 5 + 8 = 15
Main diagonal 2: [6,5,4] → 6 + 5 + 4 = 15
int[][] array2d = { {2,7,6}, {9,5,1}, {4,3,8} };
checkSum(array2d); → returns 15
magicRows(array2d, checkSum(array2d)); → returns true
magicColumns(array2d, checkSum(array2d)); → returns true
magicDiagonals(array2d, checkSum(array2d)); → returns true
isMagic(array2d); → returns true
Note that you solution must work for a 2D array of size n x n.
Note also that the 2D array will be a square. That is, it will have the same number of rows as columns, and all rows will be of the same length.
Good luck, have fun! =)