Lab 4 Directions Stacks Program 1 1 Show the ArrayStackADT i

Lab 4 Directions (Stacks) Program #1 1. Show the ArrayStackADT interface 2. Create the ArrayStackDataStrucClass with the following methods: default constructor, overloaded constructor, copy constructor, initializeStack, isEmptyStack, isFullStack, push, peek, void pop 3. Create the PrimeFactorizationDemoClass: instantiate an ArrayStackDataStrucclass object with 50 elements. Use a try- catch block in the main() using pushes/pops. 4. Exception classes: StackException, StackUnderflowException, StackOverflowException 5. Show the 4 outputs for the following: 3.960 1234 222,222 13,.780

Solution

1)package jss2;

import jss2.exceptions.*;

import java.util.Iterator;

public class ArrayStack<P> implements StackADT<P>

{

   private final int DEFAULT_CAPACITY = 100;

   private int top; // indicates the next open slot

   private transient P[] stack;

  

   // This is used to create an empty stack using the default capacity.

   public ArrayStack()

   {

      top = 0;

      stack = (P[])(new Object[DEFAULT_CAPACITY]);

   }

  

   // This is used to create an empty stack using the specified capacity.

   public ArrayStack (int initialCapacity)

   {

      top = 0;

      stack = (P[])(new Object[initialCapacity]);

   }

   // This function is used to add the specified element to the top of the stack, expanding the capacity of the stack array if necessary.

   public void push (P element)

   {

      if (size() == stack.length)

         expandCapacity();

      stack[top] = element;

      top++;

   }

  

   // The pop function removes the element at the top of the stack and returns a reference to it. Throws an EmptyStackException if the stack

   is empty.

   public P pop() throws EmptyStackException

   {

      if (isEmpty())

         throw new EmptyStackException();

      top--;

      P result = stack[top];

      stack[top] = null;

      return result;

   }

  

   // This function returns a reference to the element at the top of the stack.The element is not removed from the stack. Throws an EmptyStackException if the stack is empty.

   public P peek() throws EmptyStackException

   {

       if (isEmpty())

         throw new EmptyStackException();

       

       return stack[top-1];

   }

   // This function returns true if the stack is empty and false otherwise.

   public boolean isEmpty()

   {

        return (top == 0);

   }

   // This function returns the number of elements in the stack.

   {

        return top;

   }

   // This function returns a string representation of the stack.

   {

      String result = \"\";

      for (int scan=0; scan < top; scan++)

         result = result + stack[scan].toString() + \"\ \";

      return result;

   }

  

   // This function creates a new array to store the contents of the stack with twice the capacity of the old one.

      private void expandCapacity()

   {

      P[] larger = (P[])(new Object[stack.length*2]);

      for (int index=0; index < stack.length; index++)

         larger[index] = stack[index];

      stack = larger;

   }

}

 Lab 4 Directions (Stacks) Program #1 1. Show the ArrayStackADT interface 2. Create the ArrayStackDataStrucClass with the following methods: default constructor
 Lab 4 Directions (Stacks) Program #1 1. Show the ArrayStackADT interface 2. Create the ArrayStackDataStrucClass with the following methods: default constructor
 Lab 4 Directions (Stacks) Program #1 1. Show the ArrayStackADT interface 2. Create the ArrayStackDataStrucClass with the following methods: default constructor

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site