Question: APCSA Magic Square An nx n Matrix (two-dimensional array) filled with numbers is a magic square if the sum of the elements in each row,

APCSA Magic Square An nx n Matrix (two-dimensional array) filled with numbers is a magic square if the sum of the elements in each row, in each column, and in each of the two diagonals is the same value. For example, each row, column and diagonal sums to 34 in this magic square: 16 es 2 13 5 10 11 8 9 6 7 12 4 15 14 1 Write a Square class that has as it's instance data a square, two-dimensional array and supplies methods to access the data items and print the entire array (toString), as well as sum a row, a column and each of the diagonals. The constructor should setup an array of the appropriate size. A separate Road Square method should be used to read the array Optionally, overload the ReadSquare method for various contexts such as reading from the keyboard or a file. Also include an isMagicSquare method to return a boolean based on whether or not the square is a magic square. To test the Square class, write a Square Tester class that creates and calls the appropriate methods to show that the results of various methods (for example, print each sum) so to verity that the isMagicSquare method is returning the correct results. Additional test data: Size: 3 8 16 3 5 7 4 9 2 APOSA Sample class outlines: Square class: contructor(s) readSquare sumRow sumColumn sum Main Diagonal isMagicSquare toString TestSquare: read size create a Square object call the appropriate read Square method Use printprintin to print the entire Square (implicitly calling toString) Sum and print the results for each row, coluin and diagonal Helpful in To read from a file with the file passed as a parameter: put data file (magicData.txt) in the same folder as the rest of the project for Scanner import java.util.Scanner and so to have the type File available: import java.io." In calling method (in client); calling method also needs for no-tile-found error) throws IOException public static void main(Strindarga) throws IOException System.out.printin("Enter the name of your data file:"); Scanner scannew Scanner(System.in); String file Name - scan.nextLine(); input data file name from keyboard Scanner in File now Scanner(new File(ilename)); int sqsize inFile.nextinto: Il read the size Square = new Square (Size): sq.readSquareinFile); in Square class assuming no data file errors will occur public void readSquare(Scanner infile) for (introw = 0; row
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
