Question
*********JAVA********** Implement a class called StackSequence that is a sequence class of Section 4.5 of the required text book implemented using the stack data type
*********JAVA**********
Implement a class called StackSequence that is a sequence class of Section 4.5 of the required text book implemented using the stack data type instead of linked lists. Instead of items being stored in a linked list, the will be stored using two stacks as private member variables as in the following:
1. The bottom of the first stack is the beginning of the sequence.
2. The elements of the sequence continue up to the top of the first stack.
3. The next element of the sequence is then the top of the second stack.
4. And the elements of the sequence then continue down the bottom of the second sequence (which is the end of the sequence)
5. If there is a current element then that element is at the top of the first stack.
Do not change any of the specifications of any of the public methods
Section 4.5 sequence class:
Class DoubleLinkedSeq public class DoubleLinkedSeq from the package edu.colorado.collections A DoubleLinkedSeq is a sequence of double numbers. The sequence can have a special current element, which is specified and accessed through four methods that are not available in the bag class (start, getCurrent, advance, and isCurrent).
Limitations: Beyond Int.MAX_VALUE elements, the size method does not work. Specification
Constructor for the DoubleLinkedSeq public DoubleLinkedSeq( ) Initialize an empty sequence. Postcondition: This sequence is empty.
addAfter and addBefore public void addAfter(double element) public void addBefore(double element) Adds a new element to this sequence either before or after the current element. Parameter: element the new element that is being added Postcondition: A new copy of the element has been added to this sequence. If there was a current element, addAfter places the new element after the current element, and addBefore places the new element before the current element. If there was no current element, addAfter places the new element at the end of the sequence, and addBefore places the new element at the front of the sequence. The new element always becomes the new current element of the sequence. Throws: OutOfMemoryError Indicates insufficient memory for a new node.
addAll public void addAll(DoubleLinkedSeq addend) Place the contents of another sequence at the end of this sequence. Parameter: addend a sequence whose contents will be placed at the end of this sequence Precondition: The parameter, addend, is not null. Postcondition: The elements from addend have been placed at the end of this sequence. The current element of this sequence remains where it was, and the addend is also unchanged. Throws: NullPointerException Indicates that addend is null. Throws: OutOfMemoryError Indicates insufficient memory to increase the size of the sequence.
advance public void advance( ) Move forward so that the current element is now the next element in the sequence. Precondition: isCurrent( ) returns true. Postcondition: If the current element was already the end element of the sequence (with nothing after it), then there is no longer any current element. Otherwise, the new element is the element immediately after the original current element. Throws: IllegalStateException Indicates that there is no current element, so advance may not be called.
clone public Object clone( ) Generate a copy of this sequence. Returns: The return value is a copy of this sequence. Subsequent changes to the copy will not affect the original, nor vice versa. The return value must be typecast to a DoubleLinkedSeq before it is used. Throws: OutOfMemoryError Indicates insufficient memory for creating the clone.
concatenation public static DoubleLinkedSeq concatenation (DoubleLinkedSeq s1, DoubleLinkedSeq s2) Create a new sequence that contains all the elements from one sequence followed by another. Parameters: s1 the first of two sequences s2 the second of two sequences Precondition: Neither s1 nor s2 is null. Returns: a new sequence that has the elements of s1 followed by s2 (with no current element) Throws: IllegalArgumentException Indicates that one of the arguments is null. Throws: OutOfMemoryError Indicates insufficient memory for the new sequence.
getCurrent public double getCurrent( ) Accessor method to determine the current element of the sequence. Precondition: isCurrent( ) returns true. Returns: the current element of the sequence Throws: IllegalStateException Indicates that there is no current element.
isCurrent public boolean isCurrent( ) Accessor method to determine whether this sequence has a specified current element that can be retrieved with the getCurrent method. Returns: true (there is a current element) or false (there is no current element at the moment)
removeCurrent public void removeCurrent( ) Remove the current element from this sequence. Precondition: isCurrent( ) returns true. Postcondition: The current element has been removed from the sequence, and the following element (if there is one) is now the new current element. If there was no following element, then there is now no current element. Throws: IllegalStateException Indicates that there is no current element, so removeCurrent may not be called.
size public int size( ) Accessor method to determine the number of elements in this sequence. Returns: the number of elements in this sequence
start public void start( ) Set the current element at the front of the sequence. Postcondition: The front element of this sequence is now the current element (but if the sequence has no elements at all, then there is no current element).
.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started