• Post Reply Bookmark Topic Watch Topic
  • New Topic

Magic Box Help?  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to create a program that checks if a user inputted values are what is known as a "magic box." The values are a magic box if the sum of the elements in each row, column, and the two diagonals are the same value and every element is unique. Here is the exact project requirements

"Write a class SquareMatrix that represents a matrix, and contains the following methods: - Relevant accessor/mutator methods - void add(int i, int row, int column): o Adds an integer to the matrix at the specified location. - boolean isMagic() o Determines whether the matrix illustrates a magic square. This means:  The user entered n 2 numbers for some number n  Each of the numbers occurs exactly once in the matrix  The sums of the elements in each row, column, and the two diagonals are equal Your class should handle user input through a full GUI, or pop -up windows. The user should specify: - The size of the matrix (the value of n) - The numbers to be added to the matrix"

I tried to start writing a basic code to do this (I'm just trying to make it work as of now, I will worry about the driver class later) and I already ran into a few problem with my code. Here is my code



The code compiles but when I try to run it Java gives me the following message

"java.lang.ArrayIndexOutOfBoundsException: 3
at SquareMatrix.main(SquareMatrix.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)"

The number I input for that particular example was 4 but that happens for every number I try inputting. So what can I do to fix this? I know my code is nowhere near finished but if I can't figure this step out I don't think I will be able to continue. Thank you in advanced.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Your array is dimensioned 3x3 so the indices can be 0,1,and 2 for each dimension. box[3][3] is out of bounds
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John,
I am not sure of your understanding of Java, so I will put the comments in the code, please read the code comments for my thoughts:
John Sing wrote:
I tried to start writing a basic code to do this (I'm just trying to make it work as of now, I will worry about the driver class later) and I already ran into a few problem with my code.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!