For this assignment, you will create a stack class using an array as its underlying data structure. Our version implements the provided "CiscCollection" interface, which is a subset of java.util.Collection.

CiscStack< E > implements CiscCollection< E >
+ f DEFAULT_CAPACITY: int = 10
- elementData: E[]
- size: int
+ CiscStack()
+ CiscStack(capacity: int)
+ isEmpty(): boolean
+ push(item: E): void
+ pop(): E
+ peek(): E
+ iterator(): Iterator< E >
+ size(): int
+ contains(o: Object): boolean
+ clear(): void
+ toArray(): Object[]
- ensureCapacity(capacity: int): void

CiscStackIterator implements Iterator< E >
- nextNodeIndex: int
+ CiscStackIterator()
+ hasNext(): boolean
+ next(): E

CiscStack

f DEFAULT_CAPACITY: int

This class constant stores the default capacity of the underlying array if none is provided.

elementData: E[]

The array buffer into which the elements of the CiscStack are stored. The capacity of the CiscStack is the length of this array buffer. The element at the "top" of the stack (if it exists) will be located at index size-1. The element at the bottom of the stack (if it exists) will be located at index 0.

size: int

The size of the CiscStack (the number of elements it contains).

CiscStack()

Constructs an empty stack with an initial capacity of DEFAULT_CAPACITY.

CiscStack(initialCapacity : int)

Constructs an empty stack with the specified initial capacity. Throws an IllegalArgumentException if initialCapacity is invalid.

isEmpty() : boolean

Returns true if the stack is empty.

push(item : E) : void

Adds the item to the top of the stack, which is the next open index in elementData. Calls ensureCapacity before adding to ensure sufficient space.

pop() : E

Returns and removes the element at the top of the stack. Throws an EmptyStackException if the stack is empty.

peek() : E

Returns the element at the top of the stack. Throws an EmptyStackException if the stack is empty.

iterator() : Iterator< E >

Creates and returns a new instance of the CiscStackIterator class.

size() : int

Returns the number of elements in this stack.

contains(o : Object) : boolean

Returns true if this stack contains the specified element. More formally, returns true if and only if this stack contains at least one element e such that Objects.equals(o, e).

clear() : void

Removes all of the elements from this stack, which includes setting each index in elementData to null. The stack will be empty after this call returns.

toArray() : Object[]

Returns an array containing all the elements in the stack, beginning with the element at the top of the stack, ending with the element at the bottom of the stack, stored in consecutive indices of the returned array, starting with index 0. The length of the returned array is equal to size.

ensureCapacity(minCapacity : int) : void

Ensures that the array buffer has the capacity to hold the specified number of elements. If not, the capacity of the array buffer will be doubled + 1 (if the new capacity is greater than minCapacity) or set to minCapacity.

CiscStackIterator

This class should be private (CiscStack clients don't need to create instances of it directly) and non-static (it needs to access CiscStack internals).

nextIndex : int

The index of the next element in the stack (if it exists) to be returned by the next call to next().

CiscStackIterator()

Constructs a CiscStackIterator that can be used to iterate over the containing CiscStack instance. Ensures that the initial value of nextIndex refers to the element at the top of the stack.

hasNext() : boolean

Returns true if the iterator has yet to return all data contained in the stack.

next() : E

Returns the next element in the stack, and updates nextIndex appropriately.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.