Question: In this exercise you will write code to determine whether a square is magic. File Square.java contains the shell for a class that represents a

In this exercise you will write code to determine whether a square is magic.
File Square.java contains the shell for a class that represents a square matrix. It contains headers for a constructor that gives
the size of the square and methods to read values into the square, print the square, find the sum of a given row, find the sum
of a given column, find the sum of the main (or other) diagonal, and determine whether the square is magic. The read method
is given for you; you will need to write the others. Note that the read method takes a Scanner object as a parameter.
File SquareTest.java contains the shell for a program that reads input for squares from a file named magicData and tells
whether each is a magic square. Following the comments, fill in the remaining code. Note that the main method reads the
size of a square, then after constructing the square of that size, it calls the readSquare method to read the square in. The
readSquare method must be sent the Scanner object as a parameter.
5. You should find that the first, second, and third squares in the input are magic, and that the rest (fourth through
seventh) are not. Note that the -1 at the bottom tells the test program to stop reading.
//***************************************************************
// 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)
{
}
//--------------------------------------
//return the sum of the values in the given
row //--------------------------------------
public int sumRow(int row)
{
}
//--------------------------------------
//return the sum of the values in the given
column //--------------------------------------
public int sumCol(int col)
{
}
//-----------------------------------
//return the sum of the values in the main
diagonal //---------------------------------------
public int sumMainDiag()
{
}
//--------------------------------------
//return the sum of the values in the other ("reverse")
diagonal //--------------------------------------
public int sumOtherDiag()
{
}
//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic()
{
}
//--------------------------------------
//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()
{
}
}
//***************************************************************
// 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();
}
}
}
Magic Data
3
816
357
492
7
3039481101928
384779182729
466817263537
5141625343645
1315243342444
2123324143312
2231404921120
4
489639
27182136
15303324
1245423
3
627
153
294
4
316213
69712
105118
154141
5
17241581
23516147
4

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!