Question

I'm receiving the error "type Stack does not take parameters public class ArrayStack implements Stack" from this code:

public class ArrayStack<E> implements Stack<E> {

private E[] data;

private int size;

public ArrayStack() {
data = (E[])(new Object[1]);
size = 0;
}

public boolean isEmpty() {
return size == 0;
}

public Object pop() {
if (isEmpty()) {
    throw new EmptyStructureException();
}
size--;
return data[size];
}

public Object peek() {
if (isEmpty()) {
    throw new EmptyStructureException();
}
return data[size - 1];
}

protected boolean isFull() {
return size == data.length;
}

public void push(Object target) {
if (isFull()) {
    stretch();
}
data[size] = target;
size++;
}

protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]);
for (int i = 0; i < data.length; i++) {
    newData[i] = data[i];
}
data = newData;
}   
}

"type Stack does not take parameters public class ArrayStack implements Stack"

The stack class is as follows.

public interface Stack<E> {

public boolean isEmpty();

public E peek();

public E pop();

public void push(E target);

}
Was it helpful?

Solution

Your peek() method should like this

    public E peek() throws EmptyStructureException {
        if (isEmpty()) {
          throw new EmptyStructureException();
        }
        return (E)data[size - 1];
    }

Your push() method should like this

   public void push(E target) {
         if (isFull()) {     
            stretch();
         }
         data[size] = target;
         size++;
   }

Your pop() method should like this

    public E pop() throws EmptyStructureException {
         if (isEmpty()) {
             throw new EmptyStructureException();
         }
         size--;
         return (E)data[size];
    }

Now your interface look like below

public interface Stack<E> {

      public boolean isEmpty();

      public E peek() throws EmptyStructureException;

      public E pop() throws EmptyStructureException;

      public void push(E target);

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top