## Testing magic squares in Java

1. The problem statement, all variables and given/known data

Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

Write a program that reads in n2 values from the keyboard and tests whether they form a magic square when arranged as a square matrix. You need to test three features:

1) Did the user enter n2 numbers for some n?
2) Do each of the numbers 1, 2, ..., n2 occur exactly once in the user input?
3) When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

If the size of the input is a square, test whether all numbers between 1 and n2 are present. Then compute the row, columns, and diagonal sums. Implement a class Square with methods

public boolean isMagic()

2. Relevant equations

There isn't so much an equation as a full blown program,
the issue i'm having is with writing the class definition.

3. The attempt at a solution

Code:
public class Square
{
private int Value;

public Square(int aValue)
{
Value = aVaule;
}
{

}
public boolean isMagic()
{
if
}
}
I'm not sure how to write either of the public void add(int i) or public boolean isMagic() statements. Any advice?
 PhysOrg.com science news on PhysOrg.com >> New language discovery reveals linguistic insights>> US official: Solar plane to help ground energy use (Update)>> Four microphones, computer algorithm enough to produce 3-D model of simple, convex room

Mentor
 Quote by Hypnos_16 1. The problem statement, all variables and given/known data Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. Write a program that reads in n2 values from the keyboard and tests whether they form a magic square when arranged as a square matrix. You need to test three features: 1) Did the user enter n2 numbers for some n? 2) Do each of the numbers 1, 2, ..., n2 occur exactly once in the user input? 3) When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other? If the size of the input is a square, test whether all numbers between 1 and n2 are present. Then compute the row, columns, and diagonal sums. Implement a class Square with methods public void add(int i) public boolean isMagic() 2. Relevant equations There isn't so much an equation as a full blown program, the issue i'm having is with writing the class definition. 3. The attempt at a solution Code: public class Square { private int Value; public Square(int aValue) { Value = aVaule; } public void add(int i) { } public boolean isMagic() { if } } I'm not sure how to write either of the public void add(int i) or public boolean isMagic() statements. Any advice?
Sure. Instead of coding something up without understanding what you need to do, start with the algorithm you're going to implement in code. It would also be a good idea to get some clarification on what a couple of the methods of the Square class are supposed to do.

1) What is your class constructor supposed to do? In particular, what is the purpose of the int argument? Does the int parameter merely set the number n for an n x n square? BTW, you have a typo in the body of your constructor.

2) What is the add supposed to do? Is it supposed to add the values in a particular column or a particular row? Or one of the two diagonals? What is the purpose of the int parameter? Have you been given any more information about this method?

3) As you currently have things, the only member variable of your class is an int. I would think that there would be a two-dimensional array member to store the numbers that make up the potential magic square.

4) Your isMagic method should check all n rows, all n columns, and both diagonals to see if each of them adds up to the same value. If so, the numbers entered make up a magic square, so the method should return true. If one or more of the rows, columns, or diagonals don't add up to the same value, the method should return false.
 1) The int parameter take the given inputted value (n) and makes a two dimensional array (n x n) or that's really the only thing that makes sense to me for it to do. Also i corrected the type 2) The add is supposed to add all the variables in the rows, columns and diagonals and compare them, making sure they are all the same value. 3) Not sure what you mean here 4) I get that, i just can't visualize how i would write something like that. That being said, i'm pretty sure that's my whole problem in general.

Mentor

## Testing magic squares in Java

 Quote by Hypnos_16 1) The int parameter take the given inputted value (n) and makes a two dimensional array (n x n) or that's really the only thing that makes sense to me for it to do. Also i corrected the type 2) The add is supposed to add all the variables in the rows, columns and diagonals and compare them, making sure they are all the same value.
What's the purpose of the int parameter? Is that part of the requirements, or is that something you added?
 Quote by Hypnos_16 3) Not sure what you mean here
What I mean is that you should have a member variable that holds the two-dimensional array that you mentioned in your answer to #1.
 Quote by Hypnos_16 4) I get that, i just can't visualize how i would write something like that. That being said, i'm pretty sure that's my whole problem in general.
If, for example, you were working with a 3 x 3 square of numbers, your isMagic would need to add the three rows, the three columns, and the two diagonals. Each sum would have 3 terms. If all eight of these sums were equal, the square would be a magic square.

If you were working with a 4 x 4 square, you would have to add the four rows, four columns, and two diagonals. Each row, columns, and diagonal would have four terms. In general, for an n x n square, there will be n rows, n columns, and two diagonals, each with n terms.

 Tags java, magic, square

 Similar discussions for: Testing magic squares in Java Thread Forum Replies Calculus & Beyond Homework 3 Calculus & Beyond Homework 1 Precalculus Mathematics Homework 4 General Math 8 Set Theory, Logic, Probability, Statistics 1