Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size

JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings 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 peek() 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=size1; i>1; i) System.out.print(elements[i] + " "); System.out.println(); }1. Draw the UML diagram for the StackOfStrings class. 2. Implement the StackOfStrings class. 3. Use the test program shown on the following page which creates a StackOfStrings object, and then allows the user to push, pop or peek at the stack. 4. Draw the UML diagram for the StackOfStrings object created in the test program. import java.util.Scanner; public class Test { public static void main(String[] args) { StackOfStrings stack = new StackOfStrings(); System.out.println("STACK OF STRINGS Type PSH followed by a space " + "and a string and a carriage return or Type POP followed " + "by a carriage return or Type PEK followed by a carriage " + "return or Type STP followed buy a carriage return"); Scanner input = new Scanner(System.in); while (true) { System.out.print("Enter command: "); String s = input.nextLine(); String stackCommand = s.substring(0, 3); switch (stackCommand) { case "PSH": stack.push(s.substring(s.indexOf(" ") + 1)); stack.printStack(); break; case "POP": System.out.println(stack.pop()); stack.printStack();break; case "PEK": System.out.println(stack.peek()); stack.printStack(); break; case "STP": System.exit(0) default: System.out.println("Illegal input. Try again."); } } } } class StackOfStrings { // Implement the class here }

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

Inductive Databases And Constraint Based Data Mining

Authors: Saso Dzeroski ,Bart Goethals ,Pance Panov

2010th Edition

1489982175, 978-1489982179

Students also viewed these Databases questions