Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1: Implement the Sequence class from Section 4.5. Outline of Java code for this class is available in DoubleLinkedSeq.java. Your sequence class must have five

1: Implement the Sequence class from Section 4.5. Outline of Java code for this class is available in DoubleLinkedSeq.java. Your sequence class must have five private instance variables as described in Section 4.5. You need to use DoubleNode.java for your node class. Follow instructions from Section 4.5, except that you do not need to put your class in a package. You need to write the invariant for your sequence ADT (invariant is explained on p.126). You need to add implementations instead of blank implementations for the following:

constructor DoubleLinkedSeq( )

addAfter

addBefore

addAll

advance

clone

concatenation

getCurrent

isCurrent

removeCurrent

size

start

In addition to that you need to implement the following methods:

addAtFront - a method to add a new element at the front of the sequence

removeAtFront - a method to remove the element at the front of the sequence

toIth - a method that makes the ith element become the current element (assume that the front element is the 1st)

toString - a method that represents the sequence as a string (in any reasonable format which shows the order of elements)

reverse a method that returns a new sequence that contains the same elements as the original sequence but in reverse order. The original sequence should remain unchanged. For instance, if original sequence contains elements 1.2 - 3.5 - 4.3 - 6.44 in this order, then the new sequence should contain elements 6.44 - 4.3 - 3.5 - 1.2. The header of the method should be the following:

public DoubleLinkedSeq reverse( )

everyOther a method that returns a new sequence that contains every other element of the original sequence. The original sequence should remain unchanged. For instance, if original sequence contains elements 1.2 - 3.5 - 4.3 - 6.44, then the new sequence should contain elements 1.2 - 4.3. If the original sequence contains elements 3.2 - 2.5 - 1.0 - 7.1 - 9.5, then

the method should return sequence 3.2 - 1.0 - 9.5. The header of the method should be the following:

public DoubleLinkedSeq everyOther( )

removeSmaller a method that removes from the sequence all elements that are less than a given value which is passed as a parameter. For instance, if original sequence contains elements 5.2 - 3.5 - 4.3 - 6.44 2.5 and the value is 5.0, then the sequence should become 5.2 - 6.44. If all elements in the sequence are less than the value, then the sequence should become empty. The header of the method should be the following:

public void removeSmaller(double value)

--------------------------------------------------------------------------------------

public class DoubleLinkedSeq implements Cloneable

{

public DoubleLinkedSeq( )

{

// Implemented by student.

}

/**

* Add a new element to this sequence, after the current element.

* @param 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, then the new element is placed after the current

* element. If there was no current element, then the new element is placed

* at the end of the sequence. In all cases, the new element becomes the

* new current element of this sequence.

* @exception OutOfMemoryError

* Indicates insufficient memory for a new node.

**/

public void addAfter(double element)

{

// Implemented by student.

}

/**

* Add a new element to this sequence, before the current element.

* @param 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, then the new element is placed before the current

* element. If there was no current element, then the new element is placed

* at the start of the sequence. In all cases, the new element becomes the

* new current element of this sequence.

* @exception OutOfMemoryError

* Indicates insufficient memory for a new node.

**/

public void addBefore(double element)

{

// Implemented by student.

}

/**

* Place the contents of another sequence at the end of this sequence.

* @param 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.

* @exception NullPointerException

* Indicates that addend is null.

* @exception OutOfMemoryError

* Indicates insufficient memory to increase the size of this sequence.

**/

public void addAll(DoubleLinkedSeq addend)

{

// Implemented by student.

}

/**

* Move forward, so that the current element is now the next element in

* this sequence.

* @param - none

*

Precondition:

* isCurrent() returns true.

*

Postcondition:

* If the current element was already the end element of this 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.

* @exception IllegalStateException

* Indicates that there is no current element, so

* advance may not be called.

**/

public void advance( )

{

// Implemented by student.

}

/**

* Generate a copy of this sequence.

* @param - none

* @return

* The return value is a copy of this sequence. Subsequent changes to the

* copy will not affect the original, nor vice versa.

* @exception OutOfMemoryError

* Indicates insufficient memory for creating the clone.

**/

public DoubleLinkedSeq clone( )

{ // Clone a DoubleLinkedSeq object.

// Student will replace this return statement with their own code:

return null;

}

/**

* Create a new sequence that contains all the elements from one sequence

* followed by another.

* @param s1

* the first of two sequences

* @param s2

* the second of two sequences

*

Precondition:

* Neither s1 nor s2 is null.

* @return

* a new sequence that has the elements of s1 followed by the

* elements of s2 (with no current element)

* @exception NullPointerException.

* Indicates that one of the arguments is null.

* @exception OutOfMemoryError

* Indicates insufficient memory for the new sequence.

**/

public static DoubleLinkedSeq concatenation(DoubleLinkedSeq s1, DoubleLinkedSeq s2)

{

// Student will replace this return statement with their own code:

return null;

}

/**

* Accessor method to get the current element of this sequence.

* @param - none

*

Precondition:

* isCurrent() returns true.

* @return

* the current element of this sequence

* @exception IllegalStateException

* Indicates that there is no current element, so

* getCurrent may not be called.

**/

public double getCurrent( )

{

// Student will replace this return statement with their own code:

return 0;

}

/**

* Accessor method to determine whether this sequence has a specified

* current element that can be retrieved with the

* getCurrent method.

* @param - none

* @return

* true (there is a current element) or false (there is no current element at the moment)

**/

public boolean isCurrent( )

{

// Student will replace this return statement with their own code:

return true;

}

/**

* Remove the current element from this sequence.

* @param - none

*

Precondition:

* isCurrent() returns true.

*

Postcondition:

* The current element has been removed from this 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.

* @exception IllegalStateException

* Indicates that there is no current element, so

* removeCurrent may not be called.

**/

public void removeCurrent( )

{

// Implemented by student.

}

/**

* Determine the number of elements in this sequence.

* @param - none

* @return

* the number of elements in this sequence

**/

public int size( )

{

// Student will replace this return statement with their own code:

return 0;

}

/**

* Set the current element at the front of this sequence.

* @param - none

*

Postcondition:

* The front element of this sequence is now the current element (but

* if this sequence has no elements at all, then there is no current

* element).

**/

public void start( )

{

// Implemented by student.

}

}

-------------------------------------------------------------------------

public class DoubleNode

{

// Invariant of the DoubleNode class:

// 1. The node's double data is in the instance variable data.

// 2. For the final node of a list, the link part is null.

// Otherwise, the link part is a reference to the

// next node of the list.

private double data;

private DoubleNode link;

/**

* Initialize a node with a specified initial data and link to the next

* node. Note that the initialLink may be the null reference,

* which indicates that the new node has nothing after it.

* @param initialData

* the initial data of this new node

* @param initialLink

* a reference to the node after this new node--this reference may be null

* to indicate that there is no node after this new node.

* @postcondition

* This node contains the specified data and link to the next node.

**/

public DoubleNode(double initialData, DoubleNode initialLink)

{

data = initialData;

link = initialLink;

}

/**

* Modification method to add a new node after this node.

* @param item

* the data to place in the new node

* @postcondition

* A new node has been created and placed after this node.

* The data for the new node is item. Any other nodes

* that used to be after this node are now after the new node.

* @exception OutOfMemoryError

* Indicates that there is insufficient memory for a new

* DoubleNode.

**/

public void addNodeAfter(double item)

{

link = new DoubleNode(item, link);

}

/**

* Accessor method to get the data from this node.

* @param - none

* @return

* the data from this node

**/

public double getData( )

{

return data;

}

/**

* Accessor method to get a reference to the next node after this node.

* @param - none

* @return

* a reference to the node after this node (or the null reference if there

* is nothing after this node)

**/

public DoubleNode getLink( )

{

return link;

}

/**

* Copy a list.

* @param source

* the head of a linked list that will be copied (which may be

* an empty list in where source is null)

* @return

* The method has made a copy of the linked list starting at

* source. The return value is the head reference for the

* copy.

* @exception OutOfMemoryError

* Indicates that there is insufficient memory for the new list.

**/

public static DoubleNode listCopy(DoubleNode source)

{

DoubleNode copyHead;

DoubleNode copyTail;

// Handle the special case of the empty list.

if (source == null)

return null;

// Make the first node for the newly created list.

copyHead = new DoubleNode(source.data, null);

copyTail = copyHead;

// Make the rest of the nodes for the newly created list.

while (source.link != null)

{

source = source.link;

copyTail.addNodeAfter(source.data);

copyTail = copyTail.link;

}

// Return the head reference for the new list.

return copyHead;

}

/**

* Copy a list, returning both a head and tail reference for the copy.

* @param source

* the head of a linked list that will be copied (which may be

* an empty list in where source is null)

* @return

* The method has made a copy of the linked list starting at

* source. The return value is an

* array where the [0] element is a head reference for the copy and the [1]

* element is a tail reference for the copy.

* @exception OutOfMemoryError

* Indicates that there is insufficient memory for the new list.

**/

public static DoubleNode[ ] listCopyWithTail(DoubleNode source)

{

DoubleNode copyHead;

DoubleNode copyTail;

DoubleNode[ ] answer = new DoubleNode[2];

// Handle the special case of the empty list.

if (source == null)

return answer; // The answer has two null references .

// Make the first node for the newly created list.

copyHead = new DoubleNode(source.data, null);

copyTail = copyHead;

// Make the rest of the nodes for the newly created list.

while (source.link != null)

{

source = source.link;

copyTail.addNodeAfter(source.data);

copyTail = copyTail.link;

}

// Return the head and tail references.

answer[0] = copyHead;

answer[1] = copyTail;

return answer;

}

/**

* Compute the number of nodes in a linked list.

* @param head

* the head reference for a linked list (which may be an empty list

* with a null head)

* @return

* the number of nodes in the list with the given head

* @note

* A wrong answer occurs for lists longer than Int.MAX_VALUE.

**/

public static int listLength(DoubleNode head)

{

DoubleNode cursor;

int answer;

answer = 0;

for (cursor = head; cursor != null; cursor = cursor.link)

answer++;

return answer;

}

/**

* Copy part of a list, providing a head and tail reference for the new copy.

* @param start/end

* references to two nodes of a linked list

* @param copyHead/copyTail

* the method sets these to refer to the head and tail node of the new

* list that is created

* @precondition

* start and end are non-null references to nodes

* on the same linked list,

* with the start node at or before the end node.

* @return

* The method has made a copy of the part of a linked list, from the

* specified start node to the specified end node. The return value is an

* array where the [0] component is a head reference for the copy and the

* [1] component is a tail reference for the copy.

* @exception IllegalArgumentException

* Indicates that start and end are not references

* to nodes on the same list.

* @exception NullPointerException

* Indicates that start is null.

* @exception OutOfMemoryError

* Indicates that there is insufficient memory for the new list.

**/

public static DoubleNode[ ] listPart(DoubleNode start, DoubleNode end)

{

DoubleNode copyHead;

DoubleNode copyTail;

DoubleNode cursor;

DoubleNode[ ] answer = new DoubleNode[2];

// Make the first node for the newly created list. Notice that this will

// cause a NullPointerException if start is null.

copyHead = new DoubleNode(start.data, null);

copyTail = copyHead;

cursor = start;

// Make the rest of the nodes for the newly created list.

while (cursor != end)

{

cursor = cursor.link;

if (cursor == null)

throw new IllegalArgumentException

("end node was not found on the list");

copyTail.addNodeAfter(cursor.data);

copyTail = copyTail.link;

}

// Return the head and tail references

answer[0] = copyHead;

answer[1] = copyTail;

return answer;

}

/**

* Find a node at a specified position in a linked list.

* @param head

* the head reference for a linked list (which may be an empty list in

* which case the head is null)

* @param position

* a node number

* @precondition

* position > 0.

* @return

* The return value is a reference to the node at the specified position in

* the list. (The head node is position 1, the next node is position 2, and

* so on.) If there is no such position (because the list is too short),

* then the null reference is returned.

* @exception IllegalArgumentException

* Indicates that position is not positive.

**/

public static DoubleNode listPosition(DoubleNode head, int position)

{

DoubleNode cursor;

int i;

if (position <= 0)

throw new IllegalArgumentException("position is not positive");

cursor = head;

for (i = 1; (i < position) && (cursor != null); i++)

cursor = cursor.link;

return cursor;

}

/**

* Search for a particular piece of data in a linked list.

* @param head

* the head reference for a linked list (which may be an empty list in

* which case the head is null)

* @param target

* a piece of data to search for

* @return

* The return value is a reference to the first node that contains the

* specified target. If there is no such node, the null reference is

* returned.

**/

public static DoubleNode listSearch(DoubleNode head, double target)

{

DoubleNode cursor;

for (cursor = head; cursor != null; cursor = cursor.link)

if (target == cursor.data)

return cursor;

return null;

}

/**

* Modification method to remove the node after this node.

* @param - none

* @precondition

* This node must not be the tail node of the list.

* @postcondition

* The node after this node has been removed from the linked list.

* If there were further nodes after that one, they are still

* present on the list.

* @exception NullPointerException

* Indicates that this was the tail node of the list, so there is nothing

* after it to remove.

**/

public void removeNodeAfter( )

{

link = link.link;

}

/**

* Modification method to set the data in this node.

* @param newData

* the new data to place in this node

* @postcondition

* The data of this node has been set to newData.

**/

public void setData(double newData)

{

data = newData;

}

/**

* Modification method to set the link to the next node after this node.

* @param newLink

* a reference to the node that should appear after this node in the linked

* list (or the null reference if there is no node after this node)

* @postcondition

* The link to the node after this node has been set to newLink.

* Any other node (that used to be in this link) is no longer connected to

* this node.

**/

public void setLink(DoubleNode newLink)

{

link = newLink;

}

}

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

25 Vba Macros For Data Analysis In Microsoft Excel

Authors: Klemens Nguyen

1st Edition

B0CNSXYMTC, 979-8868455629

More Books

Students also viewed these Databases questions