I need help with this coding At bottom is my test code A Cre

I need help with this coding. At bottom is my test code.

A. Create ArrayIntStack.java (one that\'s REALLY O(constant), totally!!!)

B. Create the Javadoc web page (separate Assignment #15b).

I need help with this coding.

A. Create your own Stack Class, call it ArrayIntStack.java and write the standard four public Stack methods as listed below (this is the easy part of the assignment). Use at least one private helper method, maybe to ensure capacity of the array. All methods in your ArrayIntStack must have O(constant) run-time. Your ArrayIntStack extends no other Classes from the Java API. You must provide a default constructor.

1. boolean empty(); Tests if this stack is empty (watch spelling here, as I will)
2. int peek(); Looks at the object at the top of this stack without removing it from the stack.
3. int pop(); Removes the object at the top of this stack and returns that object as the value of this function.
4. int push(int item); Pushes an item onto the top of this stack, return what was pushed.

Additional methods, code, etc... will also need to be provided, so the client can use an iterator:

5. public class IntStackIterator { // similar to what the BJP authors show, but without a remove() implemented
6. public IntStackIterator iterator() method so the client can browse through the data with .next() etc... Start the iterator at the top of the stack!!!

You must throw an \"appropriate\" Exception (e.g. EmptyStackException) for illegal peek, pop, etc... operations. Regarding size, let\'s say this remains O(constant) while the Stack size is less than 20 elements, beyond that the push(int) would need to throw a Stack Overflow.

public class Post {

public static void main(String[] args) throws FileNotFoundException {

// required:ArrayIntStack bag15 = new ArrayIntStack();try {

bag15.pop();

} catch (EmptyStackException e) {

System.out.println(e);}

bag15.push(42); bag15.push(24);

bag15.push(-33);// works without the generic Iterator from OracleArrayIntStack.IntStackIterator handle = bag15.iterator();while (handle.hasNext())

System.out.println(handle.next());

System.out.println(bag15.empty());

System.out.println(bag15.push(42));

System.out.println(bag15.pop());

System.out.println(bag15.peek());
// optional:// if you implement Iterable<Iterator>//for (Integer i : bag15) System.out.println(i);

}

}

Solution


class ArrayIntStack {
  
   protected int capacity;
   public static final int CAPACITY = 16;   // power of 2
   public static int MINCAPACITY=1<<15; // power of 2
   protected int[] stackRep;
   protected int top = -1;

   public ArrayIntStack() {
       this(CAPACITY); // default capacity
   }

   public ArrayIntStack(int cap) {
       capacity = cap;
       stackRep = new int[capacity]; // compiler may give warning, but this                          
   }

   public int size() {
       return (top + 1);
   }

   public boolean isEmpty() {
       return (top < 0);
   }

   public void push(int data) {
       if (size() == capacity)
           expand();
       stackRep[++top] = data;
   }
  
   private void expand() {
       int length = size();
       int[] newstack=new int[length<<1];   // or 2* length
       System.arraycopy(stackRep,0,newstack,0,length);
       stackRep=newstack;
   }
  
   public int peek() throws Exception {
       if (isEmpty())
           throw new Exception(\"Stack is empty.\");
       return stackRep[top];
   }

   public int pop() throws Exception {
       int data;
       if (isEmpty())
           throw new Exception(\"Stack is empty.\");
       data = stackRep[top];
       stackRep[top--] = Integer.MIN_VALUE; // dereference S[top] for garbage collection.
       return data;
   }
  
   public String toString() {
       String s;
       s = \"top [\";
       if (size() > 0)
           s += stackRep[0];
       if (size() > 1)
           for (int i = 1; i <= size() - 1; i++) {
               s += \", \" + stackRep[i];
           }
       return s + \"] bottom\";
   }
   public IntStackIterator iterator(){
       return new Iterater();
   }
   private class Iterater implements IntStackIterator{

       int curentPos = top;
       @Override
       public boolean hasNext() {
           if(curentPos>=0)
               return true;
           else
               return false;
       }

       @Override
       public int getNext() throws Exception {
           if(hasNext())
               return stackRep[curentPos];
           else
               throw new Exception();
       }
   }
}

interface IntStackIterator {
  
   public boolean hasNext();
   public int getNext() throws Exception;
}

I need help with this coding. At bottom is my test code. A. Create ArrayIntStack.java (one that\'s REALLY O(constant), totally!!!) B. Create the Javadoc web pag
I need help with this coding. At bottom is my test code. A. Create ArrayIntStack.java (one that\'s REALLY O(constant), totally!!!) B. Create the Javadoc web pag
I need help with this coding. At bottom is my test code. A. Create ArrayIntStack.java (one that\'s REALLY O(constant), totally!!!) B. Create the Javadoc web pag

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site