PDA

View Full Version : Magic squares [Java]



JPHamlett
01-31-2011, 04:01 PM
Ok, so one last java question for a while haha.

Assignment : http://www.district196.org/evhs/academics/apcomputerscience/labs/ch6/MagicSquares1.htm


Here's my Main class



package magicsquare;

// ************************************************** **************
// SquareTest.java
//
// Uses the Square class to read in square data and tell if
// each square is magic.
//
// ************************************************** **************

import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int count = 1; //count which square we're on
System.out.println("How big is the square");
int size = scan.nextInt(); //size of next square


//Expecting -1 at bottom of input file
if (size > 0)
{

//create a new Square of the given size

Square square = new Square(size);

square.readSquare();

System.out.println("\n******** Square " + count + " ********");

square.printSquare();

for (int i = 0; i < size; i ++){
System.out.println("Row " + (i + 1) + ": " + square.sumRow(i));
System.out.println("Column " + (i + 1) + ": " + square.sumCol(i));
}

System.out.println("Main diagonal: " + square.sumMainDiag());

System.out.println("Other diagonal: " + square.sumOtherDiag());

//determine and print whether it is a magic square


//get size of next square
size = scan.nextInt();

}

}
}



Here's the Square class



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package magicsquare;

/**
*
* @author 729484
*/
import cs1.Keyboard;

public class Square {
// ************************************************** **************
// 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.
//
// ************************************************** **************


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 col = 0; col < square.length; col++)
sum += square[row][col];
return sum;
}


//--------------------------------------
//return the sum of the values in the given column
//--------------------------------------
public int sumCol(int col)
{
int sum = 0;
for (int row = 0; row < square.length; row++)
sum += square[row][col];
return sum;
}

//--------------------------------------
//return the sum of the values in the main diagonal
//--------------------------------------
public int sumMainDiag()
{
int diag = 0;
for (int i =0; i < square.length; i++){
diag += square[i][i];
}
return diag;
}

//--------------------------------------
//return the sum of the values in the other ("reverse") diagonal
//--------------------------------------
public int sumOtherDiag()
{
int diag = 0;
for (int i =0; i < square.length - 1; i++){
diag += square[square.length - i][i - 1];
}
return diag;

}

//--------------------------------------
//return true if the square is magic (all rows, cols, and diags have
//same sum), false otherwise
//--------------------------------------
public boolean magic()
{
boolean magic, arrayMatch;
int size = square.length;
int[] colArray = new int[size];
int[] rowArray = new int[size];
magic = sumOtherDiag() == sumMainDiag();
if (magic = false)
return false;
for (int i =0; i < size; i++){
colArray[i] = sumCol(i);
rowArray[i] = sumRow(i);
}
arrayMatch = colArray == rowArray;
return magic && arrayMatch;
}

//--------------------------------------
//read info into the square from the standard input.
//--------------------------------------
public void readSquare()
{
for (int row = 0; row < square.length; row++)
for (int col = 0; col < square.length; col ++)
square[row][col] = Keyboard.readInt();
}

//--------------------------------------
//print the contents of the square, neatly formatted
//--------------------------------------
public void printSquare(){
for (int i = 0; i < square.length; i++) {
for (int j = 0; j < square.length; j++) {
if (square[i][j] < 10) System.out.print(" "); // for alignment
if (square[i][j] < 100) System.out.print(" "); // for alignment
System.out.print(square[i][j] + " ");
}
System.out.println();
}

}


}



Now I get this error during run-time



Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at magicsquare.Square.sumOtherDiag(Square.java:79)
at magicsquare.Main.main(Main.java:43)
Java Result: 1


The line the error is reporting about is



diag += square[square.length - i][i - 1];



Any help???

HyperSecret
01-31-2011, 04:38 PM
Same as any other language, look at the error :p

ArrayIndexOutOfBounds, You are trying to access and element in an array that isn't there.

AFAIK, there is no [0 - 1] index in an array...

Frement
01-31-2011, 04:47 PM
So changing "int i = 0;" to 1 should fix it, no?

HyperSecret
01-31-2011, 08:47 PM
So changing "int i = 0;" to 1 should fix it, no?

That would then screw up the other index calculation. Be best to remove the '- 1'.

arash
02-06-2011, 06:15 AM
for (int i =0; i < square.length - 1; i++){
diag += square[square.length - i][i - 1];

Your issue is here, initially there is no -1.