how to make my code better
I am trying to find a way to make my layout better. I am doing a problem called N-queens which is displays N number of queens on a N x N grid. This project uses stacks, that is stacking objects. That object is labeled below as rAndC which would hold the row and column location for the queen. (one stack, two pieces of information stored) It would eventually output the locations of the queens on the grid using an array.
What I don't like about my setup is there are two constructors, one that makes an instance of the class for the driver/test file and one constructor to make an object for the queen location which is the row and column.
Can this be done so there aren't two constructors in one class?
Code:
public class MyQueens {
public static void main(String[] args) {
//Here were are calling the constructor to create an instance of
//the class and also to set the grid size for the display array which
//will output the layout of the queens.
NQueens MyQueens = new NQueens(12);
//integer below will be the number of queens
MyQueens.mainMethod(12);
//print
MyQueens.outputInformation();{
}
}
}
Code:
import java.util.Stack;
public class NQueens {
private int row;
private int column;
private int displaySize;
private char[][] grid;
Stack<NQueens> Stack = new Stack<NQueens>();
//this constructor creates an instance of the NQueens class for my
//driver/test file and also initializes the array with the size it needs to be.
public NQueens(int x) {
displaySize = x;
grid = new char [x][x];
}
//this constructor creates an instance of a queen location.
public NQueens(int rowIn, int columnIn) {
row = rowIn;
column = columnIn;
}
//Precondition: n will need to be 4 or larger.
//Postcondition: the queens have been placed in the correct locations.
public void mainMethod(int n){
NQueens rAndC;
}
//Precondition: The array "grid" has been initialized
//Postcondition: The grid of queen locations has been displayed.
public void outputInformation(){
}
}