1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Magic Square- Java

  1. Jan 11, 2012 #1
    1. The problem statement, all variables and given/known data

    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.


    2. Relevant equations

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

    3. The attempt at a solution
    Code (Text):

    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: Jan 11, 2012
  2. jcsd
  3. Jan 12, 2012 #2

    CompuChip

    User Avatar
    Science Advisor
    Homework Helper

    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)).
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Magic Square- Java
  1. Java program (Replies: 3)

  2. Java prob. (Replies: 1)

  3. HeapSort Java (Replies: 3)

Loading...