| New Reply |
Testing magic squares in Java |
Share Thread |
| Apr3-11, 02:20 PM | #1 |
|
|
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 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
}
}
|
| Apr3-11, 04:40 PM | #2 |
|
Mentor
|
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. |
| Apr3-11, 04:53 PM | #3 |
|
|
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. |
| Apr3-11, 09:22 PM | #4 |
|
Mentor
|
Testing magic squares in JavaIf 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. |
| New Reply |
| Tags |
| java, magic, square |
Similar discussions for: Testing magic squares in Java
|
||||
| Thread | Forum | Replies | ||
| 3 x 3 Magic Squares | Calculus & Beyond Homework | 3 | ||
| Magic Squares/Magic Constant | Calculus & Beyond Homework | 1 | ||
| 3x3 magic squares | Precalculus Mathematics Homework | 4 | ||
| Magic Squares | General Math | 8 | ||
| Magic Squares & Cubes | Set Theory, Logic, Probability, Statistics | 1 | ||