Create a generic type java interface StackADTwith the following methods: a.public void push(T element);//Push an element at the top of a stack
Book: Java Software Structures
Author: John Lewis; Joe Chase
Javascript
1. Create a generic type java interface StackADT<T>with the following methods:
a.public void push(T element);//Push an element at the top of a stack
b.public T pop();//Remove and return an element from the top of a stack
c.public T peek();//return the top element without removing it
d.public booleanisEmpty();//Return True if a stack is emptye.public int size();//Return the number of elements in a stack
f.public String toString();//Print all the elements of a stack
2. Define ArrayStack<T>class which will implementStackADT<T>interface
3. Use your ArrayStack<T>class to compute a postfix expression, for instance, 3 4 * will yield12;3 4 6 + *will yield 30 etc.
During computing a postfix expression:
a.While you perform a push operation, if the stack is full, generate an exception
b.While you perform a pop operation, if the stack is empty, generate an exception
c.If two operands are not available on the stack for an operator, generate a message, “Postfix expression is invalid”.
d.If more than one operand is available, while no operator is left, generate a message, “Postfix expression is invalid”.
So far I have:
package com.company;
public interface StackADT<T>
{
/**
* Adds the specified element to the top of this stack.
* @param element element to be pushed onto the stack
*/
public void push(T element)
throws java.lang.IllegalArgumentException;
/**
* Removes and returns the top element from this stack.
* @return the element removed from the stack
*/
public T pop();
/**
* Returns without removing the top element of this stack.
* @return the element on top of the stack
*/
public T peek();
/**
* Returns true if this stack contains no elements.
* @return true if the stack is empty
*/
public boolean isEmpty();
/**
* Returns the number of elements in this stack.
* @return the number of elements in the stack
*/
public int size();
/**
* Returns a string representation of this stack.
* @return a string representation of the stack
*/
public String toString();
}
package com.company;
import java.util.Arrays;
public class ArrayStack<T> implements StackADT<T>
{
private final static int DEFAULT_CAPACITY = 100;
private int top;
private T[] stack;
/**
* Creates an empty stack using the default capacity.
*/
public ArrayStack()
{
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity the initial size of the array
*/
public ArrayStack(int initialCapacity)
{
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
/**
* Adds the specified element to the top of this stack, expanding
* the capacity of the array if necessary.
* @param element generic element to be pushed onto stack
*/
public void push(T element)
{
if (size() == stack.length)
expandCapacity();
stack[top] = element;
top++;
}
@Override
public boolean isEmpty()
{
while (top==0)
return true;
return false;
}
@Override
public int size() {
return 0;
}
/**
* Creates a new array to store the contents of this stack with
* twice the capacity of the old one.
*/
private void expandCapacity()
{
stack = Arrays.copyOf(stack, stack.length * 2);
}
/**
* Removes the element at the top of this stack and returns a
* reference to it.
* @return element removed from top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T pop() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
top--;
T result = stack[top];
stack[top] = null;
return result;
}
/**
* Returns a reference to the element at the top of this stack.
* The element is not removed from the stack.
* @return element on top of stack
* @throws EmptyCollectionException if stack is empty
*/
public T peek() throws EmptyCollectionException
{
if (isEmpty())
throw new EmptyCollectionException("stack");
return stack[top-1];
}
}
package com.company;
public class EmptyCollectionException extends RuntimeException{
/**
* Sets up this exception with an appropriate message.
* @param collection the name of the collection
*/
public EmptyCollectionException(String collection)
{
super("The " + collection + " is empty.");
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps