1 Replies - 234 Views - Last Post: 13 May 2014 - 04:03 PM Rate Topic: -----

#1 raghu88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 13-May 14

magic square java

Posted 13 May 2014 - 03:22 PM

package lab3;

import java.util.Scanner;

public class SquareTest {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int count = 1;
		System.out.println("Enter size of the square");

		int size = scan.nextInt(); // size of the square

		 while (size != -1)
	        {

	            //create a new Square of the given size
	            Square mySquare = new Square (size);

	            //call its read method to read the values of the square
	            System.out.println("\n******** Square " + count + " ********");

	            //print the square
	            mySquare.printSquare(); 
	            //you do not need to surround this method with S.o.p(); because in Square class, the printSquare method already calls the S.o.p() method.

	            //print the sums of its rows
	            //System.out.println("The sum of the rows in this square: " + mySquare.sumRow());
	            for (int row = 0; row < size; row++) 
	            {
	                System.out.println("The sum of the rows in row " + row + "is: " + mySquare.sumRow(row));
	            }
	            System.out.println();

	            //print the sums of its columns
	            //System.out.println("The sum of the columns in this square: " + mySquare.sumCol());
	            for (int col = 0; col < size; col++) 
	            {
	                System.out.println("The sum of the columns in column " + col + "is: " + mySquare.sumCol(col));
	            }
	            System.out.println();

	            //print the sum of the main diagonal
	            System.out.println("The sum of the main diagonal in this square: " + mySquare.sumMainDiag());
	            //print the sum of the other diagonal
	            System.out.println("The sum of the main diagonal in this square: " + mySquare.sumOtherDiag());
	            //determine and print whether it is a magic square
	            System.out.println("Is this a magic square? " + mySquare.magic());

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

	        }

	    }
	}




package lab3;

import java.util.Scanner;

public class Square {
	int[][] square;

	// --------------------------------------
	// create new square of given size
	// --------------------------------------
	public Square(int size) {
		square = new int[size][size];

		for (int row = 0; row < square.length; row++)
			for (int col = 0; col < square.length; col++)
				square[row][col] = row * 10 + col;
	}

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

	// --------------------------------------
	// return the sum of the values in the main diagonal
	// --------------------------------------
	public int sumMainDiag() {
		int sum = 0;
		for (int j = 0; j < square.length; j++)
			sum += square[j][j]; // you can do this because a square's diagonals
									// have the same coordinate points
		return sum;
	}

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

	// --------------------------------------
	// return true if the square is magic (all rows, cols, and diags have
	// same sum), false otherwise
	// --------------------------------------
	public boolean magic() {
		boolean answer = true;
		int sum = sumMainDiag();
		if (sumOtherDiag() != sum) {
			answer = false;
		} else {
			for (int col = 0; col < square.length; col++) {
				if (sum != sumCol(col)) {
					answer = false;
				}
			}
			for (int row = 0; row < square.length; row++) {
				if (sum != sumRow(row)) {
					answer = false;
				}
			}
		}
		return answer;
	}

	// --------------------------------------
	// 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() {
		  for (int row=0; row < square.length; row++) 
	      { 
	         for (int col=0; col < square[row].length; col++) 
	            System.out.print (square[row][col] + "\t"); 
	         System.out.println(); 
	      } 
	}

}



This is the error I am getting
Enter size of the square
7

******** Square 1 ********
0 1 2 3 4 5 6
10 11 12 13 14 15 16
20 21 22 23 24 25 26
30 31 32 33 34 35 36
40 41 42 43 44 45 46
50 51 52 53 54 55 56
60 61 62 63 64 65 66
The sum of the rows in row 0is: 21
The sum of the rows in row 1is: 91
The sum of the rows in row 2is: 161
The sum of the rows in row 3is: 231
The sum of the rows in row 4is: 301
The sum of the rows in row 5is: 371
The sum of the rows in row 6is: 441

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7
at lab3.Square.sumCol(Square.java:35)
at lab3.SquareTest.main(SquareTest.java:38)

not entirely sure what i got wrong most of the code structure was provided in the assingment but
unfortunately this is how i am supposed to do the code.

Is This A Good Question/Topic? 0
  • +

Replies To: magic square java

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4353
  • View blog
  • Posts: 12,160
  • Joined: 18-April 07

Re: magic square java

Posted 13 May 2014 - 04:03 PM

Consider this line...

for (int row = 0; row < square[row].length; row++)



Let's say you enter 7 so at some point row is going to be evaluating 7 < square[7].length. So in order to evaluate this, Java is going to first go to square[7] to get an idea of what its length is going to be so it can compare it to 7. Problem is, there is no 7th row. Highest index would be 6 (because it starts at zero).

What you need to do is not use "row" as the index variable of square[] since row is the value that is changing in the loop and needing to be compared to. Hope you get the idea.

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1