// **************************************************************** // // Square.java // // Define a Square class with methods to create and read in // info for a square matrix and to compute the sum of a row, // a col, either diagonal, and whether it is magic. // // **************************************************************** import java.util.Scanner; public class Square { Scanner fileScan; int[][] square; //---------------------------------------------------------------------------- //create new square as a two-dimensional array with size rows and size columns //---------------------------------------------------------------------------- public Square(int size, Scanner fileScan ) { square = new int[size][size]; this.fileScan = fileScan; } //--------------------------------------------- //return the sum of the values in the given row //--------------------------------------------- public int sumRow(int row) { int sum = 0; for(int col = 0; col < square[row].length; col++) sum = sum + square[row][col]; return sum; } //------------------------------------------------ //return the sum of the values in the given column //------------------------------------------------ public int sumCol(int col) { int sum = 0; for(int row = 0; row < square.length; row++) sum = sum + square[row][col]; return sum; } //------------------------------------------------- //return the sum of the values in the main diagonal //------------------------------------------------- public int sumMainDiag() { int col = 0, sum = 0; for(int row = 0; row < square.length; row++) { sum = sum + square[row][col]; col++; } return sum; } //-------------------------------------------------------------- //return the sum of the values in the other ("reverse") diagonal //-------------------------------------------------------------- public int sumOtherDiag() { int col = square[0].length - 1, sum = 0; for(int row = 0; row < square.length; row++) { sum = sum + square[row][col]; col--; } return sum; } //------------------------------------------------------------------ //return true if the square is magic (all rows, cols, and diags have //same sum), false otherwise //------------------------------------------------------------------ public boolean magic() { int firstSum = sumRow(0); boolean magicSquare = true; for (int row = 1; row < square.length; row++) if (sumRow(row) != firstSum) return false; for (int col = 0; col < square[0].length; col++) if (sumCol(col) != firstSum) return false; if (sumMainDiag() != firstSum) return false; if (sumOtherDiag() != firstSum) return false; return true; } //-------------------------------------------------- //read info into the square from the standard input. //-------------------------------------------------- public void readSquare() { for (int row = 0; row < square.length; row++) { for (int col = 0; col < square.length; col ++) square[row][col] = fileScan.nextInt(); } } //-------------------------------------------------- //print the contents of the square, neatly formatted //-------------------------------------------------- public void printSquare() { for (int row = 0; row < square.length; row++) { for (int col = 0; col < square[row].length; col++) System.out.print(square[row][col] + "\t"); System.out.println(); } } }