Question: 6+5+4=15 8+5+2=15 For this question, we develop a representation for a Magic Square. In its 1+5+9=15 most basic form, a Magic Square is an n

6+5+4=15 8+5+2=15 For this question, we develop a representation for a Magic Square. In its 1+5+9=15 most basic form, a Magic Square is an n x n grid filled with the numbers 1,2,..., n, such that each row and each column, as well the two main 8 16 > 8+1+6=15 diagonals all have the same sum, called the magic constant M, where: 357 3+5+7=15 M n(n+1) 492 4+9+2=15 2 + + In the example on the right, n = 3 and M = 15. 8+3+4=15 6+742=15 For this question, you must complete the implementation of the class MagicSquare on page 4. Just like in assignment 1, MagicSquare uses a one-dimensional array to store the values of the grid. A. (4 marks) In the class declaration on page 4, declare three instance variables: (a) A reference variable used to designate a one-dimensional array of int values. The designated array will be used to store the values of the grid. (b) A variable to store the size of the grid, where the size represents both the number of rows and the number of columns of the magic square (3 in the example above). (c) A variable to store the magic constant, as defined above (15 in this example). B. (8 marks) In the class declaration on page 4, implement the constructor. Make sure that your implementation complies with the following guidelines: (a) The constructor stores the values of this magic square specified by the parameter. As illustrated by the test program on the next page, an object of the class MagicSquare is not affected by subsequent changes to the array of values passed as an argument to the constructor. Herein, you can assume that the the value of the parameter will not be null and the given array forms a square. In other words, the length of the array is a power of 2 for some integer n > 3. (b) The constructor calculates and stores the size of the MagicSquare, where the size represents both the number of rows and the number of columns. (e) Finally, you must compute and store the value of the magic constant C. (4 marks) Implement the following three (3) access methods. (a) Implement the instance method getSize() that returns the size of this MagicSquare, where the size is the number of rows and columns. (b) Implement the instance method getConstant() that returns the value of the magic constant, as defined above. (e) Finally, implement the method getValue(int row, int column). The method returns the value stored at location row and column of the grid. The first row and column have index 0. You can assume that the values will be valid. Be careful, this a two-dimensional coordinate whereas the information is stored in our one-dimensional array. D. (4 marks) Implement the helper method isMagicDiagonals(). This method returns true if and only if both sums, the sum of the elements on the main diagonal, and the sum of the elements on the main anti-diagonal, are equal to the magic constant. The method is called by the method isMagic(). You are not required to implement the other two methods called by isMagicSquare(). Specifically, you do not have to implement isMagicRows() and isMagic()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
