Results 1 to 20 of 21
Thread: Magic square
 04132010, 08:35 PM #1Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Magic square
Alright, I've had so many problems with this that I just made an ongoing thread that way my OCD doesn't keep me from just posting another problem in the same thread. My problem is weird. It compiles fine, but after I enter the size it throws a ArrayIndexOutOfBoundsException. I know what that is, I just don't understand why it throws in this instance.
Java Code:public class Magic { public static void main(String[] args) { int order; Scanner input = new Scanner(System.in); Square square; System.out.print("Enter the order of the magic square no greater than 20: "); order = input.nextInt(); square = new Square(order); while(order > 20  order < 3) { System.out.println("Invalid input. Please enter an positive order no greater than 20: "); order = input.nextInt(); } if (order % 2 == 1) { square.oddSquare(); square.printSquare(); //it throws at this line } else if(order % 4 == 0) { square.doubleSquare(); square.printSquare(); } else { square.singleSquare(); square.printSquare(); } } } class Square { int x; int y; int order; int[][] square; int diag; int num; int counter; public Square(int order) { int diag = 1; int num = 1; int counter = 1; } public void oddSquare() { int[][] square = new int[order][order]; y = 0; x = order / 2; square[y][x] = num; //it also throws at this line while (counter <= order) { while (diag <= order) { num += 1; x += 1; y = 1; if (x > order  1) { x = 0; } if (y < 0) { y = order  1; } square[y][x] = num; diag += 1; } diag = 1; counter += 1; y += 1; } } public void singleSquare() { } public void doubleSquare() { } public void printSquare() { System.out.print("+"); for (int i = 1; i <= order; i++) { System.out.print(""); } System.out.println("+"); for (int row = 0; row < order; row++) { for (int col = 0; col < order; col++) { System.out.print(""); System.out.format("%3d" + square[row][col]); } System.out.println(""); System.out.print("+"); for (int i = 1; i <= order; i++) { System.out.print(""); } System.out.println("+"); } } }
 04132010, 08:39 PM #2Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Never mind. I just noticed like, eight different problems with this.
 04132010, 08:44 PM #3Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
Java Code:public void oddSquare() { int[][] square = new int[order][order]; // ...
What you should do instead is this:
Java Code:public void oddSquare() { square = new int[order][order]; // ...
 04132010, 08:47 PM #4Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Here's my new code. It now generates a NullPointerException at the same points.
Java Code:public class Magic { public static void main(String[] args) { int order; Scanner input = new Scanner(System.in); Square square; System.out.print("Enter the order of the magic square no greater than 20: "); order = input.nextInt(); square = new Square(order); while(order > 20  order < 3) { System.out.println("Invalid input. Please enter an positive order no greater than 20: "); order = input.nextInt(); } if (order % 2 == 1) { square.oddSquare(); square.printSquare(); } else if(order % 4 == 0) { square.doubleSquare(); square.printSquare(); } else { square.singleSquare(); square.printSquare(); } } } class Square { int x; int y; int order; int[][] square; int diag; int num; int counter; public Square(int order) { int diag = 1; int num = 1; int counter = 1; int[][] square = new int[order][order]; } public void oddSquare() { y = 0; x = order / 2; square[y][x] = num; while (counter <= order) { while (diag <= order) { num += 1; x += 1; y = 1; if (x > order  1) { x = 0; } if (y < 0) { y = order  1; } square[y][x] = num; diag += 1; } diag = 1; counter += 1; y += 1; } } public void singleSquare() { } public void doubleSquare() { } public void printSquare() { System.out.print("+"); for (int i = 1; i <= order; i++) { System.out.print(""); } System.out.println("+"); for (int row = 0; row < order; row++) { for (int col = 0; col < order; col++) { System.out.print(""); System.out.format("%3d" + square[row][col]); } System.out.println(""); System.out.print("+"); for (int i = 1; i <= order; i++) { System.out.print(""); } System.out.println("+"); } } }
 04132010, 08:51 PM #5Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Oh sorry, hadn't noticed iluxa's post.
Last edited by gandalf5166; 04132010 at 08:57 PM.
 04132010, 08:54 PM #6Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
that's more like it.
here's how scopes work in Java:
Java Code:class A { int x = 21; public A () { System.out.println (x); int x = 48; System.out.println (x); printStuff (); } void printStuff () { System.out.println (x); } }
The reason is this: when you say smth like "x=48", Java needs to decide exactly which variable you're talking about. In my example, there's two variables: one belongs to the class, and one you have just declared in the constructor. Java takes the mostrecentlydefined variable into account. When it goes out of scope, however, (meaning you've closed the } ), you're back to the original one.
Here's your code:
Java Code:public Square(int order) { int diag = 1; int num = 1; int counter = 1; int[][] square = new int[order][order]; }
Java Code:public Square(int order) { diag = 1; num = 1; counter = 1; square = new int[order][order]; }
Java Code:public Square(int order) { this.diag = 1; this.num = 1; this.counter = 1; this.square = new int[order][order]; }
 04132010, 09:01 PM #7Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Ok, did what you said, and I'm not getting any more error messages, but for some reason all it will print out is 2 +'s no matter what the order is.
 04132010, 09:03 PM #8Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
that's cause you aren't printing the + signs inside any loop... take a look at your printing code again
 04132010, 09:20 PM #9Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Yeah, but it won't print out anything else. Like the 's or the 's or the numbers. And actually, although it is kinda confusing, the +'s are being printed within a loop towards the end.
 04132010, 09:22 PM #10Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
oh, i see what you're doing.
you're making the scope mistake again.
you're assigning "order" in one place, but using it in another where it was not assigned by anybody  see if you can figure it out.
if you have a debugger, would be useful to step through you program
 04132010, 09:28 PM #11Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
I have a debugger, I have no idea how to use it though..... :C
 04132010, 09:31 PM #12Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
In that case, stick "System.out.println ("order now is" + order);" EVERYWHERE and see what's going on
 04132010, 09:33 PM #13Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
THANK YOU! OK, I can figure out this last error....
 04142010, 05:06 AM #14Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
OK, it runs perfectly, but I've got a logic error. When it calculates the square it is supposed to start with one at the top in the middle. Then it should go up and to the right and put two there. If it goes of to the right or to the top, it should wrap around. Then, once it reaches a square that is already filled, it should move down a square and continue. The problem is, that instead of moving down, it moves to the right. Here's my code, what's going on?
Java Code:class Square { int x; int y; int order; int[][] square; int diag; int num; int counter; public Square(int order) { this.order = order; this.diag = 1; this.num = 0; this.counter = 1; square = new int[order][order]; } public void oddSquare() { y = 0; x = order / 2; while (counter <= order) { while (diag <= order) { num += 1; if (x > order  1) { x = 0; } if (y < 0) { y = order  1; } square[y][x] = num; diag += 1; x += 1; y = 1; } diag = 1; counter += 1; y += 1; if (y > order  1) { y = 0; } } } public void singleSquare() { } public void doubleSquare() { } public void printSquare() { System.out.print("+"); for (int i = 1; i <= order; i++) { if(i % 2 == 0) { System.out.print(""); } else { System.out.print(""); } } System.out.println("+"); for (int row = 0; row < order; row++) { for (int col = 0; col < order; col++) { System.out.print(""); System.out.format("%3d", + square[row][col]); } System.out.println(""); System.out.print("+"); for (int i = 1; i <= order; i++) { if(i % 2 == 0) { System.out.print(""); } else { System.out.print(""); } } System.out.println("+"); } } public void magicNumber() { } }
 04142010, 08:33 PM #15Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
I hate to do this...... but bump?
 04142010, 08:48 PM #16Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
FIGURED IT OUT!
Java Code:while (counter <= order) { while (diag <= order) { num += 1; x += 1; y = 1; if (x > order  1) { x = 0; } if (y < 0) { y = order  1; } square[y][x] = num; diag += 1; } diag = 1; counter += 1; y += 1; }
 04142010, 08:49 PM #17Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Actually, that wasn't the problem. The solution still works though!
 04152010, 03:49 AM #18Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Ok, so I've started on the method for squares whose orders can be divided by four. The way you do those starts with dividing the square in to as many 4x4 squares as it takes and drawing diagonal lines through each of these smaller squares. So I'm writing the bit that does that. Rather than drawing a line, it changes the value to 900. So the way I'm testing this is, before I write the rest, I'm running the program so it will print out the square with just zeros and 900's. So I should see mostly zeros with diagonal lines of 900's through it. But what I am seeing is a table of zeros. If you're having trouble seeing what I'm doing, draw a 4x4 square and then draw diagonal lines through it. This was the only way I could think of to get the same effect. Here's the code for the line drawing.
Java Code:x = 0; y = 0; for (int i = 0; i <= order / 4; i++){ for (int j = 0; j <= order / 4; j++){ square[y][x] = 900; x += 3; square[y][x] = 900; x += 1; } x = 0; y += 1; for (int j = 0; j <= order / 4; j++) { x += 1; square[y][x] = 900; x += 1; square[y][x] = 900; x += 2; } x = 0; y += 1; for (int j = 0; j <= order / 4; j++) { x += 1; square[y][x] = 900; x += 1; square[y][x] = 900; x += 2; } x = 0; y += 1; for (int j = 0; j <= order / 4; j++) { square[y][x] = 900; x += 3; square[y][x] = 900; x += 1; } y += 1; }
Last edited by gandalf5166; 04152010 at 03:51 AM.
 04152010, 05:57 PM #19Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
I am forced to do this once again...... bump.
 04152010, 07:08 PM #20Member
 Join Date
 Feb 2010
 Posts
 75
 Rep Power
 0
Similar Threads

Magic squares LUX method
By gandalf5166 in forum New To JavaReplies: 14Last Post: 04172010, 04:30 PM 
Magic Square!!!... :D
By joms999 in forum New To JavaReplies: 4Last Post: 02252010, 07:55 AM 
Magic Eightball
By sachmow in forum New To JavaReplies: 1Last Post: 11152009, 04:37 PM 
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05052008, 04:04 AM 
Incompatible magic value 1008821359
By willemjav in forum Java AppletsReplies: 2Last Post: 03212008, 09:41 AM
Bookmarks