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=size‐1; 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\nType PSH followed by a space " + "and a string and a carriage return or\nType POP followed " + "by a carriage return or\nType PEK followed by a carriage " + "return or \nType 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 }
public class Queue{
int elements[];
int size;
Queue(){
elements = new int[8];
size = 0;
}
void enqueue(int v){
elements[size++] = v;
if(size == elements.length){
doubleSize();
}
}
int dequeue(){
int temp = -1;
if(empty()) System.out.println("Queue is empty");
else{
temp = elements[0];
shiftLeft();
size--;
}
return temp;
}
boolean empty(){
if(size == 0) return true;
else return false;
}
int getSize(){
return size;
}
private void doubleSize(){
int temp[] = elements;
elements = new int[size * 2];
for(int i = 0; i < temp.length; i++){
elements[i] = temp[i];
}
}
private void shiftLeft(){
for(int i = 0; i < size - 1; i++){
elements[i] = elements[i + 1];
}
}
}
JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings...
In Java. What would the methods of this class look like?
StackADT.java
public interface StackADT<T>
{
/** Adds one element to the top of this stack.
* @param element element to be pushed onto stack
*/
public void push (T element);
/** Removes and returns the top element from this stack.
* @return T element removed from the top of the stack
*/
public T pop();
/** Returns without removing the top element of this
stack.
* @return T...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string),...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...
Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...
I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> { // Data fields private Node<T> topNode; // references the first node in the chain private int numberOfEntries; public...
how do I change my code to generic form *********************************************************************** public class UnboundedStackQueue { //question#3 } class Stack { Node head; int size; Stack() //default constructor { this.head=null; this.size=0; } //Input = data //Output = void (just adds value to list) // method pushes elements on stack // time: O(1) // space: O(1) public void push(int data) { Node node=new Node(data); node.next=head; head=node; size++; } //Input = none //Output = top of stack // method pops value from top of...