Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this program: // Square.java // // Define a Square class with methods to

ID: 3659592 • Letter: I

Question

I have this program:

// 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
{
int[][] square;
//--------------------------------------
//create new square of given size
//--------------------------------------

public Square(int size)
{
square = new int[size][size];
for (int row=0; row < square.length; row++)
for (int col=0; col < square.length; col++)[row][col] = row * 10 + col;
}

//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------

public int sumRow(int row)
{
int sum = 0;
for (int col=0; col < square.length; col++)
{
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[row].length; row++)
{
sum += square[row][col];
}
return sum;
}


//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------

public int sumMainDiag()
{
int sum = 0;
for (int j=0; j< square.length; j++)
sum += square[j][j]; //you can do this because a square's diagonals have the same coordinate points
return sum;
}
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------

public int sumOtherDiag()
{
int sum = 0;
for (int j=0; j< square.length; j++)
sum += square[j][square.length - 1 - j];
return sum;
}

//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------

public boolean magic()
{
boolean answer = true;
int sum = sumMainDiag();
if (sumOtherDiag() != sum)
{
answer = false;
}
else
{
for (int col = 0; col < square.length; col++)
{
if (sum != sumCol(col))
{
answer = false;
}
}
for (int row = 0; row < square.length; row++)
{
if (sum != sumRow(row))
{
answer = false;
}
}
}
return answer;
}

//--------------------------------------
//read info into the square from the input stream associated with the
//Scanner parameter
//--------------------------------------

public void readSquare(Scanner scan)
{
for (int row = 0; row < square.length; row++)
for (int col = 0; col < square.length; col ++)
square[row][col] = scan.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] + " ");
System.out.println();
}

}

}

I need to write a test program:

// ***************************************************************
// SquareTest.java
//
// Uses the Square class to read in square data and tell if
// each square is magic.
//
// ***************************************************************

import java.util.Scanner;
public class SquareTest
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(new File("magicData"));
int count = 1; //count which square we're on
int size = scan.nextInt(); //size of next square
//Expecting -1 at bottom of input file
while (size != -1)
{
//create a new Square of the given size
//call its read method to read the values of the square
System.out.println(" ******** Square " + count + " ********");
//print the square
//print the sums of its rows
//print the sums of its columns
//print the sum of the main diagonal
//print the sum of the other diagonal
//determine and print whether it is a magic square
//get size of next square
size = scan.nextInt();
}
}
}

To read this data:

magicData
3
8 1 6
3 5 7
4 9 2
7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
4
48 9 6 39
27 18 21 36
15 30 33 24
12 45 42 3 4

3
6 2 7
1 5 3
2 9 4
4
3 16 2 13
6 9 7 12
10 5 11 8
15 4 14 1
5
17 24 15 8 1
23 5 16 14 7
4 6 22 13 20
10 12 3 21 19
11 18 9 2 25
7
30 39 48 1 10 28 19
38 47 7 9 18 29 27
46 6 8 17 26 37 35
5 14 16 25 34 45 36
13 15 24 33 42 4 44
21 23 32 41 43 12 3
22 31 40 49 2 20 11
-1

Explanation / Answer

public class MagicSquare { private int[][] square; private boolean[] possible; private int totalSqs; private int sum; private int numsquares; private int numBoards; // Creates an empty MagicSquare object. public MagicSquare(int n) { // Fill in an empty square; 0 represents unfilled. square = new int[n][n]; for (int i=0; i