Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Following code has the start of a class which implements a Stack using a list (rather than Java's adding Stack methods to a List class).

Following code has the start of a class which implements a Stack using a list (rather than Java's adding Stack methods to a List class). You will need to complete the push(), pop(), and peek() methods so that they use the List as its backing store

package edu.uncw.cse; import java.util.ArrayList; import java.util.EmptyStackException; import java.util.List; /** * Class which implements a proper {@code Stack} -- e.g., unlike Java this class eliminates methods that are not * appropriate for a Stack. Because performance is also important, this implementation uses an ArrayList as its backing * store. * * @param  Type of data stored within this Stack. */ public class ProperStack { /** The backing store in which all the elements are stored. */ private List store; /** Create a new (empty) instance of this class. */ public ProperStack() { store = new ArrayList<>(); } /** * Pushes an item onto the top of this stack. Traditionally, this is the only * method available to add data to a Stack. * * @param item Element to be added to the top 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 it from the stack. * * @return Element that is at the top of the stack. * @throws EmptyStackException Thrown when the Stack does not have any * elements to remove. */ public E peek() { } /** * Returns the number of elements in this Stack. * * @return Items available in the Stack. */ public int size() { return store.size(); } /** * Returns whether there are any elements in this Stack. * * @return True if the Stack does not have any elements; false otherwise. */ public boolean isEmpty() { return store.isEmpty(); } /** * Removes all the elements from the Stack. */ public void clear() { store.clear(); } } 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions