Question: Create a class called MagicSquare.java. It includes the following main method: public static void main(String[] args) { int[][] square = { {2, 7, 6}, {9,

Create a class called MagicSquare.java.

Create a class called MagicSquare.java. It includes the following main method: public

It includes the following main method:

public static void main(String[] args) { int[][] square = { {2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; System.out.printf("Square %s a magic square. %n", (isMagicSquare(square) ? "is" : "is not")); }

It is your job to write the method isMagicSquare:

public static boolean isMagicSquare(int[][] square) { // TODO }

How do you know whether a 2-dimensional array is a magic square? First of all it needs to be a square. To keep the scope of the lab manageable you may assume that the 2-dimensional arrays passed have the same number of rows and columns and that there is at least one row and one column. The square is 'magic' if the numbers in each of the rows, columns, and diagonals add up to the same value (see image above) Recommendation: use private methods tp structure your code.

2 7 6 15 9 5 1 15 4 3 8 15 15 15 15 15 15 magic square

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!