I'm doing the Algorithm homework of Princeton University, which asked us to finish 2 functions.
The question is, when I want to compile "PercolationStats.java" which needs the class "Percolation" in the same directory, It shows the error message:
PercolationStats.java:14: error: cannot find symbol
I tried to find the solution of "how to import default package" on Stackoverflow and turned out the answer that there's no need to import. I'm wondering the reason and I don't wanna use import package method since the homework spec said that both files should be in default package.
I will put both file I'd like to compile & run down below. I hope there's someone can tell me the reason/solution why this happened.
public class Percolation {
private boolean[] id;
private int number;
public WeightedQuickUnionUF a;
private boolean flag;
public Percolation(int var1) {
if (var1 < 1) {
throw new IllegalArgumentException("Please input parameter larger than 0!");
}
else {
this.number = var1;
this.id = new boolean[this.number * this.number + 2];
this.a = new WeightedQuickUnionUF(this.number * this.number + 2);
for (int var2 = 0; var2 < this.number * this.number + 2; ++var2) {
this.id[var2] = false;
}
}
}
}
public class PercolationStats {
private int trial_num;
private double[] log;
public PercolationStats(int var1, int var2) {
if (var1 >= 1 && var2 >= 1) {
Percolation pr = new Percolation(var1);
trial_num = var2;
}
else {
throw new IllegalArgumentException("Please make sure both input greater equal than 1!");
}
}
}
For readability, I only put the constructor of both class. And I used following command to compile and run:
$ javac PercolationStats.java
$ java PercolationStats.java
The compile error happened in:
Percolation pr = new Percolation(var1);
javac Percolation.java PercolationStats.java(same asjavac *.java) and the code can be compiled successfully. However, when I tryjava PercolationStats.javato run, it still shows errorerror: cannot find symboljava PercolationStats.javais for single file source code execution which explicitly limits you to code from a single file (and doesn't require the earlierjavaccall). If you want to run the class you just compiled, you need to specify the class name and not the full file name. I.e.java PercolationStats(without the.java!).javac Percolation.java PercolationStats.java->java PercolationStatsand it still turned out that it couldn't find the Percolation class. Maybe I'll try to reset my java environment.CLASSPATHvariable set? Try explicitly providing-cp .to bothjavacandjava