Java help

Kwerty

Expert Member
Company Rep
Joined
Jun 3, 2008
Messages
1,149
Reaction score
12
Location
Cape Town
Heya, I was hoping someone here can help. I have a CS project due for 2PM today (yes yes I know). I was actually finished but now I see they slightly changed the brief so here I am...

Anyways, I have a class called Percolation that takes an int and a boolean (boolean decides weather it uses Quick find or weighted quick find algorithm). The problem is that I need to declare my quick find structure before I know if the boolean is true or false, and I have no idea how to do that.

Here's what I have at the moment (without the boolean):
Code:
public class Percolation
        {
	private QuickFindUF uf;
	private int SIZE;
        private boolean[][] grid;

	public Percolation(int N)
	{
		grid = new boolean [N][N];
		uf = new QuickFindUF((N*N) + 2);
		SIZE = N;
	}

And this is the type of thing I need:
Code:
public class Percolation 
{
	private boolean[][] grid;
	private int SIZE;
	private Object uf;

	public Percolation(int N, boolean weighted) 
{
		grid = new boolean[N][N];

		if(weighted)
			uf = new WeightedQuickUnionUF((N*N)+2);
		else
			uf = new QuickFindUF((N*N)+2);

		SIZE = N;
}

Just casting it doesn't work because it doesn't have any of the properties it needs.

Any suggestions would be great,
Thanks
 
TBH that implementation should rather use OO concepts and you will easily fix this with some inheritance (interface / abstract class and concrete implementation of the different behaviours). There should really be to concrete implementations based on a common interface class.

I hope your downcasting example onto Object is just an example. As said, I would create an interface which then has a find-method. For generic stuff, create an abstract class implementing the interface which does the default functions. Then create specific implementations and override the find method.
 
TBH that implementation should rather use OO concepts and you will easily fix this with some inheritance (interface / abstract class and concrete implementation of the different behaviours). There should really be to concrete implementations based on a common interface class.

I hope your downcasting example onto Object is just an example. As said, I would create an interface which then has a find-method. For generic stuff, create an abstract class implementing the interface which does the default functions. Then create specific implementations and override the find method.

Asked someone to explain this to me and I got it working. Thanks for the reply
 
Top
Sign up to the MyBroadband newsletter
X