0

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);

5
  • Does this answer your question? javac : Compiling a .java file which uses other classes in it Commented Sep 29, 2022 at 8:49
  • @Turamarth Thanks for your reply, I tried the command javac Percolation.java PercolationStats.java (same as javac *.java) and the code can be compiled successfully. However, when I try java PercolationStats.java to run, it still shows error error: cannot find symbol Commented Sep 29, 2022 at 8:56
  • 1
    java PercolationStats.java is for single file source code execution which explicitly limits you to code from a single file (and doesn't require the earlier javac call). 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!). Commented Sep 29, 2022 at 9:09
  • @JoachimSauer Thanks! I tried javac Percolation.java PercolationStats.java -> java PercolationStats and it still turned out that it couldn't find the Percolation class. Maybe I'll try to reset my java environment. Commented Sep 29, 2022 at 12:36
  • Do you have the CLASSPATH variable set? Try explicitly providing -cp . to both javac and java Commented Sep 29, 2022 at 13:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.