Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implementing the method clear() in the class ArrayStack The class ArrayStack uses a fixed-size array and implements the interface Stack. Now that the interface Stack

Implementing the method clear() in the class ArrayStack The class ArrayStack uses a fixed-size array and implements the interface Stack. Now that the interface

Stack has been modified to have a method clear(), the current implementation of the class ArrayStack is broken (try compiling it without making any change, what is the error message displayed?). Since the class ArrayStack implements the interface Stack, it has to provide an implementation for all the methods that are declared by the interface. Consequently, write an implementation for the method void clear(). It removes all of the elements from this ArrayStack. The stack will be empty after this call returns.

file:

public class ArrayStack implements Stack { // Instance variables private E[] elems; // Used to store the elements of this ArrayStack private int top; // Designates the first free cell @SuppressWarnings( "unchecked" ) // Constructor public ArrayStack( int capacity ) { elems = (E[]) new Object[ capacity ]; top = 0; } // Returns true if this ArrayStack is empty public boolean isEmpty() { // Same as: // if ( top == 0 ) { // return true; // } else { // return false; // } return ( top == 0 ); } // Returns the top element of this ArrayStack without removing it public E peek() { // pre-conditions: ! isEmpty() return elems[ top-1 ]; } // Removes and returns the top element of this stack public E pop() { // pre-conditions: ! isEmpty() // *first* decrements top, then access the value! E saved = elems[ --top ]; elems[ top ] = null; // scrub the memory! return saved; } // Puts the element onto the top of this stack. public void push( E element ) { // Pre-condition: the stack is not full // *first* stores the element at position top, then increments top elems[ top++ ] = element; } public void clear() { for ( int i=0; i 

file:

public class L4Q2 { public static void main( String[] args ) { Stack s; s = new ArrayStack( 10 ); for ( int i=0; i<10; i++ ) { s.push( "Elem-" + i ); } s.clear(); while ( ! s.isEmpty() ) { System.out.println( s.pop() ); } for ( int i=0; i<10; i++ ) { s.push( "** Elem-" + i ); } while ( ! s.isEmpty() ) { System.out.println( s.pop() ); } } } // > java L4Q2 // ** Elem-9 // ** Elem-8 // ** Elem-7 // ** Elem-6 // ** Elem-5 // ** Elem-4 // ** Elem-3 // ** Elem-2 // ** Elem-1 // ** Elem-0 

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

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions

Question

What does stickiest refer to in regard to social media

Answered: 1 week ago