In Java 28 AC only A String toStringcreates and returns a st
In Java
#28 A-C only
A. 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.
B. 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.
C. void popSome(int count)—removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack.
D. 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.
E. 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.
This is to help
2.5 Array-Based Stack Implementations
Show the implementation representation (the values of the instance variables elements and topIndex) at each step of the following code sequence:
Show the implementation representation (the values of the instance variables elements and topIndex) at each step of the following code sequence:
Describe the effects each of the changes would have on the ArrayBoundedStack class.
Remove the final attribute from the DEFCAP instance variable.
Change the value assigned to DEFCAP to 10.
Change the value assigned to DEFCAP to -10.
In the first constructor change the statement to
In isEmpty, change “topIndex == -1” to “topIndex < 0”.
Reverse the order of the two statements in the else clause of the push method.
Reverse the order of the two statements in the else clause of the pop method.
In the throw statement of the top method change the argument string from “Top attempted on an empty stack” to “Pop attempted on an empty stack.”
Solution
package com.list;
import java.util.Scanner;
//Two constructors are provided: one that creates an array of a
//default size and one that allows the calling program to
//specify the size.
//----------------------------------------------------------------
public class ArrayBoundedStack<T> implements StackInterface<T>
{
protected final int DEFCAP = 10; // 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
// 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
// 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
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element from 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 reverse(){
Scanner scan = new Scanner(System.in);
StackInterface<String> holdStrings;
holdStrings = new ArrayBoundedStack<String>(3);
String line = null;
for (int i = 1; i <= 3; i++)
{
System.out.print(\"Enter a line of text > \");
line = scan.nextLine();
holdStrings.push(line);
}
System.out.println(\"\ Reverse is:\ \");
while (!holdStrings.isEmpty())
{
line = holdStrings.top();
holdStrings.pop();
System.out.println(line);
}
return line;
}
public static void main(String[] args) {
StackInterface<String> s = new ArrayBoundedStack<String> (5);
s.push(\"Elizabeth\");
s.push(\"Anna Jane\");
s.pop();
s.push(\"Joseph\");
s.pop();
System.out.println( s.isFull());
//s.pop();s.pop();s.pop();
System.out.println(s.isEmpty());
s.reverse();
}
}
package com.list;
public interface StackInterface<T>
{
void push(T element) throws StackOverflowException;
// Throws StackOverflowException if this stack is full,
// otherwise places element at the top of this stack.
void pop() throws StackUnderflowException;
// Throws StackUnderflowException if this stack is empty,
// otherwise removes top element from this stack.
T top() throws StackUnderflowException;
// Throws StackUnderflowException if this stack is empty,
// otherwise returns top element from this stack.
boolean isEmpty();
// Returns true if this stack is empty, otherwise returns false.
boolean isFull();
// Returns true if this stack is full, otherwise returns false.
String reverse();
}
package com.list;
public class StackUnderflowException extends RuntimeException
{
public StackUnderflowException()
{
super();
}
public StackUnderflowException(String message)
{
super(message);
}
}
package com.list;
public class StackOverflowException extends RuntimeException
{
public StackOverflowException()
{
super();
}
public StackOverflowException(String message)
{
super(message);
}
}





