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

.java Array Based Stack Implement the interface listed below to create a stack c

ID: 3690170 • Letter: #

Question

.java

Array Based Stack

Implement the interface listed below to create a stack class written in Java using an array. In addition, create a driver class that tests your stack class. The specification and implementation of our stack is different from the books implementation, but they are very similar.

Recall that a stack is a linear collection where insertion and deletion occur at one end only. A stack is a last in, first out (LIFO) data structure. The last element to be put on the stack will be the first element that gets removed. Stacks can be applied to numerous programming problems such as the evaluation of arithmetic expressions.

An abstract data type (ADT) defines the operations that can be performed on the collection (data structure). Whenever you create a data structure, you should formally list and describe the operations that can be performed on it. That way, you can separate the interface to the collection from the implementation technique used to create it.

Operation Description

push

Adds an element to the top of the stack.

pop

Removes an element from the top of the stack.

peek

Examines the element at the top of the stack.

isEmpty

Returns true if the stack is empty.

isFull

Returns true if the stack is full.

size

Returns the number of elements in the stack.

public interface StackIntADT

{

public void push(int element);

public int pop();

public int peek();

public boolean isEmpty();

public boolean isFull();

public int size();

}

Implementation Notes

Your stack class should implement the StackADT interface using an array of ints. The first element entered into your stack should reside at position 0 in the array. You should use a private field named stackPointer, or SP, to hold the index of the next available location within the stack. Both push and pop will modify the stack pointer.

You will need to provide at least two constructors for your stack. The default, zero- parameter, constructor should set the stack size to 10. The second constructor should allow the user to specify the number of elements in the stack.

A suggested IntArrayStack class is outlined below:

public class IntArrayStack implements StackIntADT

{

   // fields

   // constructors

   // stack class methods

}

So, I just need to create an IntArrayStack class, and a driver class to test. Just a simple integer stack code using an array, and not too complicated.

push

Adds an element to the top of the stack.

pop

Removes an element from the top of the stack.

peek

Examines the element at the top of the stack.

isEmpty

Returns true if the stack is empty.

isFull

Returns true if the stack is full.

size

Returns the number of elements in the stack.

Explanation / Answer

import java.util.Scanner;

interface StackIntADT {
   public void push(int element);

   public int pop();

   public int peek();

   public boolean isEmpty();

   public boolean isFull();

   public int size();
}

public class IntArrayStack implements StackIntADT {
   // fields
   private static int DEFAULT_SIZE = 10;
   private int stackPointer;
   private int[] myStack;
   private int size;

   // constructors
   public IntArrayStack() {
       myStack = new int[DEFAULT_SIZE];
       stackPointer = 0;
   }

   public IntArrayStack(int size) {
       if (size > 0) {
           myStack = new int[size];
           stackPointer = 0;
       }
   }

   // stack class methods
   // add an element to the stack
   public void push(int element) {
       // if(stackPointer == myStack.length)

       if (isFull() == true) {
           System.out.println("isfull");
       } else {
           stackPointer++;
           myStack[stackPointer] = element;
       }
   }

   // take an element off the stack
   public int pop() {
       if (isEmpty() == true) {
           System.exit(0);

       }
       return (stackPointer--);

   }

   // access the element specified
   public int peek() {
       if (stackPointer == -1) {
           return -1;

       } else
           return myStack[stackPointer];

   }

   // check if the stack is empty
   public boolean isEmpty() {
       return (stackPointer == -1);
   }

   // check if the stack is full
   public boolean isFull() {
       if (stackPointer == size - 1)
           return true;
       else
           return false;

   }

   public int size() {
       return myStack.length;
   }

   public static void main(String[] args) {
       Scanner b = new Scanner(System.in);
       StackIntADT ASImp;

       System.out
               .println("Enter a size > 0, or enter -1 if you want the default size of 10 ");
       int sizeIn = b.nextInt();
       if (sizeIn == -1) {
           ASImp = new IntArrayStack();
       } else {
           ASImp = new IntArrayStack(sizeIn);
       }
       ASImp.push(1);
       ASImp.push(2);
       ASImp.push(3);
       ASImp.push(4);
       boolean isFullRes = ASImp.isFull();
       System.out.println(isFullRes);
       int val = ASImp.pop();
       System.out.println(val);
       System.out.println(ASImp.peek());

   }

}

OUTPUT:

Enter a size > 0, or enter -1 if you want the default size of 10

-1
false
4
3