Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-10-2010, 12:00 AM
Member
 
Join Date: Jan 2010
Posts: 31
Rep Power: 0
vendetta is on a distinguished road
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?


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>();
	
	
	//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(){
	
             }
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-10-2010, 12:17 AM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 456
Rep Power: 1
travishein is on a distinguished road
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.

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-10-2010, 12:24 AM
Member
 
Join Date: Jan 2010
Posts: 31
Rep Power: 0
vendetta is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-10-2010, 12:26 AM
Member
 
Join Date: Jan 2010
Posts: 31
Rep Power: 0
vendetta is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-10-2010, 09:14 PM
travishein's Avatar
Senior Member
 
Join Date: Sep 2009
Location: Canada
Posts: 456
Rep Power: 1
travishein is on a distinguished road
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.
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Code to make an Applet that downloads? Leeky Java Applets 0 09-05-2009 10:56 PM
Convert java code to midlet code coldvoice05 Advanced Java 1 08-09-2009 01:21 PM
Eclipse don't take on consideration the code chages I make! karim Eclipse 4 03-25-2009 01:54 PM
Java code that allows me to make and receive calls, send and receive sms nareshbabu@live.in Networking 0 12-02-2008 10:55 AM
Generating Code Automatically Using Custom code Template In Eclipse JavaForums Eclipse 1 04-26-2007 03:52 PM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:18 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org