I am absolutely new to Java and having trouble referencing a class that I have declared in another .java file. I read a lot about it on the Oracle Website and also many stackoverflow questions, but couldn't solve the problem. Here are the steps I have taken:
Set the class path.
I am running this on UNIX. So I typed in
% java -classpath /users/myUserName/algs4/assignment1. Instead of changing the class path it gives me a long list of other options.
Include the 'package name' in each of my .java files which have class definitions I want to refer to.
I also ensured that all my files that I want to refer are in the same directory. Here is some of my code:
package assignment1;
public class Percolation
{
private int[][] grid;
....
//Other method definitions etc.
public Percolation(int N)
{
uf = new WeightedQuickUnionUF(N);
grid = new int[N][N];
...
//Other code
}
}
My WeightedQuickUnionUF class is also defined as:
package assignment1;
public class WeightedQuickUnionUF {
private int[] id; // id[i] = parent of i
private int[] sz; // sz[i] = number of objects in subtree rooted at i
private int count; // number of components
//Create an empty union find data structure with N isolated sets.
public WeightedQuickUnionUF(int N) {
count = N;
id = new int[N];
sz = new int[N];
for (int i = 0; i < N; i++) {
id[i] = i;
sz[i] = 1;
}
}
}
But now when I compile using javac Percolation.java. It gives me the errors:
Percolation.java:7: cannot find symbol
symbol : class WeightedQuickUnionUF
location: class assignment1.Percolation
WeightedQuickUnionUF uf;
^
I get it that it is because the compiler doesn't know what is the class WeightedQuickUnionUF. It cannot refer it. But how do I do it then? I have already tried the popular solutions.
I am not using any IDE. Just a text editor and then compiling it on the terminal.
(algs4 folder has files like stdlib.jar and others)
EDIT: I missed the 'new' keyword. I have added that. Also I am compiling my WeightedQuickUnionUF class before Percolation.java