Create a class called MagicSquare.java. public static void main(String[] args) {
ID: 3830529 • Letter: C
Question
Create a class called MagicSquare.java.
public static void main(String[] args) {
int[][] square = {
{ 8, 11, 14, 1},
{13, 2, 7,12},
{ 3, 16, 9, 6},
{10, 5, 4, 15}
};
System.out.printf("The 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.
Explanation / Answer
package org.students;
public class MagicSquareOrNot {
public static void main(String[] args) {
int[][] square = {
{ 8, 11, 14, 1},
{13, 2, 7,12},
{ 3, 16, 9, 6},
{10, 5, 4, 15}
};
System.out.printf("The square %s a magic square. %n",
(isMagicSquare(square) ? "is" : "is not"));
}
/* This method will check whether the matrix is magic square magic square or not
* @param 2-D array of type integer
* @return boolean
*/
private static boolean isMagicSquare(int[][] square) {
int rows=square.length;
int cols=square[0].length;
boolean bool = false;
if(rows==cols && (rows>=1 && cols>=1))
{
//Creating arrays and declaring variables
int sumDiag[]=new int[rows];
int sumCol=0,sumRow=0;
int roww[]=new int[rows];
int coll[]=new int[cols];
//Calculating the sum of each row
for (int row = 0; row < rows; row++)
{
sumRow=0;
for (int col = 0; col < cols; col ++)
{
sumRow+= square[row][col];
roww[row]=sumRow;
}
//Displaying the sum of each row
System.out.println("sum row "+row+"::"+sumRow );
}
//calculating the Sum of Each column
for (int col = 0; col < cols; col++)
{
sumCol=0;
for (int row = 0; row < rows; row ++)
{
sumCol += square[row][col];
coll[col]=sumCol;
}
//Displaying the sum of each column
System.out.println("sum columns "+col+"::"+sumCol);
}
sumDiag[0]=0;
//calculating the Sum of Diagonal0
for (int row = 0; row < rows; row++)
{
sumDiag[0] += square[row][row];
}
//Displaying the sum of elements in the diagonal0
System.out.println("sum diagonal 0 "+"::"+sumDiag[0]);
sumDiag[1]=0;
//calculating the Sum of Diagonal 1
for(int row = 0; row < rows; row++)
{
sumDiag[1] += square[row][rows - 1 - row];
}
//Displaying the sum of elements in the diagonal1
System.out.println("sum diagonal 1 " + "::" +sumDiag[1]);
/* checking whether sum of elements in each row,each column and each diagonal are equal or not
* If yes, The square array is as Magic Square
* if not, The square array is not Magic Square
*/
//if (sumRow==sumCol && sumCol==sumDiag[0] && sumDiag[0]==sumDiag[1] && sumDiag[1]==sumRow) {
if (roww[0]==roww[1] && roww[1]==roww[2] && coll[0]==coll[1] && coll[1]==coll[2] && roww[0]==sumDiag[0] && sumDiag[0]==sumDiag[1] && sumDiag[1]==coll[0])
bool=true;
else
bool=false;
}
return bool;
}
}
____________________
Output:
sum row 0::34
sum row 1::34
sum row 2::34
sum row 3::34
sum columns 0::34
sum columns 1::34
sum columns 2::34
sum columns 3::34
sum diagonal 0 ::34
sum diagonal 1 ::34
The square is a magic square.
___________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.