Results 1 to 5 of 5
  1. #1
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default 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(){
    	
                 }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.

    Java Code:
    public static NQueens getSquareGrid(int size) {
      return new NQueens(size, size);
    }
    where here we removed the one argument constructor with this method that just calls the two argument one with the same argument twice.

  3. #3
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    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.

  4. #4
    vendetta is offline Member
    Join Date
    Jan 2010
    Posts
    31
    Rep Power
    0

    Default

    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

  5. #5
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.
    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;
    }
    .. but now its a 3 args constructor.

Similar Threads

  1. Code to make an Applet that downloads?
    By Leeky in forum Java Applets
    Replies: 0
    Last Post: 09-05-2009, 10:56 PM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Replies: 4
    Last Post: 03-25-2009, 01:54 PM
  4. Replies: 0
    Last Post: 12-02-2008, 10:55 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •