# Magic Square Java Homework: Implement Class Square

• Comp Sci
• eechord
So if you can calculate that sum, you only have to check if the sum of each row, column and diagonal is equal to that sum.*) For the loadSquare method, you will indeed need the Scanner class to get input from the user. You can use the Scanner's nextInt() method to read an integer from the user. You will need to use a nested for-loop to fill the square array with the user's input.
eechord

## Homework Statement

An n by 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 class that creates a square matrix (as a two-dimensional array) andtests whether it forms a magic square. You need to test two features:
 Do each of the numbers 1, 2, … n2 occur exactly once in the user input?
 When the numbers are put into a square, are the sums of the rows, columns, and diagonals equal to each other?

Implement the class Square, which you will include the following methods:
 constructor method Square(int[][] square) that initializes the instance variable squareArray with the parameter square provided by the runner class. This constructor must also calculate the side length of the square.
 public void printSquare(), which will print the contents of the square, with tab characters between the values in each column.
 public boolean isFull( ) – returns true if the values 1, 2, … n2 occur exactly once, false otherwise.
 public boolean isMagic( ) – returns true if the sum of each row, each column and the two diagonals is the same.
 public void loadSquare(), which prompts the user for a value for each row and column in the square.

## Homework Equations

I don't have the runner class with me, but can post it later.

## The Attempt at a Solution

Code:
public class Square
{
private int sideLength;
private int [][] squareArray;

public Square (int [][]square)
{
int [][] squareArray= int [][]square;       //not sure if i did this constructor right
sideLength= 4;
int [][]squareArray= new int [sideLength][sideLength];
}
public void printSquare ()
{
for (int row=0; row<squareArray.length; r++)    //not sure whether to use square or squareArray
{
for (int col=0; col<squareArray[row].length; col++)
{
System.out.print (squareArray [row][col];   //not sure how to put tab characters in                                                                       between values
}
System.out.println ();
}
public boolean isFull ()
{
for (int r=0; r<squareArray.length;r++
{for (int c=0; c<squareArray[r].length; c++)
{if (for(int r2=1; r2<squareArray.length; r2++)
{for (int c2=1; c2<squareArray.length; c2++)
}
return true;
}
public boolean isMagic ()
{
//how do i test if they're all the same, without comparing them individually?
}
public void loadSquare ()
{
//scanner class?
}

Last edited by a moderator:
A few tips and pointers:
*) In the constructor, the first new is useless. It gets overwritten almost immediately by the second new.
*) A tab character is "\t", for example: System.out.println("My name is:\teechord");
*) You have a syntax error in the System.out.print statement (missing closing bracket)
*) To test if they are the same, you will have to check each row, column and diagonal separately. To make it easier, given the side length you know what the sum will be (namely 1 + 2 + ... + sidelength = 1/2 * sidelength * (sidelength + 1)).

## 1. What is a Magic Square in Java?

A Magic Square in Java is a grid filled with numbers where the sum of each row, column, and diagonal is the same. It is a popular programming exercise and can be implemented using the Square class.

## 2. How do I implement the Square class in my Java program?

To implement the Square class, you will need to first create a new Java file and name it Square.java. Then, you can define the necessary variables and methods in the class to represent a Magic Square. You can also add additional features or functionality as desired.

## 3. What are the key components of a Magic Square in Java?

The key components of a Magic Square in Java are the size of the square, the numbers used, and the algorithm used to generate the square. The size of the square will determine the number of rows and columns, while the numbers used will determine the sum of each row, column, and diagonal. The algorithm used will determine the arrangement of the numbers in the square.

## 4. How do I test my Magic Square Java program?

To test your Magic Square Java program, you can create multiple instances of the Square class with different sizes and numbers and check if the sum of each row, column, and diagonal is equal. You can also print out the magic square to visually confirm that it follows the rules of a Magic Square.

## 5. Are there any resources or libraries available to help with implementing a Magic Square in Java?

Yes, there are resources and libraries available that can help with implementing a Magic Square in Java. Some popular libraries include Apache Commons Math and JScience. These libraries provide pre-written code for generating Magic Squares and can save you time and effort in implementing the class.

### Similar threads

• Engineering and Comp Sci Homework Help
Replies
12
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
7
Views
1K
• Engineering and Comp Sci Homework Help
Replies
4
Views
7K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
4
Views
2K
• Engineering and Comp Sci Homework Help
Replies
2
Views
3K
• Engineering and Comp Sci Homework Help
Replies
4
Views
3K
• Engineering and Comp Sci Homework Help
Replies
2
Views
6K
• Engineering and Comp Sci Homework Help
Replies
5
Views
2K