JAVA!!!- Activity 3. Magic Squares. In this activity, you will be given a 2D arr
ID: 3681307 • Letter: J
Question
JAVA!!!- Activity 3. Magic Squares. In this activity, you will be given a 2D array of integers (acquired within the main: code is provided to you), and you have to check if this 2D array (table or matrix as you prefer) is a magic square.
What is a magic square?
A magic square is a square 2D array or table or matrix, which has the following properties: the numbers in each row, column, and diagonal (main and secondary) add up to the same value.
So for instance, the following table is a magic square:
Method MagicSquare
Parameter: a 2D array of integers
Return type: Boolean
This method returns true if the parameter is a magic square, false otherwise.
Requirement: The implementation of this method requires the implementation of four auxiliary methods: sumRow, sumColumn, sumDiagonal1, and sumDiagonal2.
Method sumRow
Parameters:
· A 2D array of integers; and
· An integer i
Return type: integer
This method returns the sum of the numbers in the ith row of the 2D array.
Requirement: You are expected to use a for loop to implement this method.
Method sumColumn
Parameters:
· A 2D array of integers; and
· An integer i
Return type: integer
This method returns the sum of the numbers in the ith column of the 2D array.
Requirement: You are expected to use a for loop to implement this method.
Method sumDiagonal1
Parameters: a 2D array of integers
Return type: integer
This method returns the sum of the numbers in the main diagonal of the 2D array.
Requirement: You are expected to use a for loop to implement this method.
Method sumDiagonal2
Parameters: a 2D array of integers
Return type: integer
This method returns the sum of the numbers in the secondary diagonal of the 2D array.
Requirement: You are expected to use a for loop to implement
Given:
public static boolean MagicSquare(int[][] table) {
// your code goes here
return true;
}
// this method computes the sum of row number "row" of a given 2D array of integers
public static int sumRow(int[][] table, int row) {
int sum = 0;
// your code goes here
return sum;
}
// this method computes the sum of column number "column" of a given 2D array of integers
// we will assume the table is not ragged
public static int sumColumn(int[][] table, int column) {
int sum = 0;
// your code goes here
return sum;
}
// this method computes the sum of numbers in the main diagonal of a given 2D array of integers
public static int sumDiagonal1(int[][] table) {
int sum = 0;
// your code goes here
return sum;
}
// this method computes the sum of numbers in the secondary diagonal of a given 2D array of integers
public static int sumDiagonal2(int[][] table) {
int sum = 0;
// your code goes here
return sum;
}
Method MagicSquare
Parameter: a 2D array of integers
Return type: Boolean
Explanation / Answer
/**
* @author Srinivas Palli
*
*/
public class MagicSquare {
// method returns true if the parameter is a magic square, false otherwise.
public static boolean MagicSquare(int[][] table) {
// your code goes here
final int n = table.length;
final int M = (n * n * (n * n + 1) / 2) / n;
for (int j = 0; j < table.length; j++) {
if (sumRow(table, j) != M || sumColumn(table, j) != M)
return false;
}
if (sumDiagonal1(table) != M || sumDiagonal2(table) != M)
return false;
return true;
}
// this method computes the sum of row number "row" of a given 2D array of
// integers
public static int sumRow(int[][] table, int row) {
int sum = 0;
// your code goes here
for (int j = 0; j < table.length; j++) {
sum += table[row][j];
}
return sum;
}
// this method computes the sum of column number "column" of a given 2D
// array of integers
// we will assume the table is not ragged
public static int sumColumn(int[][] table, int column) {
int sum = 0;
// your code goes here
for (int i = 0; i < table.length; i++) {
sum += table[i][column];
}
return sum;
}
// this method computes the sum of numbers in the main diagonal of a given
// 2D array of integers
public static int sumDiagonal1(int[][] table) {
int sum = 0;
// your code goes here
for (int i = 0; i < table.length; i++) {
sum += table[i][i];
}
return sum;
}
// this method computes the sum of numbers in the secondary diagonal of a
// given 2D array of integers
public static int sumDiagonal2(int[][] table) {
int sum = 0;
for (int i = 0; i < table.length; i++) {
sum += table[i][table.length - i - 1];
}
return sum;
}
public static void main(String[] args) {
int[][] table = { { 2, 7, 6 }, { 9, 5, 1 }, { 4, 3, 8 } };
if (MagicSquare(table))
System.out.println("Magic Square");
else
System.out.println("Not a Magic Square");
}
}
OUTPUT:
Magic Square
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.