Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: making my code a little better, if needed.

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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?


     
    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(){
     
                 }
    }
    Last edited by vendetta; February 9th, 2010 at 07:39 PM.


  2. #2
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

  3. #3
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default 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.

  4. #4
    Junior Member
    Join Date
    Feb 2010
    Posts
    10
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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

Similar Threads

  1. Need help making program
    By ixjaybeexi in forum Collections and Generics
    Replies: 5
    Last Post: December 6th, 2009, 11:36 PM
  2. code needed for the following problem
    By romilc in forum Java Theory & Questions
    Replies: 1
    Last Post: October 11th, 2009, 10:05 AM
  3. Urgent code needed
    By subhvi in forum AWT / Java Swing
    Replies: 4
    Last Post: August 27th, 2009, 12:55 AM
  4. HELP "code needed"
    By Dave in forum File I/O & Other I/O Streams
    Replies: 14
    Last Post: August 26th, 2009, 11:06 AM
  5. Digital map application with Java GUI
    By donjuan in forum AWT / Java Swing
    Replies: 3
    Last Post: May 15th, 2009, 03:32 AM