Question: 1.2 Implement the method clear) of the class ArrayStack The class ArrayStack uses an array of fixed size and implements the interface Stack. Since the

 1.2 Implement the method clear) of the class ArrayStack The classArrayStack uses an array of fixed size and implements the interface Stack.Since the interface Stack has been modified to have the method clear),

1.2 Implement the method clear) of the class ArrayStack The class ArrayStack uses an array of fixed size and implements the interface Stack. Since the interface Stack has been modified to have the method clear), the implementation of the class ArrayStack is faulty. (Try to compile it now without changing anything, what error do you see?). Since the class ArrayStack implements the interface Stack, it needs to have an implementation for all methods of the interface. Therefore, you will need to write a method void clear0). This method will remove all the elements from this stack (ArrayStack). The stack will be empty after this call. Use the class L6Q1 to test the implementation of the method clear() Files: ArrayStack.java L6Q1.java public class ArrayStack implements Stack private E[] elems; Used to store the elements of this ArrayStack // Designates the first free cell private int top; SuppressWarnings"unchecked" // Constructor public ArrayStack( int capacity t elems(E]) new Object capacity 1; top6 // Returns true if this Arraystack is empty public boolean isEmpty) / Same as: // if (tope) t /return true; / else return false; return top) // Returns the top element of this Arraystack without removing it public E peek)t // pre-conditions: ! isEmpty() return elems[ top-1 ]; 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! saved elems top elems top]null; scrub the memoryl return saved; Puts the element onto the top of this stack. public void push( E element / Pre-condition: the stack is not ful1 I first stores the element at position top, then increments top elems topelement; // Gets current capacity of the array (for testing purpose) public int getCapacity) { return elems.length; Suppressvarnings( unchecked") // Add clear method

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!