Magic Square Java Homework: Implement Class Square

  • Context: Comp Sci 
  • Thread starter Thread starter eechord
  • Start date Start date
  • Tags Tags
    Java Magic Square
Click For Summary
SUMMARY

The discussion focuses on implementing a Java class named Square to create and validate a magic square, defined as an n by n matrix where the sums of all rows, columns, and diagonals are equal. Key methods to be implemented include a constructor to initialize the matrix, printSquare() for displaying the matrix, isFull() to check if all numbers from 1 to n² are present, and isMagic() to verify the magic square condition. The discussion highlights common coding errors and provides tips for correct implementation.

PREREQUISITES
  • Java programming fundamentals
  • Understanding of two-dimensional arrays in Java
  • Basic knowledge of control structures (loops and conditionals)
  • Familiarity with the Scanner class for user input
NEXT STEPS
  • Implement the loadSquare() method using the Scanner class for user input
  • Research Java exception handling to manage input errors
  • Learn about unit testing in Java to validate the isFull() and isMagic() methods
  • Explore optimization techniques for checking magic square properties
USEFUL FOR

Students learning Java, software developers implementing algorithms, and anyone interested in matrix operations and validation techniques.

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
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K