Write a display method to go inside of the ArrayStack class. Display the content
ID: 3819868 • Letter: W
Question
Write a display method to go inside of the ArrayStack class. Display the contents like this (including the BOTTOM and TOP labels):
BOTTOM element3 element2 element1 TOP
The method header is:
public void display()
Notes:
This method goes inside of the ArrayStack class, so you can directly access the instance data variables of that class.
For full credit, your methods should be efficient- O(n ).
Take advantage of being able to directly access the array.
System.out.println("----------Q20");
LinkedStack displayLinkedStack = new LinkedStack();
System.out.println("Should give a message that the stack is empty.");
displayLinkedStack.display();
displayLinkedStack.push("Alaska");
displayLinkedStack.push("Delaware");
displayLinkedStack.push("Iowa");
displayLinkedStack.push("New York");
System.out.println("Should print BOTTOM Alaska Delaware Iowa New York TOP");
displayLinkedStack.display();
import java.util.Arrays;
import java.util.EmptyStackException;
public final class ArrayStack<T> implements StackInterface<T> {
private T[] stack; // Array of stack entries
private int topIndex; // Index of top entry
private boolean initialized = false;
private static final int DEFAULT_CAPACITY = 50;
private static final int MAX_CAPACITY = 10000;
public ArrayStack() {
this(DEFAULT_CAPACITY);
} // end default constructor
public ArrayStack(int initialCapacity) {
checkCapacity(initialCapacity);
// The cast is safe because the new array contains null entries
@SuppressWarnings("unchecked")
T[] tempStack = (T[]) new Object[initialCapacity];
stack = tempStack;
topIndex = -1;
initialized = true;
} // end constructor
public void push(T newEntry) {
checkInitialization();
ensureCapacity();
stack[topIndex + 1] = newEntry;
topIndex++;
} // end push
public T peek() {
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else
return stack[topIndex];
} // end peek
public T pop() {
checkInitialization();
if (isEmpty())
throw new EmptyStackException();
else {
T top = stack[topIndex];
stack[topIndex] = null;
topIndex--;
return top;
} // end if
} // end pop
public boolean isEmpty() {
return topIndex < 0;
} // end isEmpty
public void clear() {
checkInitialization();
// Remove references to the objects in the stack,
// but do not deallocate the array
while (topIndex > -1) {
stack[topIndex] = null;
topIndex--;
} // end while
// Assertion: topIndex is -1
} // end clear
public void display() {
// YOUR CODE HERE!
}
// Throws an exception if this object is not initialized.
private void checkInitialization() {
if (!initialized)
throw new SecurityException(
"ArrayStack object is not initialized properly.");
} // end checkInitialization
// Throws an exception if the client requests a capacity that is too large.
private void checkCapacity(int capacity) {
if (capacity > MAX_CAPACITY)
throw new IllegalStateException("Attempt to create a stack "
+ "whose capacity exceeds " + "allowed maximum.");
} // end checkCapacity
// Doubles the size of the array stack if it is full
// Precondition: checkInitialization has been called.
private void ensureCapacity() {
if (topIndex >= stack.length - 1) // If array is full, double its size
{
int newLength = 2 * stack.length;
checkCapacity(newLength);
stack = Arrays.copyOf(stack, newLength);
} // end if
} // end ensureCapacity
} // end ArrayStack
Explanation / Answer
Implementation of display method
public void display() {
checkInitialization(); // checks whether stack is initialized
if(this.isEmpty()){ // checks whether stack is empty
System.out.println("Stack is Empty");
}
else{
System.out.print("BOTTOM "); // print bottom label
/*
* In above array implementation,
* element at bottom of the stack is stack[0]
* element at top of the stack is stack[topIndex]
* elements in stack from bottom to top corresponds to elements in array from first to last
*
*/
for(int i=0;i<=this.topIndex;i++){
System.out.print(this.stack[i].toString()); // print the element
System.out.print(" "); // print space between elements
}
System.out.println("TOP"); //print top label
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.