Need help with code.
This is a computer science lab assignment. It is supposed to determine whether or not an array of numbers from "magicData" make-up magic squares. All the calculations are correct and the Square.java file compiles.
I can't get the SquareTest.java file to compile. I keep getting errors concerning the Square.readSquare( ) method. Exact error is : Square(java.util.Scanner) in Square cannot be applied to ()
Any help would be appreciated.
Here is the code:
// 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 x=0; x < square.length; x++)
{
sum+= square[row][x];
}
return(sum);
}
//--
//return the sum of the values in the given column
//--
public int sumCol(int col)
{
int sum = 0;
for (int x=0; x < square.length; x++)
{
sum+= square[x][col];
}
return(sum);
}
//--
//return the sum of the values in the main diagonal
//--
public int sumMainDiag()
{
int sum = 0;
int y = 0;
for (int x=0; x < square.length; x++)
{
sum+= square[x][y];
y++;
}
return(sum);
}
//--
//return the sum of the values in the other ("reverse") diagonal
//--
public int sumOtherDiag()
{
int sum = 0;
int y = (square.length -1);
for (int x=(square.length -1); x >= 0; x--)
{
sum+= square[x][y];
y--;
}
return(sum);
}
//--
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--
public boolean magic()
{
int firstSum = sumRow(0);
boolean magicSquare = true;
for (int row = 1; row < square.length; row++)
if (sumRow(row) != firstSum)
return false;
for (int col = 0; col < square[0].length; col++)
if (sumCol(col) != firstSum)
return false;
if (sumMainDiag() != firstSum)
return false;
if (sumOtherDiag() != firstSum)
return false;
return true;
}
//--
//read info into the square from the standard input.
//--
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 x,y;
for (x=0;x<square.length;x++)
{
for (y=0;y<square.length;y++)
{
System.out.print(" " + square[x][y]);
}
System.out.println();
}
}
}
And the test class:
// ****************************************************************
// 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"));
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)
{
size = scan.nextInt();
//create a new Square of the given size
Square magic = new Square(size);
//call its read method to read the values of the square
Square.readSquare();
System.out.println("\n******** Square " + count + " ********");
//print the square
Square.printSquare();
System.out.println();
//print the sums of its rows
for(int i = 0; i >< size;i++)
{
int sumRow = magic.sumRow(i);
System.out.println("The sum of row"+(i+1)+"is" + sumRow);
}
//print the sums of its columns
for(int i = 0; i < size;i++)
{
int sumCol = magic.sumCol(i);
System.out.println("The sum of column"+(i+1)+"is" + sumCol);
}
//print the sum of the main diagonal
int sumDiag1 = magic.sumMainDiag();
System.out.println("The sum of the main diagonal is" + sumDiag1);
//print the sum of the other diagonal
int sumDiag2 = magic.sumOtherDiag();
System.out.println("The sum of the other diagonal is" + sumDiag2);
//determine and print whether it is a magic square
if (magic.magic())
System.out.println("This is a magic square.");
else
System.out.println("This is not a magic square.");
//get size of next square
size = scan.nextInt();
}
}
}

