I coded a Magic Square program with a driver and a class. The only problem I'm having is adding up the rows, columns, and diagonals. I made a int constant called "col" for column, that counts up 1 each time an entire row is summed. Therefore, the multi-dimensional array would first sum [0,0],[0,1],[0,2] to whatever size the square is, and then col would be incremented, changing to [1,0],[1,1],[1,2] etc. However, after my adding method computes one row, goes to print, and comes back for the next row, col is set back to 0. Is there a way I can prevent it from resetting and keep its incrementation? I think that's the final step. Note: I only put the col on the row and column sum methods so far, I just want to get it to work for those first.
Note: Please make sure you name the text file correctly when transferring the text given here
Thank you all in advance.
// ****************************************************************
// MagicSquare.java
//
// Text below is to be filled by student.
//
// ****************************************************************
import java.util.Scanner;
public class MagicSquare
{
int[][] square;
public MagicSquare(int size)
{
square = new int[size][size];
}
int col = 0;
//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------
private int sumMagicRow(int size,int col)
{
int sum = 0;
for (int i=0;i<(size);i++)
{
sum += square[col][i];
}
col++;
return sum;
}
//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
private int sumMagicCol(int size, int col)
{
int sum = 0;
for (int i=0;i<size;i++)
{
sum += square[col][i];
}
col++;
return sum;
}
//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
private int sumMagicDiagMain(int size)
{
int sum = 0;
for (int i=0;i<size;i++)
{
sum += square[i][i];
}
return sum;
}
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
private int sumMagicDiagRev(int size)
{
int sum = 0;
for (int i=0;i<size;i++)
{
sum += square[i][(size-1)-i];
}
return sum;
}
//--------------------------------------
//return true if the square is magic (all rows, cols, and diags
// have same sum), false otherwise
//--------------------------------------
public boolean isMagicSquare(int size)
{
boolean answer =false;
if(sumMagicCol(size,col)==sumMagicRow(size,col) && sumMagicRow(size,col)==sumMagicDiagMain(size) && sumMagicDiagMain(size)==sumMagicDiagRev(size))
{
answer=true;
}
return answer;
}
//--------------------------------------
//compute and display sums of square including row, column, main diagonal, and other diagonal
//--------------------------------------
public void printMagicSquareSums(int size)
{
for(int i=0;i<size;i++)
{
System.out.println("Sum of row " + i + " is: " + sumMagicRow(size,col));
}
for(int i=0;i<size;i++)
{
System.out.println("Sum of column " + i + " is: " + sumMagicCol(size,col));
}
for(int i=0;i<size;i++)
{
System.out.println("Sum of row " + i + " is: " + sumMagicDiagMain(size));
}
for(int i=0;i<size;i++)
{
System.out.println("Sum of row " + i + " is: " + sumMagicDiagRev(size));
}
}
//--------------------------------------
//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(int size)
{
int column=0;
for(int x=0;x<size;x++)
{
for(int i=0;i<size;i++)
{
System.out.printf("%3d ",square[x][i]);
}
System.out.println("");
}
}
}
Driver program:
// ****************************************************************
// MagicSquareTest.java
//
// Text below is to be filled by student.
//
// ****************************************************************
import java.util.Scanner;
import java.io.IOException;
import java.io.File;
public class MagicSquareTest
{
public static void main(String[] args) throws IOException
{
Scanner scan = new Scanner(new File("magicText.txt"));
// make sure that the file magicData is in the current directory
int count = 1; //count which square we're on
int size = scan.nextInt(); //size of next square
int mem = size;
//Expecting -1 at bottom of input file
while (size != -1)
{
//create a new Square of the given size
MagicSquare s = new MagicSquare(size);
size=mem;
//call its read method to read the values of the square
System.out.println("\n***** Square " + count + " *****");
s.readSquare(scan);
//print the square
s.printSquare(size);
//print the square id
//System.out.println(count);
//print the sums
s.printMagicSquareSums(size);
//determine and print whether it is a magic square
System.out.println(s.isMagicSquare(size));
//get size of next square
size = scan.nextInt();
count++;
}
}
}
Text File (that reads into array)
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