<p>Here\'s the question I\'m having some issues with. I basically have to fill i
ID: 3640374 • Letter: #
Question
<p>Here's the question I'm having some issues with. I basically have to fill in the three methods (rowSum(), colSum(), and diagSum()) :<br /><br />An n x n matrix that is filled with the numbers 1, 2, 3, …, n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The MagicSquare class that follows holds a two-dimensional array, and the method isSquare() determines if the array matrix forms a magic square. There are three methods missing in the class that you must provide: rowSum(), colSum(), and diagSum().<br /><br />Here is the code, and any help is appreciated, thanks!<br /><br />/**<br /> * MagicSquare - determines if array forms a magic square.<br /> */<br />public class MagicSquare<br />{<br /> private int[][]square;<br /> <br /> /**<br /> * Constructor - passed in a square<br /> */<br /> public MagicSquare(int[][]inSquare)<br /> {<br /> square = inSquare;<br /> }<br /><br /> /**<br /> * isMagic - determines if array forms a magic square.<br /> * <br /> * @return true if array is a magic square, false otherwise<br /> */<br /> public boolean isMagic()<br /> {<br /> // test if two dimensions are the same<br /> if (square.length != square[0].length)<br /> {<br /> return false; // not n x n<br /> }<br /> <br /> // test that all integers are represented<br /> for (int num = 1; num <= square.length * square.length; num++)<br /> {<br /> boolean found = false;<br /> for (int i = 0; i < square.length && found == false; i++)<br /> {<br /> for (int j = 0; j < square.length; j++)<br /> {<br /> if (square[i][j] == num)<br /> {<br /> found = true;<br /> break; // out of inner for loop<br /> }<br /> }<br /> }<br /> if (found == false) // number not in array<br /> {<br /> return false;<br /> }<br /> }<br /> <br /> // test that any row, column or diagonal did not match<br /> if (rowSum() == -1 || colSum() == -1 || diagSum() == -1)<br /> {<br /> return false;<br /> }<br /> <br /> // test that the sums of rows, columns, and diagonals are same<br /> if (rowSum() != colSum() || colSum() != diagSum())<br /> {<br /> return false;<br /> }<br /> <br /> // it is a magic square<br /> return true;<br /> }<br /> /**<br /> * rowSum - determines sum of rows.<br /> * <br /> * @return sum of rows if all the same, -1 otherwise<br /> */<br /> public int rowSum()<br /> {</p><p>// create method here<br /><br /> }<br /> <br /> /**<br /> * colSum - determines sum of columns.<br /> * <br /> * @return sum of columns if all the same, -1 otherwise<br /> */<br /> public int colSum()<br /> {<br /> // create method here</p>
<p><br /> }<br /> <br /> /**<br /> * diagSum - determines sum of the two diagonals.<br /> * <br /> * @return sum of two diagonals if the same, -1 otherwise<br /> */<br /> public int diagSum()<br /> {</p>
<p>// create method here<br /><br /> }<br />}<br /><br /></p>
Explanation / Answer
public class MagicSquare { public static void main(String[] args) { int N = Integer.parseInt(args[0]); if (N % 2 == 0) throw new RuntimeException("N must be odd"); int[][] magic = new int[N][N]; int row = N-1; int col = N/2; magic[row][col] = 1; for (int i = 2; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.