Implement the push(), pop(), and peek() methods for efficient execution using an
ID: 3600490 • Letter: I
Question
Implement the push(), pop(), and peek() methods for efficient execution using an ArrayListbacking store. Since you can use the ArrayList to do much of the work, you must also write a class of JUnit test methods provide 100% coverage of your methods.
import java.util.ArrayList;
import java.util.EmptyStackException;
/**
* Class which wraps Java's built-in {@link ArrayList} implementation so that it adds & removes elements from the end of
* the List to improve efficiency AND uses the traditional method names.
*
* @param <E> Type of data stored within this Stack.
*/
public class ProperStack<E> {
/**
* The actual Stack in which all the elements will be stored. This is protected (and not private) so that students can
* use this field in the test cases they create..
*/
protected ArrayList<E> store;
/** Create a new (empty) instance of this class. */
public ProperStack() {
store = new ArrayList<>();
}
/**
* Adds an item to the end of the stack. Traditionally, this is the only method available to add data to a Stack.
*
* @param item Element to be added to the end of the stack.
* @return Element added to the Stack (e.g., {@code item}).
*/
public E push(E item) {
}
/**
* Removes and returns the element at the top of this Stack. Traditionally, this is the only method available to
* remove data from the Stack.
*
* @return Element that was removed from the top of the Stack.
* @throws EmptyStackException Thrown when the Stack does not have any elements to remove.
*/
public E pop() {
}
/**
* Like {@link #pop()}, this returns the element at the top of the Stack, but unlike {@link #pop} this method DOES NOT
* remove the element.
*
* @return Element that is at the top of the Stack.
*/
public E peek() {
}
}
Explanation / Answer
Please find my implementation.
import java.util.ArrayList;
import java.util.EmptyStackException;
/**
* Class which wraps Java's built-in {@link ArrayList} implementation so that it adds & removes elements from the end of
* the List to improve efficiency AND uses the traditional method names.
*
* @param <E> Type of data stored within this Stack.
*/
public class ProperStack<E> {
/**
* The actual Stack in which all the elements will be stored. This is protected (and not private) so that students can
* use this field in the test cases they create..
*/
protected ArrayList<E> store;
/** Create a new (empty) instance of this class. */
public ProperStack() {
store = new ArrayList<>();
}
/**
* Adds an item to the end of the stack. Traditionally, this is the only method available to add data to a Stack.
*
* @param item Element to be added to the end of the stack.
* @return Element added to the Stack (e.g., {@code item}).
*/
public E push(E item) {
store.add(item);
return item;
}
/**
* Removes and returns the element at the top of this Stack. Traditionally, this is the only method available to
* remove data from the Stack.
*
* @return Element that was removed from the top of the Stack.
* @throws EmptyStackException Thrown when the Stack does not have any elements to remove.
*/
public E pop() {
if(store.size() == 0)
return null;
return store.remove(store.size()-1);
}
/**
* Like {@link #pop()}, this returns the element at the top of the Stack, but unlike {@link #pop} this method DOES NOT
* remove the element.
*
* @return Element that is at the top of the Stack.
*/
public E peek() {
if(store.size() == 0)
return null;
return store.get(store.size()-1);
}
}
##########
public class ProperStackTest {
public static void main(String[] args) {
ProperStack<Integer> stack = new ProperStack<>();
System.out.println("Push: "+stack.push(34));
System.out.println("Push: "+stack.push(4));
System.out.println("Pop: "+stack.pop());
System.out.println("Push: "+stack.push(54));
System.out.println("Push: "+stack.push(21));
System.out.println("Top: "+stack.peek());
System.out.println("Pop: "+stack.pop());
}
}
/*
Sample run:
Push: 34
Push: 4
Pop: 4
Push: 54
Push: 21
Top: 21
Pop: 21
*/