O_mini 0 Newbie Poster

Program created in BlueJ:
For this Assignment I am to create a magic square from odd numbers >= 3 input by the user.

I want to test my code to see if it works, but I can't figure out how to actually print the square from the number input by a user. When the program compiles, and I run it the terminal window doesn't even show up, before I started messing around trying to figure out the printSquare method, it would show and ask for user input.

My program has to be in 2 classes. MagicSquare and MagicSquareTester.

public class MagicSquare
    private int n;                        //Holds size of MagicSquare
    private int [][] magicSquare;         //Create a 2d array named magicSquare
    private int rowSum = 0;               
    private int colSum = 0;               //To hold the sums
    private int diagSumLR = 0;
    private int diagSumRL = 0;
    private boolean magicRow = true;      //To check if all rows/cols are equal
    private boolean magicCol = true;
    private boolean magicDiagLR = true;   //Checks if diagonals are equal
    private boolean magicDiagRL = true;
    int num = n*(n*n + 1) / 2;            //MagicSquare equation to check is col/row/diag sums
                                          //are equal
    //Create a constructor that assigns space for the 2d array of the MagicSquare
    public MagicSquare(int num)
        n = num;                           //@n declare variable num to hold values
        magicSquare = new int [num][num];  //2d array named magicSquare that has certain "num"ber of 
                                           //rows and columns, to be determined by user input
    //Method statement to fill the array with numbers from 1 to n^2 in ascending order
    //starting at the bottom, middle cell. Repeatedly assign the next integer to the cell next to it,
    //right down diagonally. If that cell has already been filled, use the cell above the one you
    //are moving from.
    public void makeSquare()
        int posA = n-1;                   //starting at the bottom row
        int posB = n/2;                   //in the middle
        //for loop for starting at number 1
        for(int i = 1; i <= n*n; i++)
            magicSquare[posA][posB] = i;       //put following number inside square then
            posA++;                            //move down 1 & then
            posB++;                            //move right 1
            //If the last location from the move was at the bottom edge and the
            //next location is occupied, need to make an adjustment
            if(posA == n && posB ==n)      //if previous spot was at right bottom edge of square
            {                              //instead of wrapping around
                posA = n-2;                //back track 2 spaces
                posB = n-1;                //and move over 1 space above previous location
            //If number is at bottom edge, wrap around square to top part
            if(posA >= n)
                posA = 0;
            //If number is at right edge, wrap around right edge to left side
            if(posB >=n)
                posB = 0;
            //If next spot is occupied
            if(magicSquare[posA][posB] !=0)
                posA -= 2;                   //Move to position where you were previously
                posB--;                      //and go above it
    //Method to calcuate rowSum, colSum, and diagSum
    public void checkSquare()
        //Go through row/cols & add up everything
        for (int row = 0; row < n; row++)
            for(int col = 0; col < n; col++)
                rowSum += magicSquare[row][col];
                colSum += magicSquare[row][col];
                //Add up diagonals leftbottom to upright & rightbottom to upleft
                //@n ie. n=3 - row 0 - 1 = row 2, col 2 = value 6 plus...
                //n=3 - row 1 - 1 = row 1, col 1 = value 5
                diagSumLR += magicSquare[row][col];
                diagSumRL += magicSquare[n-row-1][col];
            //All calculated sums must equal the equation in order to count as MagicSquare
            magicRow = rowSum == num;
            magicCol = colSum == num;
            magicDiagLR = diagSumLR == num;
            magicDiagRL = diagSumRL == num;

    public static void printSquare()
     This is my trouble spot

Tester Class

import java.util.Scanner;
public class MagicSquareTester
    public static void main(String[] args)
        Scanner keyboard = new Scanner(System.in);
        int input = Integer.parseInt(keyboard.nextLine());
        Am I properly accessing my MagicSquare class?
        MagicSquare mSquare = new MagicSquare(input);

        //Ask the user for an odd number
        System.out.print("Enter an odd number >= 3 to create a Magic Square: ");
        input = keyboard.nextInt();
        //Ensure input is greater than 3, otherwise prompt user
        while( input < 3 || (input % 2) == 0 )
            System.out.println("Invalid entry. Please enter an number >=3");
            input = keyboard.nextInt();

       How do I call printSquare and print it out?

Tips, tricks, explanations would be great. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.