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

Write a Java program that implements a stack using an array. You can use the cod

ID: 3596506 • Letter: W

Question

Write a Java program that implements a stack using an array. You can use the code from the program below (which was written in C++) as a guideline. Demonstrate your program works using your main function.

#include <iostream>

#include <cstdlib>

using namespace std;

#define DEF_CAPACITY 10

template <class X>

class stack

{

X *arr;

int t;

int capacity;

public:

ArrayStack(int size = DEF_CAPACITY);

void push(X);

X pop();

X top();

int size();

bool empty();

bool isFull();

};

template <class X>

ArrayStack<X>::ArrayStack(int size)

{

arr = new X[size];

capacity = size;

t = -1;

}

template <class X>

void ArrayStack<X>::push(X x)

{

if (isFull())

{

cout << "OverFlow Program Terminated ";

exit(EXIT_FAILURE);

}

cout << "Inserting" << x << endl;

arr[++t] = x;

}


template <class X>

X ArrayStack<X>::pop()

{

if (empty())

{

cout << "UnderFlow Program Terminated ";

exit(EXIT_FAILURE);

}

cout << "Removing " << top() << endl;


return arr[t--];

}

template <class X>

X ArrayStack<X>::top()

{

if (!empty())

return arr[t];

else

exit(EXIT_FAILURE);

}

template <class X>

int ArrayStack<X>::size()

{

return t + 1;

}

template <class X>

bool ArrayStack<X>::empty()

{

return t == -1;

}

template <class X>

bool ArrayStack<X>::isFull()

{

return t == capacity - 1;

}
void grow(double pt[], int size)

{

int newsize = size * 2;

for(int t= 0; t<size; t++)

{

pt[t+size] = pt[t];

}

cout<<"twice of the array is: "<<endl;

for(int t=0;t<newsize;t++)

{

cout<<pt[t]<<" ";

}

cout<<endl;

}


int main()

{

ArrayStack<string> pt(2);

pt.push("Bob");

pt.push("Alice");

pt.top();

pt.pop();

pt.push("Eve");

cout << "Top element is " << pt.top() << endl;

cout << "ArrayStack size is " << pt.size() << endl;

cout<< "ArrayStack new size is "<<pt.

pt.pop();


if (pt.empty())

cout << "ArrayStack Is Empty ";

else

cout << "ArrayStack Is Not Empty ";

grow(pt,newsize)

return 0;

}

Explanation / Answer

Please find my implementation.

/* ArrayStack.java */

public class ArrayStack <Item>

{

   private Item container[];

   private int top;

   private final static int DEFAULT_SIZE = 5;

   public ArrayStack ()

   {

       this(DEFAULT_SIZE);

   }

   public ArrayStack (int initSize)

   {

       container = (Item[]) new Object [initSize];

       top = -1;

   }

   private void resize (int newSize)

   {

       Item t[] = (Item[]) new Object[newSize];

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

           t[i] = container[i];

       container = t;

   }

   public Item getTop()

   {

       if (top == -1)

           return null;

       return container[top];

   }

   public boolean isEmpty()

   {

       return (top == -1);

   }

   public Item pop()

   {

       if (top == -1)

           return null;

       Item itm = container[top];

       container[top--] = null; // avoid loitering

       return itm;

   }

   public void push(Item itm)

   {      

       if (top == container.length - 1)

           resize(2 * container.length);

       container[++top] = itm;

   }

   public int size()

   {

       return (top + 1);

   }

}

######################

public class ArrayStackDriver

{

   public static void main (String a[])

   {

       ArrayStack <String> stack1 = new ArrayStack<String>();

       ArrayStack <String> stack2 = new ArrayStack<String>();

      

       stack1.push("apple");

       stack1.push("orange");

       stack1.push("banana");

       stack1.push("grapes");

      

       stack2.push("apple");

       stack2.push("orange");

       stack2.push("banana");

       stack2.push("grapes");

   }

}

/*

Sampel run:

Size of the stack: 6

Following items pushed to Stack as of now:

70

60

50

40

30

20

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote