Question: Create a class called DropOutStack. UML diagram for DropOutStack -maxSize: int -top: LinearNode +DropOutStack() +DropOutStack(max: int) +DropOutStack(elem: T) +DropOutStack(elem: T, max: int) +popStack(capacity: int):T[capacity] +setMaxSize(max:
Create a class called "DropOutStack".
UML diagram for DropOutStack
-maxSize: int
-top: LinearNode
+DropOutStack()
+DropOutStack(max: int)
+DropOutStack(elem: T)
+DropOutStack(elem: T, max: int)
+popStack(capacity: int):T[capacity]
+setMaxSize(max: int): void
+getMaxSize(): int
What is a drop out stack?
Same thing as a regular stack, except with a maximum capacity.
Whenever a new element is added when the stack is full, the bottom-most element must be removed from the collection The new element is then inserted and the size and max capacity shouldn't change.
Be sure to keep the following stipulations in mind:
- You must rely on links for its underlying data structure.
- Tip: Reuse and modify class 'LinkedStack' since its generics and attributes are already established and are still important to have.
- The code within interface 'StackADT' and class 'LinearNode' should be the same as they originally were.
- All [four] constructors serve similar purposes and should work like so:
- The starting value for attribute 'maxSize' will depend on what value the constructor's integer parameter is, otherwise if said parameter doesn't exist it should be 10 (by default).
- Important: You MUST invoke the class' own mutator method to handle said assignment operation.
- The starting value for attribute 'maxSize' will depend on what value the constructor's integer parameter is, otherwise if said parameter doesn't exist it should be 10 (by default).
-
- The constructors which have the 'T' parameter must set said element as the starting 'top' node of the collection (otherwise it should remain null).
- Tip: Focus on implementing the 4th constructor (the one with two parameters) and have the other constructors invoke it, using the 'this' keyword, with appropriate arguments.
- Don't forget that the "pop" and "peek" method MUST throw a [runtime] exception if the stack is empty.
- Tip: For method "push", check the capacity limit AFTER inserting the element into the collection.
- Hint: If the last node needs to be decoupled from the collection, then this will require us to find and modify the 'next' reference of the second-to-last node in the sequence.
- Here's how the "popStack" function should work:
- It should throw a [runtime] exception if it's parameter 'capacity' exceeds the stack's [current] size, otherwise ...
- It'll return an array consisting of elements that are popped from the stack
- Be sure to keep the same order (in the array) as it was in the stack.
- First is the top-most element
- Second is second from the top
- Third is third from the top
- etc.
- Tip: Invoke the "pop" method. This will greatly reduce the total amount of code needed for this function.
- Be sure to keep the same order (in the array) as it was in the stack.
- The accessor and mutator methods for 'maxSize' should follow the standard template, HOWEVER, the mutator needs to throw a [runtime] exception if its parameter is LESS than the [current] number of elements within the stack.
public class LinkedStack implements StackADT
{
public LinkedStack()
{
count = 0;
top = null;
}
public void push(T element)
{
LinearNode temp = new LinearNode(element);
temp.setNext(top);
top = temp;
count++;
}
public T pop()
{
if(isEmpty())
throw new RuntimeException("Empty stack.");
T result = top.getElement();
top = top.getNext();
count--;
return result;
}
public T peek()
{
if(isEmpty())
throw new RuntimeException("Empty Stack.");
return top.getElement();
}
public boolean isEmpty()
{
return (count == 0);
}
public int size()
{
return count;
}
}
-------------------------------------------------------
public interface StackADT
{
public void push(T element);
public T pop();
public T peek();
public boolean isEmpty();
public int size();
}
---------------------------------------------------------
public class LinearNode
{
private LinearNode next;
private T element;
public LinearNode()
{
this(null);
}
public LinearNode(T elem)
{
next = null;
element = elem;
}
public LinearNode getNext()
{
return next;
}
public T getElement()
{
return element;
}
public void setNext(LinearNode node)
{
next = node;
}
public void setElement(T elem)
{
element = elem;
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
