0

I am new to Java and am having an issue calling a method. I was hoping someone might be able to help me figure out what is going on.

The code I have is as follows:

public class QuickFindUF
{
    private int[] id;
    public QuickFindUF(int N)
    {
        id = new int[N];
        for (int i = 0; i < N; i++)
            id[i] = i;
    }

    public boolean connected(int p, int q)
    { return id[p] == id[q]; }

    public void union(int p, int q)
    {
        int pid = id[p];
        int qid = id[q];
        for (int i = 0; i < id.length; i++)
            if (id[i] == pid) id[i] = qid;
    }
}

I took a look on Stack and figured the way to call my method would be using the following code: QuickFindUF x = new QuickFindUF(10);

When I run this I get an error that says

QuickFindUF.java:27: error: class, interface, or enum expected
QuickFindUF x = new QuickFindUF(10);
^
1 error

If someone could point me in the right direction I would really appreciate it. Thanks.

2
  • 3
    The 27 is the line number that's causing the problem. It looks like the problem is in code you haven't shown us. (The line calling new QuickFindUF isn't part of the file you've shown so far.) Commented Feb 5, 2015 at 17:55
  • I suspect you're not calling your constructor from within a main() method (or similar) Commented Feb 5, 2015 at 17:57

2 Answers 2

3

If the code you posted is your complete code, it appears you need a main method.

public class QuickFindUF
{
    //
    // add this so you can run code when your program executes
    //
    public static void main(String[] args)
    {
        QuickFindUF x = new QuickFindUF(10);
        //
        // call your methods on x here
        // e.g.
        // boolean connected = x.connected(2, 3);
        //
    }

    private int[] id;
    public QuickFindUF(int N)
    {
        id = new int[N];
        for (int i = 0; i < N; i++)
            id[i] = i;
    }

    public boolean connected(int p, int q)
    { return id[p] == id[q]; }

    public void union(int p, int q)
    {
        int pid = id[p];
        int qid = id[q];
        for (int i = 0; i < id.length; i++)
            if (id[i] == pid) id[i] = qid;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! I've got one more quick question: when I run x.union(0,1);, outside the code block, I don't get an error. But when I try to run x.show(); to see the output, I get an error. Any clue why that would happen?
What does your show() method do? What is the error you're getting? And what do you mean by outside of the code block?
Sorry for not being more clear and for the blatant typo (should say "inside the code block"). I thought .show() allowed me to see the output. When I ran x.union(0,1) and x.show where you said to call the methods on x I get an error that says: error: cannot find symbol x.show(); ^ symbol: method show() location: variable x of type QuickFindUF 1 error
That error means that you do not have a method named show defined on the class. Based on the posted code, you only have union and connected as available methods. You probably want to use x.toString(). It is built in, however, to make it useful you should define your own public string toString() { return "foo"; } Just replace "foo" with actual code that displays your object.
1

Your main method might be outside the class , You need to declare main method inside the class like this way :

public static void main(String []args){

QuickFindUF x = new QuickFindUF(10);

}

Comments

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.