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

One interesting application of two-dimensional arrays is magic squares. A magic

ID: 3688879 • Letter: O

Question

One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and both diagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code to determine whether a square is magic.

In this program you should have two files: Square.java and SquareTest.java

File Square.java  should contain a class that represents a square matrix. It should include 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.

File SquareTest.java should contain a program that reads input for squares from a file named magicData.txt and tells whether each is a magic square. Main method should read the size of a square, then after constructing the square of that size, it should call the readSquare method to read the square in.

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.

Here magicData.txt

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
46 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
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

// ****************************************************************
// 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];
}
//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------
public int sumRow(int row)
{
int sum = 0;
for (int i=0;i<square[0].length;i++)
sum += square[row][i];
return sum;
}
//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col)
{
int sum = 0;
for (int i=0;i<square.length;i++)
sum += square[i][col];
return sum;
}
//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
public int sumMainDiag()
{
int sum = 0;
for (int i=0;i<square[0].length;i++)
sum += square[i][i];
return sum;
}
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag()
{
int sum = 0;
int x = square.length;
for (int i=0;i<x;i++)
sum += square[x-1-i][i];
return sum;
}
//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic(int[] sums)
{
boolean bool = true;
for (int i = 0; i<sums.length; i++)
{
if (i+1 != sums.length)
{
if (sums[i] == sums[i+1])
bool = true;
else
{
i = sums.length;
bool = false;
}
}
}
return bool;
}
//--------------------------------------
//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++)
{
System.out.println("");
for (int col = 0; col < square.length; col ++)
System.out.print(square[row][col] + " ");
}
}
}

// ****************************************************************
// 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
{
Scanner scan = new Scanner(new File("magicData.txt"));
int count = 1; //count which square we're on
int size = scan.nextInt(); //size of next square
boolean isSquare;
//Expecting -1 at bottom of input file
while (size != -1)
{
int[] arr = new int[size+size];
//create a new Square of the given size
Square sq = new Square(size);
//call its read method to read the values of the square
sq.readSquare(scan);
System.out.println(" ******** Square " + count + " ********");
//print the square
sq.printSquare();
System.out.println("");
//print the sums of its rows
for (int i = 0; i < size; i++)
{
arr[i] = sq.sumRow(i);
System.out.print(" Sum of row #" + i + ": " + arr[i]);
}
System.out.println("");
//print the sums of its columns
for (int i = 0; i < size; i++)
{
arr[i+size-1] = sq.sumRow(i);
System.out.print(" Sum of column #" + i + ": " + arr[i+size-1]);
}
System.out.println("");
//print the sum of the main diagonal
arr[size+size-2] = sq.sumMainDiag();
System.out.println("Sum of main diagonal: " + arr[size+size-2]);
//print the sum of the other diagonal
arr[size+size-1] = sq.sumOtherDiag();
System.out.println("Sum of other diagonal: " + arr[size+size-1]);
//determine and print whether it is a magic square
System.out.println("");
//for (int i = 0; i<arr.length; i++)
//System.out.print(arr[i] + " ");
isSquare = sq.magic(arr);
if (isSquare)
System.out.println("Magical square!");
else
System.out.println("There is no magic in this square..");
//get size of next square
size = scan.nextInt();
count++;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote