Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*************************************JAVA*********************************** code must include the provided specifications of methods above where specification of each method shows up above it as structured comments. Java file must

*************************************JAVA***********************************

code must include the provided specifications of methods above where specification of each method shows up above it as structured comments.

Java file must compile with no compilation errors.

A sequence is similar to a bag in the sense that both are collection data types, however, unlike a bag, the elements in a sequence are arranged one after another.

Implement a data type named DoubleArraySeq to represent a sequence of elements of type double.

Instance variables for DoubleArraySeq:

DoubleArraySeq has the following instance variables:

1. data: a partially filled array that stores the elements of the sequence (this is similar to data in IntArrayBag data type, discussed in the class)

2. manyItems: keeps track of how much of the data array is currently being used (this is similar to manyItems in IntArrayBag)

3. currentIndex: keeps track of the index of the current element in the array (if there is one).

Methods for DoubleArraySeq:

DoubleArraySeq should have the following methods and implementation of each method must satisfy its given specification.

SPECIFICATION START

public class DoubleArraySeq from the package edu.colorado.collections

A DouleArraySeq keeps track of 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:

(1) The capacity of a sequence can change after its created, but the maximum capacity is limited by the amount of free memory of the machine. The constructor, addAfter, addBefore, clone, and concatenation will result in an OutofMemoryError when free memory is exhausted.

(2) A sequences capacity cannot exceed the largest integer, 2,147,483,647 (Integer.MAX_VALUE). Any attempt to create a larger capacity in failure due to the arithmetic overflow.

Specification

+ Constructor for the DoubleArraySeq

public DoubleArraySeq()

Initialize an empty sequence with an initial capacity of 10. Note that the addAfter and addBefore methods work efficiently (without needing more memory) until the capacity is reached.

Postcondition

This sequence is empty and has an initial capacity of 10.

Throws: OutofMemoryError

Indicates insuffiecnt memory for new double [10].

+ Second Constructor for the DoubleArraySeq

public DoubleArraySeq(int initialCapacity)

Initialize an empty sequence with a specified initial capacity. Note that the addAfter and addBefore methods work efficiently (without adding more memory) until this capacity is reached.

Parameter:

initialCapacity- the initial capacity of this sequence

Precondition:

initalCapacity is non-negative.

Postcondition:

This sequence is empty and has the given initial capacity.

Throws: IllegalArgumentException

Indicates that initialCapacity is negative.

Throws: OutofMemoryError

Indicates insufficient memory for new double [initialCapacity].

+ 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. If this new element would take this sequence beyond its current capacity, then the capacity is increased before adding the new element.

Parameters:

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, the 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, then addAfter places the new element at the end of this sequence, and addBefore places the new element at the front of this sequence. In all cases, the new element becomes the new current element of this sequence.

Throws: OutOfMemoryError

Indicates insufficient memory to increase the size of this sequence.

Note:

An attempt to increase the capacity beyond Interger.MAX_VALUE will cause this sequence to fail with an arithmetic overflow.

+ addAll

public void addAll(DoubleArraySeq addend)

Places 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 parameters, addend, is not null.

Postcondition:

The element 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: NullPointerExpection

Indicates that addend is null.

Throws: OutOfMemoryError

Indicates insufficient memory to increase the capacity of this sequence.

Note:

An attempt to increase the capacity beyond Interger.MAX_VALUE will cause this sequence to fail with an arithmetic overflow.

+ advance

public void advance( )

Move forward so that the current element is now the next element in this sequence.

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

Throws: IllegealStateException

Indicates that there is no current element, so advance may not be called.

+clone

public DoubleArraySeq 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 DoubleArraySeq before it is used.

Throws: OutofMemoryError

Indicates insufficient memory for creating a clone.

+ concatenation

public static DoubleArraySeq concatenation (DoubleArraySeq s1, DoubleArraySeq 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 seconds of two sequences

Precondition:

Neither s1 nor s2 is null.

Returns:

A new sequence that has the elements of s1 followed by the elements of s2(with no current element)

Throws: NullPointerException

Indicates that one of the arguments is null.

Throws: OutofMemoryError

Indicates insufficient memory for the new sequence.

Note:

An attempt to increase the capacity beyond Integer. MAX_VALUE will cause this sequence to fail with an arithmetic overflow.

+ ensureCapacity

public void ensureCapacity(int minimumCapacity)

Change the current capacity of this sequence

Parameters:

minimumCapacity the new capacity for this sequence

Postcondition:

This sequences capacity has been changed to at least minimumCapacity.

Throws: OutOfMemoryError

Indicates insufficient memory for new double[minimumCapacity].

+ getCapacity:

public int getCapacity( )

Accessor method to determine the current capacity of this sequence. The addBefore and addAfter methods efficiently (without needing more memory) until this capacity is reached.

Returns:

The current capacity of the sequence.

+ getCurrent:

public double getCurrent( )

Accessor method to determine the current element of this sequence.

Precondition:

isCurrent( ) returns true.

Returns:

The current element of this 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 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 this sequence, and the following element (if there is one) is now 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 element in this sequence.

Returns:

The number of elements of this sequence

+ start

public void start( )

Set the current element at the front of this sequence

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).

+ trimToSize

public void trimToSize( )

Reduce the current capacity of this sequence to its actual size (i.e., the number of elements its contains).

Postcondition:

This sequences capacity has been to its current size.

Throws: OutOfMemroyError

Indicates insufficient memory for altering the capacity

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

More Books

Students also viewed these Databases questions