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

Modify the Stack ADT implementations Utilizing your Stack ADT implementations: I

ID: 3748860 • Letter: M

Question

Modify the Stack ADT implementations
Utilizing your Stack ADT implementations:
Implement the methods specified for the bounded stack (ArrayStack)
• Add an inspector method inspector(int loc). The method will return the element found at
the location loc. Return null if the location is invalid.
• Implement popSome(int count). The method will remove the top count items from the
stack. The method should throw a StackUnderFlow Exception as needed. Note that you will
have some room for decisions in this method. Document your approach in the comments.
• Without adding any new instance variables, implement a size() method. The method will
return an integer value indicating the number of elements in the stack.
** These methods will not be part of the StackInterface or the BoundedStackInterface. You don’t need
to define them in the interface declarations.


package ch03;
public class ArrayStack<T> implements BoundedStackInterface<T> {
protected int DEFCAP = 100;
protected T[] stack;
protected int topIndex;
public ArrayStack() {
topIndex = -1;
stack = (T[]) new Object[DEFCAP];
}
public ArrayStack(int capacity) {
topIndex = -1;
stack = (T[]) new Object[capacity];
}
@Override
public void pop() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty()) {
throw new StackUnderflowException("Stack is empty");
}
else {
topIndex--;
}
}
@Override
public T top() throws StackUnderflowException {
// TODO Auto-generated method stub
if (isEmpty()) {
throw new StackUnderflowException("Stack is empty");
}
else {
return stack[topIndex];
}
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return (topIndex == -1);
}
@Override
public void push(T element) throws StackOverflowException {
// TODO Auto-generated method stub
if (isFull()) {
throw new StackOverflowException("Stack is full");
}
else {
topIndex++;
stack[topIndex] = element;
}
}
@Override
public boolean isFull() {
// TODO Auto-generated method stub
return (topIndex == stack.length-1);
}
}

Explanation / Answer

public T inspector(int loc){
  
  
if(loc > topIndex){
  
return null;
}
else
return stack[loc];
  
  
}


public void popSome(int count) throws StackUnderFlowException{
  
if(count>topIndex+1){
  
throw new StackUnderflowException("Can not perform operation");
  
}
  
else
topIndex = topIndex-count;
  
  
  
}


public int size() {
  
  
retun topIndex+1;   
}

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