0

I need to write a program to solve the eight queens problem and I have no idea how to do it, but I have already started with the multidimensional array. The thing is that I don't know how to call a constructor from the main class, just to check if the array was constructed properly, or how to call it from a method. Can anybody help, please? This is the code, it returns null.

import javax.swing.JOptionPane;


public class Eightqueens

{
    private static int[][] board;

    public static void main(String[] args) 
    {
        JOptionPane.showMessageDialog(null,board);
    }

    public Eightqueens (int size)
    {
        size = 8;
        board = new int[size][size];

        int row;
        int col;

        for (row = 0; row < size; row++)
            for (col = 0; col < size; col++)
                board[row][col] = 0;
    }

}
1
  • The constructor is only called, if you're instantiating an object from this class. So the question is, where you want to instantiate this class?
    – bobbel
    Oct 31, 2014 at 12:19

7 Answers 7

6

You are supposed to use the value passed to the constructor, not overwrite it :

...
private int[][] board;
...
public Eightqueens (int size)
{
    size = 8; // remove this
    ...
}

public int[][] getBoard() 
{
    return board;
}

public static void main(String[] args) 
{
    Eightqueens equeens = new Eightqueens (8); // create an instance
    JOptionPane.showMessageDialog(null,equeens.getBoard()); // use accessor to get
                                                            // board
}

In addition, it doesn't make sense to initialize a static variable (board) in the constructor, since each new instance you create would overwrite its value. I'd change it to non-static, and then you can get it from your instance by using an accessor method (equeens.getBoard()).

0

To call your constructor you must 'construct' a new object.

Eightqueens obj = new Eightqueens(5);
0

just when you instantiate your object of class with new keyword , you call your constructor. like this :

public static void main(String[] args) {
    Eightqueens eq = new Eightqueens(5);
}
1
  • 1
    That's funny, why did we all chose 5 as a random size parameter ?
    – Dici
    Oct 31, 2014 at 12:20
0

You can start by calling your constructor by making a new instance of Eightqueens

int size = 1; // anything
new Eightqueens(size);
0

This code does not return null, it does not return anything. You never call your constructor :

public static void main(String[] args) {
    Eightqueens eq = new Eightqueens(5);
}
0

You can't call your constructor again. It only runs at the start of the code.

You can however, create a new instance of it to run it again.

0

The Dialog Box is empty because the value of board is nothing, which is because it has not been initialized. The board get a value in the constructor method. So you have to call the constructor method before you show the dialog box.

public static void main(String[] args) 
{
      Eightqueens eq = new Eightqueens(8);
      JOptionPane.showMessageDialog(null,board);
}

Also, in the constructor method, you are overwriting the value of size given by you earlier. Thus, there is practically no need to supply the variable to the method. So, either you remove the line:

size = 8;

or, change the constructor method to not input the value, like this:

public Eightqueens ()
{
    int size = 8;
    board = new int[size][size];
    int row;
    int col;

    for (row = 0; row < size; row++)
        for (col = 0; col < size; col++)
              board[row][col] = 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.