2

Full Error shown in the compiler: ListPriorityQueue.java:11: error: cannot find symbol public class ListPriorityQueue > implements PriorityQueue ^ symbol: class PriorityQueue I just don't understand because the error shown is not exactly very informative. PriorityQueue does exist in the package.

package data_structures;

import java.util.Iterator;
import java.util.NoSuchElementException;


/**
 * This is a linked list implementing ListADT interface
 * @param <E> The data type of the linked list
 */
public class ListPriorityQueue <E extends Comparable<E>> implements PriorityQueue<E>
{

       int count;           //number of elements in the queue
       Node<E> first;       //Head of the queue
       Node<E>  last;           //Tails of the queue
       int capacity= DEFAULT_MAX_CAPACITY;

       //Constructor
       public ListPriorityQueue()
       {
          first = null;
          last = null;
       }

       public ListPriorityQueue(int capacity )
       {
          first = null;
          last = null;
          this.capacity= capacity;
       }
            public boolean insert(E object) 
        {
            if(this.isFull())
                return false;

            Node<E> prev = null;
             Node<E> current = this.first;
             Node<E> temp = new Node<E>(object);
             // while (current != null && current.getData().pValue >= item.pValue)
             while (current != null && current.getData().compareTo(temp.getData())<0 )
              {
                 prev = current;
                 current = current.getNext();
              }

              if (prev == null)
              {
                 temp.setNext(this.first);
                 this.first = temp;
              }
              else
              {
                 temp.setNext(current);
                 prev.setNext( temp);
              }  

              count++;
            return true;
        }


       public E remove() 
       {
           E result;
           //If the list is empty
           if(count==0)
           {
               return null;
           }

           result= first.getData();
           first= first.getNext();

           count--;
           if(count==0)
           {
               last=null;
           }
           return result;
       }


            public E peek() 
        {
            if(count==0)
            {
                return null;
            }

            return first.getData();  //Return first element without removing
        }



       public int size()
       {
           return count;
       }
       public boolean contains (E target) 
       {
           boolean found= false;

           //If there is no element in the list
           if (count==0)
           {
               return found;
           }


          Node<E> current;
          current = first;

          // iterate the list looking for target
          while(!found && current != null)
          {
              //If current node data same as target
              if(target.equals(current.getData()))
              {
                 found = true;
              }
              else
                 current = current.getNext();
          }

          return found;
       }

           public Iterator<E> iterator()
       {
          return new TheIterator();
       }
            public void clear() 
        {
            //Loop to removeFirst till the list is empty 
            while (!this.isEmpty()) 
            {
                    this.remove();
             }

        }

       public boolean isEmpty()
       {
           if(count == 0){
           return true;}
           else{return false;}
       }

        public boolean isFull() 
        {
            if(count == capacity){  
              return true;}
              else { return false;}
        }









class Node<E>
{
  private Node<E> next; //Next element
  private E data;       //Data


 //Initialize Node
  public Node (E obj)
  {
    next = null;
    data = obj;
  }

  //Return next node
  public Node<E> getNext()
  {
    return next;
  }

  //Set the next node
  public void setNext (Node<E> node)
  {
    next = node;
  }

  //Get data of the node
  public E getData()
  {
    return data;
  }

  //Set node data
  public void setData (E obj)
  {
    data = obj;
  }
} 


/**
 * 
 * Iterartor for the passed in collection
 * @param <E>
 */
class TheIterator<E> implements Iterator<E>
{

   private Node<E> current; 


    public TheIterator ()
    {
       current = (Node<E>)first;     
    }


    public boolean hasNext()
    {
    return current != null;               
    }


    public E next() 
    {
       if ( hasNext() == false) //If there are no more elements
          throw new NoSuchElementException();

       E result = current.getData();
       current = current.getNext();
       return result;
    }


    public void remove()
    {
       throw new UnsupportedOperationException(); //remove not supported
    }
}
}
user2983046
  • 21
  • 1
  • 3
  • 3
    You're missing a lot of imports. – Kayaman Nov 12 '13 at 11:57
  • How are you compiling? `javac`? IDE? – artbristol Nov 12 '13 at 13:54
  • Yeah Im compiling using java not the IDE. When I compile with the IDE, it works. With javac, it shows the error. – user2983046 Nov 12 '13 at 14:00
  • Priority Queue is an interface that is implemented in the code which is also in the same package. I am told not to import the Priority Queue. – user2983046 Nov 12 '13 at 14:28
  • 1
    See http://stackoverflow.com/questions/4800781/how-to-compile-multiple-java-source-files-in-command-line – artbristol Nov 12 '13 at 14:31
  • Show your compile command. – Ian McLaird Nov 12 '13 at 14:33
  • I'm not too sure but I have tried compiling all the java files together through javac and it did work. But I thought I should check one by one. Is there a reason why it wouldn't work if I did it one by one? – user2983046 Nov 12 '13 at 14:36
  • My compile command is javac *.java and I also tried javac file1.java file2.java etc.. etc.. Both of them worked, but when I just comile one file like javac file1.java, it shows the error. – user2983046 Nov 12 '13 at 14:37
  • Yes. In order to compile, you need class files for all of your dependencies. These can be in a jar on the compile command's `classpath`, or in the `-sourcepath` parameter, or in other java files specifically named. – Ian McLaird Nov 12 '13 at 14:39

1 Answers1

0

When your compiler says

ListPriorityQueue.java:11: error: cannot find symbol 
public class ListPriorityQueue implements PriorityQueue 
                                          ^ 
    symbol: class PriorityQueue

That's its way of saying "What's a PriorityQueue? I see that you want to implement this, but I don't know what it is!"

javac must be told where all the parts of your program are so that it can verify all of the code for the program is correct, and fits together properly. There's no way to compile just a single file if it has dependencies on other code. You have to give it all the information. There are several ways to accomplish this.

  1. List the files specifically.

    javac ListPriorityQueue.java PriorityQueue.java
    

    This tells it that you're only interested in these two files. You're saying "between these files and the standard library this is everything you need to know, compiler."

  2. Use the sourcepath parameter to the compiler (Note that this command is run one folder up on the path from where the source is actually located, rather than from the same folder).

    javac -sourcepath data_structures data_structures/PriorityQueue.java
    

    Number 1 above works great when you've only got a couple of files, but as you'll soon learn if you stick with java programming, you'll almost never have just a couple of files. So the compiler designers added the ability to give it a whole folder tree to work with. When you do this, you're saying to the compiler "I want you to compile PriorityQueue.java, and if there's something you don't recognize, look in the data_structures folder."

  3. Using the classpath parameter to the compiler (this won't work in your specific case, mind you, I'm just including it for completeness).

    javac -classpath data_structures_api.jar PriorityQueue.java
    

    This says to the compiler "I want you to compile PriorityQueue.java. I don't have source code for my dependencies, but they've already been compiled, and the result of that compilation is in data_structures_api.jar. You can use that for anything you don't recognize." This is a very common thing to tell the java compiler, because most real-world applications will use 3rd party libraries that are distributed as .jar files.

  4. The above options can be used in combinations. It's very common to use both the sourcepath and classpath parameters at the same time. Compile your entire source tree, and use 3rd party libraries. This is actually what your IDE is doing for you, and why it worked there, but not on the command line.

Ian McLaird
  • 5,507
  • 2
  • 22
  • 31