// 9.7 import java.util.Scanner; public class MagicSquare{ public static void main (String[] args){ // Set up the array and input the data Scanner reader = new Scanner(System.in); int square[][] = new int[4][4]; int dataCount = 0, row = 0, col = 0; while (row < 4){ dataCount++; System.out.print("Enter the number at (" + row + "," + col + "): "); int data = reader.nextInt(); square[row][col] = data; col++; if (col == 4){ row++; col = 0; } } displayResults(square); } private static void displayResults(int square[][]){ displayList(square); if (magicSquare(square)) System.out.println("\nThis is a magic square"); else System.out.println("\nThis is not a magic square"); } private static void displayList(int a[][]){ for (int row = 0; row < a.length; row++) for (int col = 0; col < a.length; col++){ System.out.print(String.format("%3d", a[row][col]) + " "); if (col == a.length - 1) System.out.println(""); } } //Complete this section private static boolean magicSquare(int a[][]){ } private static int colSum(int a[][], int col){ int sum = 0; for (int row = 0; row < a.length; row++) sum = sum + a[row][col]; return sum; } private static int rowSum(int a[][], int row){ int sum = 0; for (int col = 0; col < a.length; col++) sum = sum + a[row][col]; return sum; } private static int diagSum(int a[][], int row, int col){ int sum = 0; int j = 0; if (row == col) // top left to bottom right for (int i = 0; i < a.length; i++){ sum = sum + a[i][j]; j++; } else // bottom left to top right for (int i = a.length - 1; i >= 0; i--){ sum = sum + a[i][j]; j++; } return sum; } }