making my code a little better, if needed.
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?
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>();
//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(){
}
}
Re: making my code a little better, if needed.
I'm not entirely sure I understand the logic here, you're passing in 12 into the mainMethod call as well as the constructor, why?
What is the mainMethod eventually supposed to do?
// Json
Re: making my code a little better, if needed.
There is nothing wrong with having multiple constructors in the same class. In fact, it's very common.
It's hard to tell what you're trying to do, but if mainMethod should always use the same data as the constructor, it makes sense to make it a private method, and call it from within the constructor itself.
Re: making my code a little better, if needed.
well, I'll try to answer both of you best as I can. So, the project is to make a method with one argument that passes an integer such as 5, 12 or 20. that number is the number of queens that will be placed on a virtual checker board. If we request that the number of queens is 12, then the board size is a 12 x 12 square.
Json, I am passing 12 into the constructor to simply setup the display array that would show graphically where the queens get located. I don't know how to do this any other way. The main method's function is to find the locations of the queens. If we pass 12 into the method, then the method will find the 12 locations of where to put the queens. The instructions say to pass the number of queens into the method. Passing the 12 into the constructor is just an idea I came up with, so it's on the chopping block to change.
Shambolic, I see quite a few java classes that have multiple constructors, but one thing I haven't seen is a class that has one constructor with instance variables A and B and another constructor that has instance variables C and D, so it left me wondering if it was bad practice because the two constructors don't relate with each other and have different functions. One constructor sets up an array and is called just once for the driver/test file. The other constructor is called for every queen instance after the first constructor is called. The project uses stacks, the item that is stacked is an object that the second constructor is responsible for. The second constructor gets called 12 times if were locating 12 queens on a 12x12 board.
Re: making my code a little better, if needed.
Its not good practise to have a constructor which is public and used only for testing since that constructor will be available to anyone later on. When doing testing you want to make sure you can create an instance of an object just like as if it was the real thing, testing is supposed to be as close to the live runtime as possible.
// Json