here is the short version:
my first constructor has two variables in it.
my second constructor has two entirely different variables in it.
is this ok to do or is this bad practice?
here is the long version:
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 and that object is labeled below as rAndC which holds the row and column location for the queen. (one stack; "row" and "column" are stored on the stack) 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 creates an instance of the class for the driver/test file and one constructor to make an object instance for the queen location which is the row and column.
Can this be done so there aren't two constructors in one class or is this an ok practice?
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();{ } } }
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>(); //constructor #1. 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]; } //constructor #2. 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(){ } }