tags:

views:

78

answers:

2

For a CS class I need to solve an assigned problem using three data structures: Queue, PriorityQueue, and Stack. I wanted to write a single solution to the problem using an abstract data structure. I would implement the ADT with a wrapper class of each required data type. This is what I have so far:

An interface called Method:

public interface Method<E> {

    public abstract void add(E data);
    public abstract E remove();
    public abstract E peek();
    public abstract Iterator<E> Iterator();
}

And three wrapper classes that implement the interface. I called them QueueMethod, StackMethod, and PriorityQueueMethod. I'm having some trouble implementing the interface though. This is a start for the implementation that gives the error "Class is not abstract and does not override abstract method add(java.lang.Object)." As far as I can tell the signature of the two add methods are identical.

Here's the beginning QueueMethod wrapper class:

public class PriorityQueueMethod<T> implements Method {

    PriorityQueue<T> queue;

    public PriorityQueueMethod() {
        queue = new PriorityQueue<T>();
    }

    public void add(T data) {
        queue.offer(data);
    }
}
+5  A: 

Add generic to the Method class you are implementing, like this:

public class PriorityQueueMethod<T> implements Method<T> 
Emil
+2  A: 

Use the generic signature in your implements declaration:

public class PriorityQueueMethod<T> implements Method<T>

Here's a sample implementation for an ArrayList based solution:

public class ArrayListMethod<T> implements Method<T>{

    private final List<T> inner;

    public ArrayListMethod(){
        inner = new ArrayList<T>();
    }

    public ArrayListMethod(final Collection<T> data){
        inner = new ArrayList<T>(data);
    }

    @Override
    public void add(final T data){
        inner.add(data);
    }

    @Override
    public T remove(){
        return inner.remove(0);
    }

    @Override
    public T peek(){
        return inner.get(0);
    }

    @Override
    public Iterator<T> Iterator(){
        return inner.iterator();
    }
}
seanizer