|
complete codes
import java.util.Scanner;
public class Square
{
int[][] square;
//--------------------------------------
//create new square of given size
//--------------------------------------
public Square(int size)
{
int[][] square = new int[size][size];
}
//--------------------------------------
//return the sum of the values in the given row
//--------------------------------------
public int sumRow(int row)
{
int rowTotal = 0;
for (int x = 0;x < square.length;x++)
{
rowTotal += square[row][x];
}
return rowTotal;
}
//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col)
{
int colTotal =0;
for (int x = 0;x < square.length;x++)
{
colTotal += square[x][col];
}
return colTotal;
}
//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
public int sumMainDiag()
{
int MDTotal = 0;
for (int x = 0;x < square.length;x++)
{
MDTotal += square[x][x];
}
return MDTotal;
}
//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag()
{
int OMDTotal = 0;
for (int x = (square.length -1);x >= 0;x--)
{
OMDTotal += square[x][x];
}
return OMDTotal;
}
//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic()
{
boolean ItIsMagic = false;
int TheSum;
TheSum = sumMainDiag();
if (TheSum == sumOtherDiag())
{
for (int x = 0; x < square.length; x++)
{
if (sumCol(x) == TheSum && sumRow(x) == TheSum)
ItIsMagic = true;
else
{
ItIsMagic = false;
continue;
}
}
}
else
{
ItIsMagic = false;
}
return ItIsMagic;
}
//--------------------------------------
//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.length; col++)
System.out.print(square[row][col] + "\t");
System.out.println();
}
}
}
Main method below
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
//Expecting -1 at bottom of input file
while (size != -1)
{
Square square = new Square(size);
//create a new Square of the given size
square.readSquare(scan);
//call its read method to read the values of the square
System.out.println("\n******** Square " + count + " ********");
//print the square
square.printSquare();
//print the sums of its rows
for (int x = 0; x < size;x++)
{
System.out.print(square.sumRow(x)+"\t");
}
//print the sums of its columns
for (int x = 0; x < size;x++)
{
System.out.print(square.sumCol(x)+"\t");
}
//print the sum of the main diagonal
System.out.println(square.sumMainDiag());
//print the sum of the other diagonal
System.out.println(square.sumOtherDiag());
//determine and print whether it is a magic square
if (square.magic() == true)
System.out.println("The Square is a magic square");
else
System.out.println("The square is not a magic square");
//get size of next square
size = scan.nextInt();
}
}
}
i think the error i discribed earlier is what preventing me from seeing the assignment work.
|