Java Program Download the ArrayStack.java source code file. Create a new program
ID: 3686849 • Letter: J
Question
Java Program
Download the ArrayStack.java source code file.
Create a new program called StackTest with a main method that performs the following steps: 1. Create an ArrayStack object with an initial capacity of 4 and generic type String. ArrayStack stack = new ArrayStack(4); 2. Add the following String objects to the stack: “Red”, “Yellow”, “Green”, “Blue”, “Purple”, “Orange”, “White” 3. Remove 2 elements from the stack and print them to standard out. 4. Add the following element to the stack “Grey”. 5. Create a while loop that removes and prints all elements from the stack to standard out (do not use a counter).
//
***************************************************************************************************************
***********************************************************************
Explanation / Answer
Below classes updated as per the requirement. Code updates highlighted below.
ArrayStack.java
//
//CONSTRUCTION: with no initializer
//
//******************PUBLIC OPERATIONS*********************
//void push( x ) --> Insert x
//void pop( ) --> Remove most recently inserted item
//AnyType top( ) --> Return most recently inserted item
//AnyType topAndPop( ) --> Return and remove most recent item
//boolean isEmpty( ) --> Return true if empty; else false
//void makeEmpty( ) --> Remove all items
//******************ERRORS********************************
//top, pop, or topAndPop on empty stack
/**
* Array-based implementation of the stack.
* @author Mark Allen Weiss/ revised by Debra Duke
*/
public class ArrayStack<AnyType> implements Stack<AnyType> {
private AnyType [ ] theArray;
private int topOfStack;
private static final int DEFAULT_CAPACITY = 10;
public ArrayStack( ) {
this(DEFAULT_CAPACITY);
}
@SuppressWarnings("unchecked")
public ArrayStack(int initialCapacity) {
if(initialCapacity > 0){
theArray = (AnyType[]) new Object[ initialCapacity ];
topOfStack = -1;
}
else throw new IllegalArgumentException("invalid initial size");
}
/**
* Test if the stack is logically empty.
* @return true if empty, false otherwise.
*/
public boolean isEmpty( )
{
return topOfStack == -1;
}
/**
* Make the stack logically empty.
*/
public void makeEmpty( )
{
topOfStack = -1;
}
/**
* Get the most recently inserted item in the stack.
* Does not alter the stack.
* @return the most recently inserted item in the stack.
* @throws UnderflowException if the stack is empty.
*/
public AnyType top( )
{
if( isEmpty( ) )
throw new UnderflowException( "ArrayStack top" );
return theArray[ topOfStack ];
}
/**
* Remove the most recently inserted item from the stack.
* @throws UnderflowException if the stack is empty.
*/
public void pop( ) {
if( isEmpty( ) )
throw new UnderflowException( "ArrayStack pop" );
topOfStack--;
}
/**
* Return and remove the most recently inserted item
* from the stack.
* @return the most recently inserted item in the stack.
* @throws Underflow if the stack is empty.
*/
public AnyType topAndPop( ) {
if( isEmpty( ) )
throw new UnderflowException( "ArrayStack topAndPop" );
return theArray[ topOfStack-- ];
}
/**
* Insert a new item into the stack.
* @param x the item to insert.
*/
public void push( AnyType x )
{
if( topOfStack + 1 == theArray.length )
doubleArray( );
theArray[ ++topOfStack ] = x;
}
/**
* Internal method to extend theArray.
*/
@SuppressWarnings("unchecked")
private void doubleArray( )
{
AnyType [ ] newArray;
newArray = (AnyType []) new Object[ theArray.length * 2 ];
for( int i = 0; i < theArray.length; i++ )
newArray[ i ] = theArray[ i ];
theArray = newArray;
}
public AnyType peek( )
{
if(isEmpty( ))
{
return null;
}
return theArray[topOfStack];
}
}
Stack.java
//Stack interface
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// AnyType top( ) --> Return most recently inserted item
// AnyType topAndPop( ) --> Return and remove most recent item
// boolean isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// top, pop, or topAndPop on empty stack
/**
* Protocol for stacks.
* @author Mark Allen Weiss
*/
public interface Stack<AnyType>
{
/**
* Insert a new item into the stack.
* @param x the item to insert.
*/
void push( AnyType x );
/**
* Remove the most recently inserted item from the stack.
* @exception UnderflowException if the stack is empty.
*/
void pop( );
/**
* Get the most recently inserted item in the stack.
* Does not alter the stack.
* @return the most recently inserted item in the stack.
* @exception UnderflowException if the stack is empty.
*/
AnyType top( );
/**
* Return and remove the most recently inserted item
* from the stack.
* @return the most recently inserted item in the stack.
* @exception UnderflowException if the stack is empty.
*/
AnyType topAndPop( );
/**
* Test if the stack is logically empty.
* @return true if empty, false otherwise.
*/
boolean isEmpty( );
/**
* Make the stack logically empty.
*/
void makeEmpty( );
public AnyType peek( );
}
UnderflowException.java
/**
* Exception class for access in empty containers
* such as stacks, queues, and priority queues.
* @author Mark Allen Weiss
*/
public class UnderflowException extends RuntimeException
{
/**
* Construct this exception object.
* @param message the error message.
*/
public UnderflowException( String message )
{
super( message );
}
}
StackTest.java
public class StackTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayStack<String> stack = new ArrayStack<String>(4);
stack.push("Red");
stack.push("Yellow");
stack.push("Green");
stack.push("Blue");
stack.push("Purple");
stack.push("Orange");
stack.push("White");
stack.pop();
stack.pop();
System.out.println("Orange & White Strings removed from Stack.");
stack.push("Grey");
System.out.println("Stack Elements after adding Grey Color");
while (!stack.isEmpty()) {
String value = stack.peek();
System.out.println(value);
stack.pop();
}
}
}
Output:
Orange & White Strings removed from Stack.
Stack Elements after adding Grey Color
Grey
Purple
Blue
Green
Yellow
Red
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.