Comp Sci Magic Square Java Homework: Implement Class Square

Click For Summary
The discussion focuses on implementing a Java class named Square to create and validate a magic square, which is an n by n matrix where the sums of all rows, columns, and diagonals are equal. Key methods to be included are a constructor to initialize the matrix, a method to print the square, and methods to check if the square is full and if it is a magic square. Participants highlight issues in the initial code, such as incorrect array initialization and syntax errors, while providing guidance on using tab characters for formatting and calculating the expected sum for validation. The importance of checking each row, column, and diagonal individually for equality is emphasized. Overall, the conversation aims to refine the implementation of the magic square functionality in Java.
eechord
Messages
1
Reaction score
0

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:
Physics news on Phys.org
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)).
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K