// **************************************************************** // SquareTest.java // // Uses the Square class to read in square data and tell if // each square is magic. // // **************************************************************** import java.util.Scanner; import java.io.*; public class SquareTest { public static void main(String[] args) throws IOException { int count = 1; //count which square we're on Scanner scan = new Scanner(System.in); System.out.print("\nEnter the name of the input file: "); String fileName = scan.nextLine(); Scanner fileScan = new Scanner(new File(fileName)); // read the size of the square from the input file int size = fileScan.nextInt(); // expecting -1 at bottom of input file while (size != -1) { Square square = new Square(size,fileScan); //create a new Square of the given size //call its read method to read the values of the square square.readSquare(); System.out.println("\n\n******** Square " + count++ + " ********\n"); //** print the square square.printSquare(); System.out.println(); //** print the sums of its rows for (int i = 0; i < size; i++) System.out.println("Row " + i + " sum = " + square.sumRow(i)); System.out.println(); //** print the sums of its columns for (int i = 0; i < size; i++) System.out.println("Column " + i + " sum = " + square.sumCol(i)); System.out.println(); //** print the sum of the main diagonal System.out.println("The sum of the main diagonal = " + square.sumMainDiag()); System.out.println(); //** print the sum of the other diagonal System.out.println("The sum of the other diagonal = " + square.sumOtherDiag()); System.out.println(); //** determine and print whether it is a magic square if (square.magic()) System.out.println("The Square IS a magic square."); else System.out.println("The Square IS NOT magic square."); System.out.println(); System.out.print("\n\nPress enter to continue:"); scan.nextLine(); // read the size of the square from the input file size = fileScan.nextInt(); } } }