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

Add the following methods to the ArrayBoundedStack class Add the following metho

ID: 3792764 • Letter: A

Question

Add the following methods to the ArrayBoundedStack class

Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class.

String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.

int size()—returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method.

void popSome(int count)—removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack.

boolean swapStart()—if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.

T poptop( )—the “classic” pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.

//----------------------------------------------------------------
//
// Implements StackInterface using an array to hold the
// stack elements.
//
// Two constructors are provided: one that creates an array of a
// default size and one that allows the calling program to
// specify the size.
//----------------------------------------------------------------

package ch02.stacks;

public class ArrayBoundedStack<T> implements StackInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected T[] elements;           // holds stack elements
protected int topIndex = -1;      // index of top element in stack

public ArrayBoundedStack()
{
    elements = (T[]) new Object[DEFCAP];
}

public ArrayBoundedStack(int maxSize)
{
    elements = (T[]) new Object[maxSize];
}

public void push(T element)
// Throws StackOverflowException if this stack is full,
// otherwise places element at the top of this stack.
{    
    if (isFull())
      throw new StackOverflowException("Push attempted on a full stack.");
    else
    {
      topIndex++;
      elements[topIndex] = element;
    }
}

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{                
    if (isEmpty())
      throw new StackUnderflowException("Pop attempted on an empty stack.");
    else
    {
      elements[topIndex] = null;
      topIndex--;
    }
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{               
    T topOfStack = null;
    if (isEmpty())
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
      topOfStack = elements[topIndex];
    return topOfStack;
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{            
    return (topIndex == -1);
}

public boolean isFull()
// Returns true if this stack is full, otherwise returns false.
{            
    return (topIndex == (elements.length - 1));
}
}

Explanation / Answer

//----------------------------------------------------------------
//
// Implements StackInterface using an array to hold the
// stack elements.
//
// Two constructors are provided: one that creates an array of a
// default size and one that allows the calling program to
// specify the size.
//----------------------------------------------------------------
import java.lang.Iterable;
import java.util.*;
import java.lang.*;
import java.io.*;
//package ch02.stacks;

//public class ArrayBoundedStack<T> implements StackInterface<T>

//public class StackUnderflowException extends RuntimeException
class StackUnderflowException extends RuntimeException
{
public StackUnderflowException()
{
    super();
}

public StackUnderflowException(String message)
{
    super(message);
}
}
class StackOverflowException extends RuntimeException
{
public StackOverflowException()
{
    super();
}

public StackOverflowException(String message)
{
    super(message);
}
}
class ArrayBoundedStack<T> implements StackInterface<T>
{
protected final int DEFCAP = 100; // default capacity
protected T[] elements;           // holds stack elements
protected int topIndex = -1;      // index of top element in stack

public ArrayBoundedStack()
{
    elements = (T[]) new Object[DEFCAP];
}

public ArrayBoundedStack(int maxSize)
{
    elements = (T[]) new Object[maxSize];
}

public void push(T element)
// Throws StackOverflowException if this stack is full,
// otherwise places element at the top of this stack.
{    
    if (isFull())
      throw new StackOverflowException("Push attempted on a full stack.");
    else
    {
      topIndex++;
      elements[topIndex] = element;
    }
}

public void pop()
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
{                
    if (isEmpty())
      throw new StackUnderflowException("Pop attempted on an empty stack.");
    else
    {
      elements[topIndex] = null;
      topIndex--;
    }
}

public T top()
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element of this stack.
{               
    T topOfStack = null;
    if (isEmpty())
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
      topOfStack = elements[topIndex];
    return topOfStack;
}

public boolean isEmpty()
// Returns true if this stack is empty, otherwise returns false.
{            
    return (topIndex == -1);
}

public boolean isFull()
// Returns true if this stack is full, otherwise returns false.
{            
    return (topIndex == (elements.length - 1));
}
public String toString()
{
    String ans=new String();
    int counter=topIndex;
    while(counter>=0)
    {
      ans=ans+elements[topIndex].toString()+"--";
      counter--;
    }

}
public int size()
{
    return topIndex+1;
}
public void popSome(int count)
{
    if(size()<count)
      throw new StackUnderflowException("Less number of elements in stack.");
    while(count>0)
    {
      elements[topIndex] = null;
      topIndex--;
      count--;
    }
}
public boolean swapStart()
{
    if(size()<2)
      return false;
    T topOfStack = null;
    topOfStack=elements[topIndex];
    elements[topIndex]=elements[topIndex-1];
    elements[topIndex-1]=topOfStack;
    return true;
}
public T poptop()
{
    if(topIndex==-1)
      throw new StackUnderflowException("Top attempted on an empty stack.");
    else
    {
      T t=null;
      t=elements[topIndex--];
      return t;
    }
}
}
public class ArrayBounded
{
public static void main(String args[])
{
    Stack<Integer> s=new ArrayBoundedStack<Integer>();
    s.push(10);
    s.push(20);
    s.push(30);
    System.out.println(s.toString());
    System.out.println(s.size());
    System.out.println(s.poptop());

}
}

output:

10--20--30

3

10

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