Comp Sci How to Test if a Matrix is a Magic Square in Java?

AI Thread Summary
To determine if a matrix is a magic square in Java, a program must verify three key conditions: the user must input n² numbers, each number from 1 to n² must appear exactly once, and the sums of all rows, columns, and diagonals must be equal. The implementation involves creating a class called Square with methods to add numbers and check if the matrix is a magic square. The class should include a two-dimensional array to store the matrix values and logic to compute the sums for validation. Properly defining the methods and understanding their roles is crucial for successful implementation.
Hypnos_16
Messages
148
Reaction score
1

Homework Statement



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()


Homework Equations



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

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?
 
Physics news on Phys.org
Hypnos_16 said:

Homework Statement



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()


Homework Equations



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

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.
 
Hypnos_16 said:
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?
Hypnos_16 said:
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.
Hypnos_16 said:
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.
 

Similar threads

Replies
1
Views
4K
Replies
7
Views
2K
Replies
12
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
1
Views
1K
Back
Top