Question: Design a class named StackOfStrings that contains a. A private array elements to store String in the stack b. A private data field size to
Design a class named StackOfStrings that contains a. A private array elements to store String in the stack b. A private data field size to store the number of String in the stack c. A constructor to construct an empty stack with a default capacity of 4. d. A constructor to construct an empty stack with a specified capacity. e. A method empty that returns true if the stack is empty f. A method push(string value) that stores value at the top of the stack. This method checks if the stack is full before pushing to it. In case the stack is full, it I. copies the contents of the stack to a new temp array ii. creates a new elements array with double the current capacity iii. copies back temp to elements iv. places value at the top of the new stack g. A method pop that removes the string at the top of the stack and returns it. In case the stack is empty, it returns "Stack is EMPTY" h. A method peeko that returns the string at the top of the stack without removing it from the stack. In case the stack is empty, it returns Stack is "EMPTY i. A method printStack() as shown below: public void printStack! System.out.print("Stack (top to bottom): "); for (int i=size-1; i>-1; i--) System.out.print(elements[i] + System.out.println(;)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
