Results 1 to 5 of 5
Thread: how to make my code better
- 02-10-2010, 12:00 AM #1
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
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?
Java 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();{ } } }
Java 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(){ } }
- 02-10-2010, 12:17 AM #2
if you want the ability to have an instance created where one parameter is specified and it creates a square grid, or two parameters are specified and it creates a rectangle grid, if you want these two options, then you need the two constructors.
Java does not have default values for arguments like some other languages do.
if you want to have one constructor, you can try to create a static factory method.
where here we removed the one argument constructor with this method that just calls the two argument one with the same argument twice.Java Code:public static NQueens getSquareGrid(int size) { return new NQueens(size, size); }
- 02-10-2010, 12:24 AM #3
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
the two argument constructor though will have different values in them. queen location: row 1, column 2 and one might be: row 7, column 4.
- 02-10-2010, 12:26 AM #4
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
the grid is always square. one constructor sets the size
the other constructor has different values in it and that one is for the queen location
- 02-10-2010, 09:14 PM #5
oh, then the 2 args constructor can call the single arg one first, and then do the other stuff specific to initial placement of the queen.
.. but now its a 3 args constructor.Java Code://this constructor creates an instance of a queen location. public NQueens(int x, int rowIn, int columnIn) { this(x); row = rowIn; column = columnIn; }
Similar Threads
-
Code to make an Applet that downloads?
By Leeky in forum Java AppletsReplies: 0Last Post: 09-05-2009, 10:56 PM -
Convert java code to midlet code
By coldvoice05 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 01:21 PM -
Eclipse don't take on consideration the code chages I make!
By karim in forum EclipseReplies: 4Last Post: 03-25-2009, 01:54 PM -
Java code that allows me to make and receive calls, send and receive sms
By nareshbabu@live.in in forum NetworkingReplies: 0Last Post: 12-02-2008, 10:55 AM -
Generating Code Automatically Using Custom code Template In Eclipse
By JavaForums in forum EclipseReplies: 1Last Post: 04-26-2007, 03:52 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks