Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this Java Data Structures Assignment. Thanks Array Based Stack

ID: 3815514 • Letter: I

Question

I need help with this Java Data Structures Assignment. Thanks

Array Based Stack Class - You will write the ArrayBasedStack.java class which will implement the Stack Interface. Please note that Stack Interface extends the Iterable Interface.

Information On The Iterable Interface - The Iterable Interface requires you to implement one method Iterator iterator().

Element Iterator Class - You will write the ElementIterator.java class which will implement the Iterator Interface. Please note, you only need to implement two methods from the Iterator interface: boolean hasNext() and E next().

Stack Interface Methods

Iterable Interface Method

Iterator Interface Methods

Interface Stacklnterface

Explanation / Answer

Interface Stack

public interface Stack<E, T> extends Iterable<T>{
   /**
   * Removes All elements from the stack
   */
   void clear();
   /**
   * This method is called to determine if the stack is empty
   * @return
   */
   boolean isEmpty();
   /**
   * Retrieves but does not remove the top of the stack
   * @return
   */
   E peek();
   /**
   * Retrieves and removes the element at top of the stack
   * @return
   */
   E pop();
  
   /**
   * Pushed the specified element into this queue
   * @param e
   */
   void push(E e);

   /**
   * To obtain the count of element in the stack
   * @return
   */
   int size();
   }

ArrayBasedStack.java

import java.util.Iterator;

public class ArrayBasedStack implements Stack<Object, Object> {
   private Object[] theArray;
   private int topOfStack;

   private static final int DEFAULT_CAPACITY = 10;

   public ArrayBasedStack() {
       theArray = new Object[DEFAULT_CAPACITY];
       topOfStack = -1;
   }

   @Override
   public Iterator<Object> iterator() {
       return new ElementIterator<Object>(this);
   }

   @Override
   public void clear() {
       topOfStack = -1;
   }

   @Override
   public boolean isEmpty() {
       return topOfStack == -1;
   }

   @Override
   public Object peek() {
       if (isEmpty())
           throw new IllegalStateException("ArrayBasedStack peek");
       return theArray[topOfStack];
   }

   @Override
   public Object pop() {
       if (isEmpty())
           throw new IllegalStateException("ArrayBasedStack pop");
       return theArray[topOfStack--];
   }

   @Override
   public void push(Object e) {
       if (topOfStack + 1 == theArray.length)
           doubleArray();
       theArray[++topOfStack] = e;

   }

   @Override
   public int size() {
       return topOfStack+1;
   }

   private void doubleArray() {
       Object[] newArray;
       newArray = new Object[theArray.length * 2];
       for (int i = 0; i < theArray.length; i++)
           newArray[i] = theArray[i];
       theArray = newArray;
   }

}

ElementIterator<E>.java

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

public class ElementIterator<E> implements Iterator<E> {
   private ArrayBasedStack theArray; //ArrayList<Vector> will be set here

public ElementIterator(ArrayBasedStack theArray) {
this.theArray = theArray;
}

   @Override
   public boolean hasNext() {
       return !theArray.isEmpty();
   }

   @Override
   public E next() {
       if(hasNext()) {
return (E) theArray.pop();
} else {
throw new NoSuchElementException("There are no elements size = " + theArray.size());
}
   }

}

Test.java

public class Test {

   public static void main(String[] args) {
       ArrayBasedStack newStack = new ArrayBasedStack();
newStack.push("Element1");
newStack.push("Element2");
newStack.push("Element3");

System.out.println(newStack.size());
System.out.println(newStack.peek());
newStack.pop();
System.out.println("Size..."+newStack.size());
System.out.println(newStack.isEmpty());

       ElementIterator<?> iter = (ElementIterator<?>) newStack.iterator();

       while(iter.hasNext()) {
           String animalObj = (String)iter.next();
           System.out.println(animalObj);
       }
   }

}