SlideShare a Scribd company logo
1 of 26
Download to read offline
public class DoubleArraySeq implements Cloneable
{
// Private Instance Variables
private double[] data;
private int manyItems;
private int currentIndex;
//Constructor Methods
/**
* Initialize an empty sequence with an initial capacity of 10.
**/
public DoubleArraySeq()
{
try{
//Set a default capacity for new DoubleArraySeq's.
int INITAL_CAPACITY = 10;
//Set each instance variable to its initial value.
data = new double[INITAL_CAPACITY];
manyItems = 0;
currentIndex = 0;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory to create a new
sequence!");
}//end catch
}//end DoubleArraySeq() method
/**
* Initialize an empty sequence with a specified initial capacity. Note that the addAfter and
addBefore methods work
* efficiently (without needing more memory) until this capacity is reached.
**/
public DoubleArraySeq(int initialCapacity)
{
try{
//Set each instance variable to its initial value.
data = new double[initialCapacity];
currentIndex = 0;
manyItems = 0;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory to create a new sequence
of capacity " + initialCapacity + "!");
}//end catch
}//end DoubleArraySeq(int initialCapacity) method
// Accessor Methods
/**
**/
public boolean isCurrent()
{
return (currentIndex < manyItems);
}//end isCurrent() method
/**
* Accessor method to get the current element of this sequence.
**/
public double getCurrent()
{
//Confirm that there is a current element first.
if (isCurrent())
return data[currentIndex];
else
throw new IllegalStateException("There is no current element! Please specify a current
element first.");
}//end getCurrent() method
/**
* Accessor method to get the current capacity of this sequence.
**/
public int getCapacity()
{
//Returns the number of indexes in the array.
return data.length;
}//end getCapacity() method
/**
* Accessor method to get the available capacity (number of empty indexes) of this sequence.
* The available capacity (number of empty indexes) of this sequence.
**/
public int getAvailCapacity()
{
//Returns the number of empty indexes in the array.
return data.length - manyItems;
}//end getAvailCapacity() method
/**
**/
public int size()
{
//Returns the number of elements in the sequence.
return manyItems;
}//end size() method
// Setter Methods
/**
* A method to move forward, so the current element is now the next element in this sequence.
**/
public void advance()
{
if (isCurrent())
currentIndex++;
else
throw new IllegalStateException ("There is no current element! Advance may not be
called.");
}//end advance() method
/**
* A method to set the current element at the front of this sequence.
**/
public void start()
{
if (manyItems > 0)
currentIndex = 0;
else
throw new IllegalStateException("This sequence is empty!");
}//end start() method
/**
* A method that makes the last element of the sequence the current element.
**/
public void setCurrentLast()
{
if (manyItems > 0)
currentIndex = manyItems - 1;
else
throw new IllegalStateException("This sequence is empty!");
}//end setCurrentLast() method
/**
**/
public double setCurrent(int n)
{
//'n' must range from 1 to manyItems and the DoubleArraySeq may not be empty.
if (manyItems > 0 && n > 0 && n <= manyItems){
currentIndex = n-1;
return data[currentIndex];
}//end if
else
throw new IllegalStateException ("This sequence is either empty or 'n' is greater than
the sequence size (or less than 1)!");
}//end setCurrent(int n) method
/**
* A method that and makes the selected element the current element and returns what nth
number of the sequence that element is.
**/
public int getElement(double element)
{
//Verify that the sequence is not empty.
if (manyItems < 1)
throw new IllegalStateException ("This sequence is empty!");
//Search for the element in the sequence and return what nth number of the sequence that
element is, if found.
int i;
for (i = 0; i < manyItems; i++){
if (data[i] == element){
currentIndex = i;
return currentIndex + 1;
}//end if
}//end for
if (i == manyItems)
throw new IllegalStateException ("This sequence does not contain the element " +
element + "!");
else
return 0;
}//end getElement(double element) method
// Size Management Methods
/**
* A method to change the current capacity of this sequence.
**/
public void ensureCapacity(int minimumCapacity)
{
try{
if (getCapacity() < minimumCapacity){
//Create a new array of size minimumCapacity.
double[] expandData = new double[minimumCapacity];
//Copy all elements from data into the new array.
System.arraycopy(data, 0, expandData, 0, manyItems);
//Change data's reference to expandData.
data = expandData;
}//end if
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("This sequence capacity is too large! There is not
enough memory to store a sequence of capacity " + minimumCapacity + "! "
+ "Note: The add methods double the sequence capacity if maximum
capacity has already been reached. "
+ "If necessary, try manually ensuring a smaller capacity before adding
more elements to this sequence.");
}//end catch
}//end ensureCapacity(int minimumCapacity) method
/**
* A method to reduce the current capacity of this sequence to its actual size (i.e., the number
of elements it contains).
**/
public void trimToSize()
{
try{
if (data.length > manyItems){
//Create a new double[] of size manyItems that data will point to after running this
method.
double[] trimmedArray = new double[manyItems];
//Copy the information from data to trimmedArray. Then assign data to trimmedArray.
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}//end if
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory left to alter the capacity
of this sequence!");
}//end catch
}//end trimToSize() method
// Add Element Methods
/**
* A method to add a new element to this sequence, after the current element.
**/
public void addAfter(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
if (isCurrent()){
//Move all elements up an index, beginning at the end of the DoubleArraySeq and ending
at the index after currentIndex.
for (int i = manyItems; i > (currentIndex + 1); i--)
data[i] = data[i-1];
//Add the new element after the current element.
currentIndex++;
data[currentIndex] = element;
manyItems++;
}//end if
else{
//If there is no current element, add the element to the end of the sequence.
currentIndex = manyItems;
data[currentIndex] = element;
manyItems++;
}//end else
}//end addAfter(double element) method
/**
* A method to add a new element to this sequence, before the current element.
* An attempt to increase the capacity beyond Integer.MAX_VALUE will cause the sequence
to fail with an arithmetic overflow.
**/
public void addBefore(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
if (isCurrent()){
//Move all elements up an index, beginning at the end of the DoubleArraySeq and ending
at currentIndex.
for (int i = manyItems; i > currentIndex; i--)
data[i] = data[i-1];
//Add the new element before the current element (in the current element's old index).
data[currentIndex] = element;
manyItems ++;
}//end if
else{ //currentIndex is beyond the last element or the DoubleArraySeq is empty.
//Move all elements in the sequence up an index (only if currentIndex is beyond the last
element).
for (int i = manyItems; i > 0; i--)
data[i] = data[i-1];
//Add the new element to the beginning of the sequence.
currentIndex = 0;
data[currentIndex] = element;
manyItems ++;
}//end else
}//end addBefore(double element) method
/**
* A method to add a new element at the front of the sequence and make it the current element.
**/
public void addFront(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
//Move all elements in the sequence up an index.
for (int i = manyItems; i > 0; i--)
data[i] = data[i-1];
//Add the new element to the beginning of the sequence.
currentIndex = 0;
data[currentIndex] = element;
manyItems ++;
}//end addFront(double element) method
/**
* A method to add a new element at the end of the sequence and make it the current element.
**/
public void addEnd(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
//Add the new element to the end of the sequence.
data[manyItems] = element;
currentIndex = manyItems;
manyItems++;
}//end addEnd(double element) method
/**
* A method to place the contents of another sequence at the end of this sequence.
* An attempt to increase the capacity beyond Integer.MAX_VALUE will cause an arithmetic
overflow that will cause the sequence to fail.
**/
public void addAll(DoubleArraySeq addend)
{
//Make sure there is enough capacity to add the other DoubleArraySeq.
ensureCapacity(manyItems + addend.manyItems);
//Copy the addend sequence to the end of the invoked sequence.
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}//end addAll(DoubleArraySeq addend) method
/**
* A method to create a new sequence that contains all the elements from one sequence
followed by another.
**/
public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2)
{
try{
//Create a new DoubleArraySeq large enough to store the s1 and s2 DoubleArraySeq's.
DoubleArraySeq newSequence = new DoubleArraySeq(s1.manyItems + s2.manyItems);
//Copy DoubleArraySeq s1 and s2 to the new DoubleArraySeq, newSequence.
System.arraycopy(s1.data, 0, newSequence.data, 0, s1.manyItems);
System.arraycopy(s2.data, 0, newSequence.data, s1.manyItems, s2.manyItems);
newSequence.manyItems = (s1.manyItems + s2.manyItems);
newSequence.currentIndex = newSequence.manyItems;
return newSequence;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("The sequences are too large! There is not enough
memory to concatenate these sequences!");
}//end catch
}//end concatenation(DoubleArraySeq s1, DoubleArraySeq s2) method
// Remove Element Methods
/**
* A method to remove the current element from this sequence.
* Indicates that there is no current element, so removeCurrent may not be called.
**/
public void removeCurrent()
{
if (isCurrent()){
//Move each element down an index, starting with the element after the currentIndex.
for (int i = currentIndex; i < manyItems; i++){
data[i] = data[i + 1];
}//end for loop
manyItems--;
}//end if
else
throw new IllegalStateException ("There is no current element!");
}//end removeCurrent() method
/**
* A method to remove the first element of the sequence.
* Indicates that the sequence is empty.
**/
public void removeFront()
{
if (manyItems > 0){
currentIndex = 0;
removeCurrent();
}//end if
else
throw new IllegalStateException ("The sequence is empty!");
}//end removeFront() method
// Overridden Java Methods -- clone(), equals(Object obj), toString()
/**
* A method to generate an independent copy (clone) of this sequence.
* Indicates insufficient memory for creating the clone.
**/
public DoubleArraySeq clone()
{
//Create a new DoubleArraySeq that will be returned as the clone of the invoked
DoubleArraySeq.
DoubleArraySeq answer;
try{
//Clone the instance variables of the invoked DoubleArraySeq and assign them to the
answer DoubleArraySeq.
answer = (DoubleArraySeq) super.clone( );
}//end try
catch (CloneNotSupportedException e){
// This exception should not occur. But if it does, it would probably indicate a
programming error that made super.clone unavailable.
// The most common error would be forgetting the "Implements Cloneable" clause at the
start of this class.
throw new RuntimeException ("This class does not implement Cloneable");
}//end catch
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory available to clone this
sequence!");
}//end catch
//Copy the information in the data instance variable (double[]) from the invoked
DoubleArraySeq to its clone.
answer.data = data.clone( );
return answer;
}//end DoubleArraySeq clone() method
/**
* A method to compare two DoubleArraySeq objects and determine if they are equivalent.
**/
public boolean equals(Object obj)
{
boolean areEqual = false;
//Verify 1) That obj is a DoubleArraySeq.
if (obj instanceof DoubleArraySeq){
DoubleArraySeq candidate = (DoubleArraySeq) obj;
//Verify 2) That candidate has the same number of elements as the invoked
DoubleArraySeq.
if (this.manyItems == candidate.manyItems){
//Verify 3) That the elements in candidate and the invoked DoubleArraySeq are the
same elements, in the same order.
boolean isEqual = true;
for (int i = 0; i < manyItems && isEqual; i++){
if (this.data[i] != candidate.data[i])
isEqual = false;
}//end for loop
if (isEqual)
areEqual = true;
}//end if
}//end if
return areEqual;
}//end equals(Object obj)
/**
* A method to print all elements of the sequence in order, separated by a space.
**/
public String toString()
{
//Make a String containing all the elements of this sequence in order.
StringBuilder dataString = new StringBuilder("");
if (manyItems > 0){
for(int i = 0; i < manyItems; i++)
dataString.append(data[i] + " ");
}//end if
else
throw new IllegalStateException ("There is nothing in this sequence!");
return dataString.toString();
}//end toString() method
}//end DoubleArraySeq class
Solution
public class DoubleArraySeq implements Cloneable
{
// Private Instance Variables
private double[] data;
private int manyItems;
private int currentIndex;
//Constructor Methods
/**
* Initialize an empty sequence with an initial capacity of 10.
**/
public DoubleArraySeq()
{
try{
//Set a default capacity for new DoubleArraySeq's.
int INITAL_CAPACITY = 10;
//Set each instance variable to its initial value.
data = new double[INITAL_CAPACITY];
manyItems = 0;
currentIndex = 0;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory to create a new
sequence!");
}//end catch
}//end DoubleArraySeq() method
/**
* Initialize an empty sequence with a specified initial capacity. Note that the addAfter and
addBefore methods work
* efficiently (without needing more memory) until this capacity is reached.
**/
public DoubleArraySeq(int initialCapacity)
{
try{
//Set each instance variable to its initial value.
data = new double[initialCapacity];
currentIndex = 0;
manyItems = 0;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory to create a new sequence
of capacity " + initialCapacity + "!");
}//end catch
}//end DoubleArraySeq(int initialCapacity) method
// Accessor Methods
/**
**/
public boolean isCurrent()
{
return (currentIndex < manyItems);
}//end isCurrent() method
/**
* Accessor method to get the current element of this sequence.
**/
public double getCurrent()
{
//Confirm that there is a current element first.
if (isCurrent())
return data[currentIndex];
else
throw new IllegalStateException("There is no current element! Please specify a current
element first.");
}//end getCurrent() method
/**
* Accessor method to get the current capacity of this sequence.
**/
public int getCapacity()
{
//Returns the number of indexes in the array.
return data.length;
}//end getCapacity() method
/**
* Accessor method to get the available capacity (number of empty indexes) of this sequence.
* The available capacity (number of empty indexes) of this sequence.
**/
public int getAvailCapacity()
{
//Returns the number of empty indexes in the array.
return data.length - manyItems;
}//end getAvailCapacity() method
/**
**/
public int size()
{
//Returns the number of elements in the sequence.
return manyItems;
}//end size() method
// Setter Methods
/**
* A method to move forward, so the current element is now the next element in this sequence.
**/
public void advance()
{
if (isCurrent())
currentIndex++;
else
throw new IllegalStateException ("There is no current element! Advance may not be
called.");
}//end advance() method
/**
* A method to set the current element at the front of this sequence.
**/
public void start()
{
if (manyItems > 0)
currentIndex = 0;
else
throw new IllegalStateException("This sequence is empty!");
}//end start() method
/**
* A method that makes the last element of the sequence the current element.
**/
public void setCurrentLast()
{
if (manyItems > 0)
currentIndex = manyItems - 1;
else
throw new IllegalStateException("This sequence is empty!");
}//end setCurrentLast() method
/**
**/
public double setCurrent(int n)
{
//'n' must range from 1 to manyItems and the DoubleArraySeq may not be empty.
if (manyItems > 0 && n > 0 && n <= manyItems){
currentIndex = n-1;
return data[currentIndex];
}//end if
else
throw new IllegalStateException ("This sequence is either empty or 'n' is greater than
the sequence size (or less than 1)!");
}//end setCurrent(int n) method
/**
* A method that and makes the selected element the current element and returns what nth
number of the sequence that element is.
**/
public int getElement(double element)
{
//Verify that the sequence is not empty.
if (manyItems < 1)
throw new IllegalStateException ("This sequence is empty!");
//Search for the element in the sequence and return what nth number of the sequence that
element is, if found.
int i;
for (i = 0; i < manyItems; i++){
if (data[i] == element){
currentIndex = i;
return currentIndex + 1;
}//end if
}//end for
if (i == manyItems)
throw new IllegalStateException ("This sequence does not contain the element " +
element + "!");
else
return 0;
}//end getElement(double element) method
// Size Management Methods
/**
* A method to change the current capacity of this sequence.
**/
public void ensureCapacity(int minimumCapacity)
{
try{
if (getCapacity() < minimumCapacity){
//Create a new array of size minimumCapacity.
double[] expandData = new double[minimumCapacity];
//Copy all elements from data into the new array.
System.arraycopy(data, 0, expandData, 0, manyItems);
//Change data's reference to expandData.
data = expandData;
}//end if
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("This sequence capacity is too large! There is not
enough memory to store a sequence of capacity " + minimumCapacity + "! "
+ "Note: The add methods double the sequence capacity if maximum
capacity has already been reached. "
+ "If necessary, try manually ensuring a smaller capacity before adding
more elements to this sequence.");
}//end catch
}//end ensureCapacity(int minimumCapacity) method
/**
* A method to reduce the current capacity of this sequence to its actual size (i.e., the number
of elements it contains).
**/
public void trimToSize()
{
try{
if (data.length > manyItems){
//Create a new double[] of size manyItems that data will point to after running this
method.
double[] trimmedArray = new double[manyItems];
//Copy the information from data to trimmedArray. Then assign data to trimmedArray.
System.arraycopy(data, 0, trimmedArray, 0, manyItems);
data = trimmedArray;
}//end if
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory left to alter the capacity
of this sequence!");
}//end catch
}//end trimToSize() method
// Add Element Methods
/**
* A method to add a new element to this sequence, after the current element.
**/
public void addAfter(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
if (isCurrent()){
//Move all elements up an index, beginning at the end of the DoubleArraySeq and ending
at the index after currentIndex.
for (int i = manyItems; i > (currentIndex + 1); i--)
data[i] = data[i-1];
//Add the new element after the current element.
currentIndex++;
data[currentIndex] = element;
manyItems++;
}//end if
else{
//If there is no current element, add the element to the end of the sequence.
currentIndex = manyItems;
data[currentIndex] = element;
manyItems++;
}//end else
}//end addAfter(double element) method
/**
* A method to add a new element to this sequence, before the current element.
* An attempt to increase the capacity beyond Integer.MAX_VALUE will cause the sequence
to fail with an arithmetic overflow.
**/
public void addBefore(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
if (isCurrent()){
//Move all elements up an index, beginning at the end of the DoubleArraySeq and ending
at currentIndex.
for (int i = manyItems; i > currentIndex; i--)
data[i] = data[i-1];
//Add the new element before the current element (in the current element's old index).
data[currentIndex] = element;
manyItems ++;
}//end if
else{ //currentIndex is beyond the last element or the DoubleArraySeq is empty.
//Move all elements in the sequence up an index (only if currentIndex is beyond the last
element).
for (int i = manyItems; i > 0; i--)
data[i] = data[i-1];
//Add the new element to the beginning of the sequence.
currentIndex = 0;
data[currentIndex] = element;
manyItems ++;
}//end else
}//end addBefore(double element) method
/**
* A method to add a new element at the front of the sequence and make it the current element.
**/
public void addFront(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
//Move all elements in the sequence up an index.
for (int i = manyItems; i > 0; i--)
data[i] = data[i-1];
//Add the new element to the beginning of the sequence.
currentIndex = 0;
data[currentIndex] = element;
manyItems ++;
}//end addFront(double element) method
/**
* A method to add a new element at the end of the sequence and make it the current element.
**/
public void addEnd(double element)
{
//Make sure there is enough capacity to add another element.
if (data.length == manyItems)
ensureCapacity(manyItems*2 + 1);
//Add the new element to the end of the sequence.
data[manyItems] = element;
currentIndex = manyItems;
manyItems++;
}//end addEnd(double element) method
/**
* A method to place the contents of another sequence at the end of this sequence.
* An attempt to increase the capacity beyond Integer.MAX_VALUE will cause an arithmetic
overflow that will cause the sequence to fail.
**/
public void addAll(DoubleArraySeq addend)
{
//Make sure there is enough capacity to add the other DoubleArraySeq.
ensureCapacity(manyItems + addend.manyItems);
//Copy the addend sequence to the end of the invoked sequence.
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
manyItems += addend.manyItems;
}//end addAll(DoubleArraySeq addend) method
/**
* A method to create a new sequence that contains all the elements from one sequence
followed by another.
**/
public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2)
{
try{
//Create a new DoubleArraySeq large enough to store the s1 and s2 DoubleArraySeq's.
DoubleArraySeq newSequence = new DoubleArraySeq(s1.manyItems + s2.manyItems);
//Copy DoubleArraySeq s1 and s2 to the new DoubleArraySeq, newSequence.
System.arraycopy(s1.data, 0, newSequence.data, 0, s1.manyItems);
System.arraycopy(s2.data, 0, newSequence.data, s1.manyItems, s2.manyItems);
newSequence.manyItems = (s1.manyItems + s2.manyItems);
newSequence.currentIndex = newSequence.manyItems;
return newSequence;
}//end try
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("The sequences are too large! There is not enough
memory to concatenate these sequences!");
}//end catch
}//end concatenation(DoubleArraySeq s1, DoubleArraySeq s2) method
// Remove Element Methods
/**
* A method to remove the current element from this sequence.
* Indicates that there is no current element, so removeCurrent may not be called.
**/
public void removeCurrent()
{
if (isCurrent()){
//Move each element down an index, starting with the element after the currentIndex.
for (int i = currentIndex; i < manyItems; i++){
data[i] = data[i + 1];
}//end for loop
manyItems--;
}//end if
else
throw new IllegalStateException ("There is no current element!");
}//end removeCurrent() method
/**
* A method to remove the first element of the sequence.
* Indicates that the sequence is empty.
**/
public void removeFront()
{
if (manyItems > 0){
currentIndex = 0;
removeCurrent();
}//end if
else
throw new IllegalStateException ("The sequence is empty!");
}//end removeFront() method
// Overridden Java Methods -- clone(), equals(Object obj), toString()
/**
* A method to generate an independent copy (clone) of this sequence.
* Indicates insufficient memory for creating the clone.
**/
public DoubleArraySeq clone()
{
//Create a new DoubleArraySeq that will be returned as the clone of the invoked
DoubleArraySeq.
DoubleArraySeq answer;
try{
//Clone the instance variables of the invoked DoubleArraySeq and assign them to the
answer DoubleArraySeq.
answer = (DoubleArraySeq) super.clone( );
}//end try
catch (CloneNotSupportedException e){
// This exception should not occur. But if it does, it would probably indicate a
programming error that made super.clone unavailable.
// The most common error would be forgetting the "Implements Cloneable" clause at the
start of this class.
throw new RuntimeException ("This class does not implement Cloneable");
}//end catch
catch (OutOfMemoryError e){
throw new OutOfMemoryError ("There is not enough memory available to clone this
sequence!");
}//end catch
//Copy the information in the data instance variable (double[]) from the invoked
DoubleArraySeq to its clone.
answer.data = data.clone( );
return answer;
}//end DoubleArraySeq clone() method
/**
* A method to compare two DoubleArraySeq objects and determine if they are equivalent.
**/
public boolean equals(Object obj)
{
boolean areEqual = false;
//Verify 1) That obj is a DoubleArraySeq.
if (obj instanceof DoubleArraySeq){
DoubleArraySeq candidate = (DoubleArraySeq) obj;
//Verify 2) That candidate has the same number of elements as the invoked
DoubleArraySeq.
if (this.manyItems == candidate.manyItems){
//Verify 3) That the elements in candidate and the invoked DoubleArraySeq are the
same elements, in the same order.
boolean isEqual = true;
for (int i = 0; i < manyItems && isEqual; i++){
if (this.data[i] != candidate.data[i])
isEqual = false;
}//end for loop
if (isEqual)
areEqual = true;
}//end if
}//end if
return areEqual;
}//end equals(Object obj)
/**
* A method to print all elements of the sequence in order, separated by a space.
**/
public String toString()
{
//Make a String containing all the elements of this sequence in order.
StringBuilder dataString = new StringBuilder("");
if (manyItems > 0){
for(int i = 0; i < manyItems; i++)
dataString.append(data[i] + " ");
}//end if
else
throw new IllegalStateException ("There is nothing in this sequence!");
return dataString.toString();
}//end toString() method
}//end DoubleArraySeq class

More Related Content

Similar to public class DoubleArraySeq implements Cloneable {    Priva.pdf

Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
siennatimbok52331
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
flashfashioncasualwe
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
mail931892
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
firstchoiceajmer
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdf
ezonesolutions
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
mail931892
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
kostikjaylonshaewe47
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
ARCHANASTOREKOTA
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
aravlitraders2012
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
VictorXUQGloverl
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
ravikapoorindia
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
rajat630669
 
I need help in writing the test cases of the below methods i.pdf
I need help in writing the test cases of the below methods i.pdfI need help in writing the test cases of the below methods i.pdf
I need help in writing the test cases of the below methods i.pdf
adianantsolutions
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
f3apparelsonline
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
aksahnan
 
Header file for an array-based implementation of the ADT bag. @f.pdf
 Header file for an array-based implementation of the ADT bag. @f.pdf Header file for an array-based implementation of the ADT bag. @f.pdf
Header file for an array-based implementation of the ADT bag. @f.pdf
sudhirchourasia86
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
info309708
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
arshin9
 

Similar to public class DoubleArraySeq implements Cloneable {    Priva.pdf (20)

Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
this file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdfthis file has a complete array-based MultiSet, but not the code need.pdf
this file has a complete array-based MultiSet, but not the code need.pdf
 
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdfHow do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
How do I fix it in LinkedList.javaLabProgram.javaLinkedList.jav.pdf
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
please i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdfplease i need help Im writing a program to test the merge sort alg.pdf
please i need help Im writing a program to test the merge sort alg.pdf
 
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdfHow do I fix it in LinkedList.javathis is what i didLabProgra.pdf
How do I fix it in LinkedList.javathis is what i didLabProgra.pdf
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
StackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdfStackInterface An interface for the ADT stack. Do not modif.pdf
StackInterface An interface for the ADT stack. Do not modif.pdf
 
Write a program that will test a name) method no sorting routine from.docx
 Write a program that will test a name) method no sorting routine from.docx Write a program that will test a name) method no sorting routine from.docx
Write a program that will test a name) method no sorting routine from.docx
 
Exception to indicate that Singly LinkedList is empty. .pdf
  Exception to indicate that Singly LinkedList is empty. .pdf  Exception to indicate that Singly LinkedList is empty. .pdf
Exception to indicate that Singly LinkedList is empty. .pdf
 
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
Java Foundations StackADT-java ---  - Defines the interface to a stack.docxJava Foundations StackADT-java ---  - Defines the interface to a stack.docx
Java Foundations StackADT-java --- - Defines the interface to a stack.docx
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
JAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdfJAVA A double-ended queue is a list that allows the addition and.pdf
JAVA A double-ended queue is a list that allows the addition and.pdf
 
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 PriorityQueue.cs   Jim Mischel using System; using Sy.pdf PriorityQueue.cs   Jim Mischel using System; using Sy.pdf
PriorityQueue.cs Jim Mischel using System; using Sy.pdf
 
I need help in writing the test cases of the below methods i.pdf
I need help in writing the test cases of the below methods i.pdfI need help in writing the test cases of the below methods i.pdf
I need help in writing the test cases of the below methods i.pdf
 
(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf(C++) Change the following program so that it uses a dynamic array i.pdf
(C++) Change the following program so that it uses a dynamic array i.pdf
 
So I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdfSo I have this code(StackInAllSocks) and I implemented the method but.pdf
So I have this code(StackInAllSocks) and I implemented the method but.pdf
 
Header file for an array-based implementation of the ADT bag. @f.pdf
 Header file for an array-based implementation of the ADT bag. @f.pdf Header file for an array-based implementation of the ADT bag. @f.pdf
Header file for an array-based implementation of the ADT bag. @f.pdf
 
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdfGetting StartedCreate a class called Lab8. Use the same setup for .pdf
Getting StartedCreate a class called Lab8. Use the same setup for .pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 

More from annaimobiles

Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdfStructure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
annaimobiles
 

More from annaimobiles (20)

The equation you should use is just a variation of the PV formula.pdf
 The equation you should use is just a variation of the PV formula.pdf The equation you should use is just a variation of the PV formula.pdf
The equation you should use is just a variation of the PV formula.pdf
 
True short double stranded mRNA are involved in initiating the inte.pdf
  True short double stranded mRNA are involved in initiating the inte.pdf  True short double stranded mRNA are involved in initiating the inte.pdf
True short double stranded mRNA are involved in initiating the inte.pdf
 
Yes, it would react similarly, since boric oxide .pdf
                     Yes, it would react similarly, since boric oxide .pdf                     Yes, it would react similarly, since boric oxide .pdf
Yes, it would react similarly, since boric oxide .pdf
 
spontaneous .pdf
                     spontaneous                                      .pdf                     spontaneous                                      .pdf
spontaneous .pdf
 
sol Solution A,D,E Crenation Solution B Hemol.pdf
                     sol  Solution A,D,E Crenation Solution B Hemol.pdf                     sol  Solution A,D,E Crenation Solution B Hemol.pdf
sol Solution A,D,E Crenation Solution B Hemol.pdf
 
please post the figure.... .pdf
                     please post the figure....                       .pdf                     please post the figure....                       .pdf
please post the figure.... .pdf
 
Intermediate species means species that have 2 ch.pdf
                     Intermediate species means species that have 2 ch.pdf                     Intermediate species means species that have 2 ch.pdf
Intermediate species means species that have 2 ch.pdf
 
In primitive cells atoms are present at the corne.pdf
                     In primitive cells atoms are present at the corne.pdf                     In primitive cells atoms are present at the corne.pdf
In primitive cells atoms are present at the corne.pdf
 
Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf                     Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
Hg2+ is more water soluble, methyl mercury HgCH3+.pdf
 
HCl is a polar-covalent compound and also Acetoni.pdf
                     HCl is a polar-covalent compound and also Acetoni.pdf                     HCl is a polar-covalent compound and also Acetoni.pdf
HCl is a polar-covalent compound and also Acetoni.pdf
 
for a compound to be soluble in water 1. it shou.pdf
                     for a compound to be soluble in water  1. it shou.pdf                     for a compound to be soluble in water  1. it shou.pdf
for a compound to be soluble in water 1. it shou.pdf
 
e) II and III have the loest, equal stability .pdf
                     e) II and III have the loest, equal stability .pdf                     e) II and III have the loest, equal stability .pdf
e) II and III have the loest, equal stability .pdf
 
wait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfwait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdf
 
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdfTRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
TRUE The Reynolds number of falling 2.5 micrometer flyash particles .pdf
 
the electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdfthe electron rich molecule is a central Xe with 4 F singlebonded.pdf
the electron rich molecule is a central Xe with 4 F singlebonded.pdf
 
The given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdfThe given formulae themselves are explicit. Explicit means direct i..pdf
The given formulae themselves are explicit. Explicit means direct i..pdf
 
The answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdfThe answer is Meiosis, It occurs in human body at reporductive organ.pdf
The answer is Meiosis, It occurs in human body at reporductive organ.pdf
 
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdfStructure of HemoglobinHemoglobin is a chromo protein and is found.pdf
Structure of HemoglobinHemoglobin is a chromo protein and is found.pdf
 
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdfSolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
SolutionSelection Sort after two iterations1 14 8 9 5 16 2 1.pdf
 
Starch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdfStarch agar consists of starch. The secretion of amylase enzyme by s.pdf
Starch agar consists of starch. The secretion of amylase enzyme by s.pdf
 

Recently uploaded

Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
Elizabeth Walsh
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
Peter Brusilovsky
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 

Recently uploaded (20)

dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
Orientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdfOrientation Canvas Course Presentation.pdf
Orientation Canvas Course Presentation.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptxMichaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
Michaelis Menten Equation and Estimation Of Vmax and Tmax.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 

public class DoubleArraySeq implements Cloneable {    Priva.pdf

  • 1. public class DoubleArraySeq implements Cloneable { // Private Instance Variables private double[] data; private int manyItems; private int currentIndex; //Constructor Methods /** * Initialize an empty sequence with an initial capacity of 10. **/ public DoubleArraySeq() { try{ //Set a default capacity for new DoubleArraySeq's. int INITAL_CAPACITY = 10; //Set each instance variable to its initial value. data = new double[INITAL_CAPACITY]; manyItems = 0; currentIndex = 0; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory to create a new sequence!"); }//end catch }//end DoubleArraySeq() method /** * Initialize an empty sequence with a specified initial capacity. Note that the addAfter and addBefore methods work * efficiently (without needing more memory) until this capacity is reached. **/
  • 2. public DoubleArraySeq(int initialCapacity) { try{ //Set each instance variable to its initial value. data = new double[initialCapacity]; currentIndex = 0; manyItems = 0; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory to create a new sequence of capacity " + initialCapacity + "!"); }//end catch }//end DoubleArraySeq(int initialCapacity) method // Accessor Methods /** **/ public boolean isCurrent() { return (currentIndex < manyItems); }//end isCurrent() method /** * Accessor method to get the current element of this sequence. **/ public double getCurrent() { //Confirm that there is a current element first. if (isCurrent()) return data[currentIndex]; else throw new IllegalStateException("There is no current element! Please specify a current element first."); }//end getCurrent() method
  • 3. /** * Accessor method to get the current capacity of this sequence. **/ public int getCapacity() { //Returns the number of indexes in the array. return data.length; }//end getCapacity() method /** * Accessor method to get the available capacity (number of empty indexes) of this sequence. * The available capacity (number of empty indexes) of this sequence. **/ public int getAvailCapacity() { //Returns the number of empty indexes in the array. return data.length - manyItems; }//end getAvailCapacity() method /** **/ public int size() { //Returns the number of elements in the sequence. return manyItems; }//end size() method // Setter Methods /** * A method to move forward, so the current element is now the next element in this sequence. **/ public void advance() { if (isCurrent()) currentIndex++; else
  • 4. throw new IllegalStateException ("There is no current element! Advance may not be called."); }//end advance() method /** * A method to set the current element at the front of this sequence. **/ public void start() { if (manyItems > 0) currentIndex = 0; else throw new IllegalStateException("This sequence is empty!"); }//end start() method /** * A method that makes the last element of the sequence the current element. **/ public void setCurrentLast() { if (manyItems > 0) currentIndex = manyItems - 1; else throw new IllegalStateException("This sequence is empty!"); }//end setCurrentLast() method /** **/ public double setCurrent(int n) { //'n' must range from 1 to manyItems and the DoubleArraySeq may not be empty. if (manyItems > 0 && n > 0 && n <= manyItems){ currentIndex = n-1; return data[currentIndex]; }//end if else
  • 5. throw new IllegalStateException ("This sequence is either empty or 'n' is greater than the sequence size (or less than 1)!"); }//end setCurrent(int n) method /** * A method that and makes the selected element the current element and returns what nth number of the sequence that element is. **/ public int getElement(double element) { //Verify that the sequence is not empty. if (manyItems < 1) throw new IllegalStateException ("This sequence is empty!"); //Search for the element in the sequence and return what nth number of the sequence that element is, if found. int i; for (i = 0; i < manyItems; i++){ if (data[i] == element){ currentIndex = i; return currentIndex + 1; }//end if }//end for if (i == manyItems) throw new IllegalStateException ("This sequence does not contain the element " + element + "!"); else return 0; }//end getElement(double element) method // Size Management Methods /** * A method to change the current capacity of this sequence. **/ public void ensureCapacity(int minimumCapacity) {
  • 6. try{ if (getCapacity() < minimumCapacity){ //Create a new array of size minimumCapacity. double[] expandData = new double[minimumCapacity]; //Copy all elements from data into the new array. System.arraycopy(data, 0, expandData, 0, manyItems); //Change data's reference to expandData. data = expandData; }//end if }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("This sequence capacity is too large! There is not enough memory to store a sequence of capacity " + minimumCapacity + "! " + "Note: The add methods double the sequence capacity if maximum capacity has already been reached. " + "If necessary, try manually ensuring a smaller capacity before adding more elements to this sequence."); }//end catch }//end ensureCapacity(int minimumCapacity) method /** * A method to reduce the current capacity of this sequence to its actual size (i.e., the number of elements it contains). **/ public void trimToSize() { try{ if (data.length > manyItems){ //Create a new double[] of size manyItems that data will point to after running this method. double[] trimmedArray = new double[manyItems]; //Copy the information from data to trimmedArray. Then assign data to trimmedArray. System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; }//end if }//end try
  • 7. catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory left to alter the capacity of this sequence!"); }//end catch }//end trimToSize() method // Add Element Methods /** * A method to add a new element to this sequence, after the current element. **/ public void addAfter(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); if (isCurrent()){ //Move all elements up an index, beginning at the end of the DoubleArraySeq and ending at the index after currentIndex. for (int i = manyItems; i > (currentIndex + 1); i--) data[i] = data[i-1]; //Add the new element after the current element. currentIndex++; data[currentIndex] = element; manyItems++; }//end if else{ //If there is no current element, add the element to the end of the sequence. currentIndex = manyItems; data[currentIndex] = element; manyItems++; }//end else }//end addAfter(double element) method /** * A method to add a new element to this sequence, before the current element.
  • 8. * An attempt to increase the capacity beyond Integer.MAX_VALUE will cause the sequence to fail with an arithmetic overflow. **/ public void addBefore(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); if (isCurrent()){ //Move all elements up an index, beginning at the end of the DoubleArraySeq and ending at currentIndex. for (int i = manyItems; i > currentIndex; i--) data[i] = data[i-1]; //Add the new element before the current element (in the current element's old index). data[currentIndex] = element; manyItems ++; }//end if else{ //currentIndex is beyond the last element or the DoubleArraySeq is empty. //Move all elements in the sequence up an index (only if currentIndex is beyond the last element). for (int i = manyItems; i > 0; i--) data[i] = data[i-1]; //Add the new element to the beginning of the sequence. currentIndex = 0; data[currentIndex] = element; manyItems ++; }//end else }//end addBefore(double element) method /** * A method to add a new element at the front of the sequence and make it the current element. **/ public void addFront(double element) { //Make sure there is enough capacity to add another element.
  • 9. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); //Move all elements in the sequence up an index. for (int i = manyItems; i > 0; i--) data[i] = data[i-1]; //Add the new element to the beginning of the sequence. currentIndex = 0; data[currentIndex] = element; manyItems ++; }//end addFront(double element) method /** * A method to add a new element at the end of the sequence and make it the current element. **/ public void addEnd(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); //Add the new element to the end of the sequence. data[manyItems] = element; currentIndex = manyItems; manyItems++; }//end addEnd(double element) method /** * A method to place the contents of another sequence at the end of this sequence. * An attempt to increase the capacity beyond Integer.MAX_VALUE will cause an arithmetic overflow that will cause the sequence to fail. **/ public void addAll(DoubleArraySeq addend) { //Make sure there is enough capacity to add the other DoubleArraySeq. ensureCapacity(manyItems + addend.manyItems);
  • 10. //Copy the addend sequence to the end of the invoked sequence. System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems); manyItems += addend.manyItems; }//end addAll(DoubleArraySeq addend) method /** * A method to create a new sequence that contains all the elements from one sequence followed by another. **/ public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2) { try{ //Create a new DoubleArraySeq large enough to store the s1 and s2 DoubleArraySeq's. DoubleArraySeq newSequence = new DoubleArraySeq(s1.manyItems + s2.manyItems); //Copy DoubleArraySeq s1 and s2 to the new DoubleArraySeq, newSequence. System.arraycopy(s1.data, 0, newSequence.data, 0, s1.manyItems); System.arraycopy(s2.data, 0, newSequence.data, s1.manyItems, s2.manyItems); newSequence.manyItems = (s1.manyItems + s2.manyItems); newSequence.currentIndex = newSequence.manyItems; return newSequence; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("The sequences are too large! There is not enough memory to concatenate these sequences!"); }//end catch }//end concatenation(DoubleArraySeq s1, DoubleArraySeq s2) method // Remove Element Methods /** * A method to remove the current element from this sequence. * Indicates that there is no current element, so removeCurrent may not be called. **/ public void removeCurrent()
  • 11. { if (isCurrent()){ //Move each element down an index, starting with the element after the currentIndex. for (int i = currentIndex; i < manyItems; i++){ data[i] = data[i + 1]; }//end for loop manyItems--; }//end if else throw new IllegalStateException ("There is no current element!"); }//end removeCurrent() method /** * A method to remove the first element of the sequence. * Indicates that the sequence is empty. **/ public void removeFront() { if (manyItems > 0){ currentIndex = 0; removeCurrent(); }//end if else throw new IllegalStateException ("The sequence is empty!"); }//end removeFront() method // Overridden Java Methods -- clone(), equals(Object obj), toString() /** * A method to generate an independent copy (clone) of this sequence. * Indicates insufficient memory for creating the clone. **/ public DoubleArraySeq clone() { //Create a new DoubleArraySeq that will be returned as the clone of the invoked DoubleArraySeq.
  • 12. DoubleArraySeq answer; try{ //Clone the instance variables of the invoked DoubleArraySeq and assign them to the answer DoubleArraySeq. answer = (DoubleArraySeq) super.clone( ); }//end try catch (CloneNotSupportedException e){ // This exception should not occur. But if it does, it would probably indicate a programming error that made super.clone unavailable. // The most common error would be forgetting the "Implements Cloneable" clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable"); }//end catch catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory available to clone this sequence!"); }//end catch //Copy the information in the data instance variable (double[]) from the invoked DoubleArraySeq to its clone. answer.data = data.clone( ); return answer; }//end DoubleArraySeq clone() method /** * A method to compare two DoubleArraySeq objects and determine if they are equivalent. **/ public boolean equals(Object obj) { boolean areEqual = false; //Verify 1) That obj is a DoubleArraySeq. if (obj instanceof DoubleArraySeq){ DoubleArraySeq candidate = (DoubleArraySeq) obj; //Verify 2) That candidate has the same number of elements as the invoked DoubleArraySeq.
  • 13. if (this.manyItems == candidate.manyItems){ //Verify 3) That the elements in candidate and the invoked DoubleArraySeq are the same elements, in the same order. boolean isEqual = true; for (int i = 0; i < manyItems && isEqual; i++){ if (this.data[i] != candidate.data[i]) isEqual = false; }//end for loop if (isEqual) areEqual = true; }//end if }//end if return areEqual; }//end equals(Object obj) /** * A method to print all elements of the sequence in order, separated by a space. **/ public String toString() { //Make a String containing all the elements of this sequence in order. StringBuilder dataString = new StringBuilder(""); if (manyItems > 0){ for(int i = 0; i < manyItems; i++) dataString.append(data[i] + " "); }//end if else throw new IllegalStateException ("There is nothing in this sequence!"); return dataString.toString(); }//end toString() method }//end DoubleArraySeq class Solution public class DoubleArraySeq implements Cloneable
  • 14. { // Private Instance Variables private double[] data; private int manyItems; private int currentIndex; //Constructor Methods /** * Initialize an empty sequence with an initial capacity of 10. **/ public DoubleArraySeq() { try{ //Set a default capacity for new DoubleArraySeq's. int INITAL_CAPACITY = 10; //Set each instance variable to its initial value. data = new double[INITAL_CAPACITY]; manyItems = 0; currentIndex = 0; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory to create a new sequence!"); }//end catch }//end DoubleArraySeq() method /** * Initialize an empty sequence with a specified initial capacity. Note that the addAfter and addBefore methods work * efficiently (without needing more memory) until this capacity is reached. **/ public DoubleArraySeq(int initialCapacity) {
  • 15. try{ //Set each instance variable to its initial value. data = new double[initialCapacity]; currentIndex = 0; manyItems = 0; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory to create a new sequence of capacity " + initialCapacity + "!"); }//end catch }//end DoubleArraySeq(int initialCapacity) method // Accessor Methods /** **/ public boolean isCurrent() { return (currentIndex < manyItems); }//end isCurrent() method /** * Accessor method to get the current element of this sequence. **/ public double getCurrent() { //Confirm that there is a current element first. if (isCurrent()) return data[currentIndex]; else throw new IllegalStateException("There is no current element! Please specify a current element first."); }//end getCurrent() method /** * Accessor method to get the current capacity of this sequence.
  • 16. **/ public int getCapacity() { //Returns the number of indexes in the array. return data.length; }//end getCapacity() method /** * Accessor method to get the available capacity (number of empty indexes) of this sequence. * The available capacity (number of empty indexes) of this sequence. **/ public int getAvailCapacity() { //Returns the number of empty indexes in the array. return data.length - manyItems; }//end getAvailCapacity() method /** **/ public int size() { //Returns the number of elements in the sequence. return manyItems; }//end size() method // Setter Methods /** * A method to move forward, so the current element is now the next element in this sequence. **/ public void advance() { if (isCurrent()) currentIndex++; else throw new IllegalStateException ("There is no current element! Advance may not be called.");
  • 17. }//end advance() method /** * A method to set the current element at the front of this sequence. **/ public void start() { if (manyItems > 0) currentIndex = 0; else throw new IllegalStateException("This sequence is empty!"); }//end start() method /** * A method that makes the last element of the sequence the current element. **/ public void setCurrentLast() { if (manyItems > 0) currentIndex = manyItems - 1; else throw new IllegalStateException("This sequence is empty!"); }//end setCurrentLast() method /** **/ public double setCurrent(int n) { //'n' must range from 1 to manyItems and the DoubleArraySeq may not be empty. if (manyItems > 0 && n > 0 && n <= manyItems){ currentIndex = n-1; return data[currentIndex]; }//end if else throw new IllegalStateException ("This sequence is either empty or 'n' is greater than the sequence size (or less than 1)!");
  • 18. }//end setCurrent(int n) method /** * A method that and makes the selected element the current element and returns what nth number of the sequence that element is. **/ public int getElement(double element) { //Verify that the sequence is not empty. if (manyItems < 1) throw new IllegalStateException ("This sequence is empty!"); //Search for the element in the sequence and return what nth number of the sequence that element is, if found. int i; for (i = 0; i < manyItems; i++){ if (data[i] == element){ currentIndex = i; return currentIndex + 1; }//end if }//end for if (i == manyItems) throw new IllegalStateException ("This sequence does not contain the element " + element + "!"); else return 0; }//end getElement(double element) method // Size Management Methods /** * A method to change the current capacity of this sequence. **/ public void ensureCapacity(int minimumCapacity) { try{ if (getCapacity() < minimumCapacity){
  • 19. //Create a new array of size minimumCapacity. double[] expandData = new double[minimumCapacity]; //Copy all elements from data into the new array. System.arraycopy(data, 0, expandData, 0, manyItems); //Change data's reference to expandData. data = expandData; }//end if }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("This sequence capacity is too large! There is not enough memory to store a sequence of capacity " + minimumCapacity + "! " + "Note: The add methods double the sequence capacity if maximum capacity has already been reached. " + "If necessary, try manually ensuring a smaller capacity before adding more elements to this sequence."); }//end catch }//end ensureCapacity(int minimumCapacity) method /** * A method to reduce the current capacity of this sequence to its actual size (i.e., the number of elements it contains). **/ public void trimToSize() { try{ if (data.length > manyItems){ //Create a new double[] of size manyItems that data will point to after running this method. double[] trimmedArray = new double[manyItems]; //Copy the information from data to trimmedArray. Then assign data to trimmedArray. System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; }//end if }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory left to alter the capacity
  • 20. of this sequence!"); }//end catch }//end trimToSize() method // Add Element Methods /** * A method to add a new element to this sequence, after the current element. **/ public void addAfter(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); if (isCurrent()){ //Move all elements up an index, beginning at the end of the DoubleArraySeq and ending at the index after currentIndex. for (int i = manyItems; i > (currentIndex + 1); i--) data[i] = data[i-1]; //Add the new element after the current element. currentIndex++; data[currentIndex] = element; manyItems++; }//end if else{ //If there is no current element, add the element to the end of the sequence. currentIndex = manyItems; data[currentIndex] = element; manyItems++; }//end else }//end addAfter(double element) method /** * A method to add a new element to this sequence, before the current element. * An attempt to increase the capacity beyond Integer.MAX_VALUE will cause the sequence to fail with an arithmetic overflow.
  • 21. **/ public void addBefore(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); if (isCurrent()){ //Move all elements up an index, beginning at the end of the DoubleArraySeq and ending at currentIndex. for (int i = manyItems; i > currentIndex; i--) data[i] = data[i-1]; //Add the new element before the current element (in the current element's old index). data[currentIndex] = element; manyItems ++; }//end if else{ //currentIndex is beyond the last element or the DoubleArraySeq is empty. //Move all elements in the sequence up an index (only if currentIndex is beyond the last element). for (int i = manyItems; i > 0; i--) data[i] = data[i-1]; //Add the new element to the beginning of the sequence. currentIndex = 0; data[currentIndex] = element; manyItems ++; }//end else }//end addBefore(double element) method /** * A method to add a new element at the front of the sequence and make it the current element. **/ public void addFront(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1);
  • 22. //Move all elements in the sequence up an index. for (int i = manyItems; i > 0; i--) data[i] = data[i-1]; //Add the new element to the beginning of the sequence. currentIndex = 0; data[currentIndex] = element; manyItems ++; }//end addFront(double element) method /** * A method to add a new element at the end of the sequence and make it the current element. **/ public void addEnd(double element) { //Make sure there is enough capacity to add another element. if (data.length == manyItems) ensureCapacity(manyItems*2 + 1); //Add the new element to the end of the sequence. data[manyItems] = element; currentIndex = manyItems; manyItems++; }//end addEnd(double element) method /** * A method to place the contents of another sequence at the end of this sequence. * An attempt to increase the capacity beyond Integer.MAX_VALUE will cause an arithmetic overflow that will cause the sequence to fail. **/ public void addAll(DoubleArraySeq addend) { //Make sure there is enough capacity to add the other DoubleArraySeq. ensureCapacity(manyItems + addend.manyItems); //Copy the addend sequence to the end of the invoked sequence.
  • 23. System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems); manyItems += addend.manyItems; }//end addAll(DoubleArraySeq addend) method /** * A method to create a new sequence that contains all the elements from one sequence followed by another. **/ public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2) { try{ //Create a new DoubleArraySeq large enough to store the s1 and s2 DoubleArraySeq's. DoubleArraySeq newSequence = new DoubleArraySeq(s1.manyItems + s2.manyItems); //Copy DoubleArraySeq s1 and s2 to the new DoubleArraySeq, newSequence. System.arraycopy(s1.data, 0, newSequence.data, 0, s1.manyItems); System.arraycopy(s2.data, 0, newSequence.data, s1.manyItems, s2.manyItems); newSequence.manyItems = (s1.manyItems + s2.manyItems); newSequence.currentIndex = newSequence.manyItems; return newSequence; }//end try catch (OutOfMemoryError e){ throw new OutOfMemoryError ("The sequences are too large! There is not enough memory to concatenate these sequences!"); }//end catch }//end concatenation(DoubleArraySeq s1, DoubleArraySeq s2) method // Remove Element Methods /** * A method to remove the current element from this sequence. * Indicates that there is no current element, so removeCurrent may not be called. **/ public void removeCurrent() { if (isCurrent()){
  • 24. //Move each element down an index, starting with the element after the currentIndex. for (int i = currentIndex; i < manyItems; i++){ data[i] = data[i + 1]; }//end for loop manyItems--; }//end if else throw new IllegalStateException ("There is no current element!"); }//end removeCurrent() method /** * A method to remove the first element of the sequence. * Indicates that the sequence is empty. **/ public void removeFront() { if (manyItems > 0){ currentIndex = 0; removeCurrent(); }//end if else throw new IllegalStateException ("The sequence is empty!"); }//end removeFront() method // Overridden Java Methods -- clone(), equals(Object obj), toString() /** * A method to generate an independent copy (clone) of this sequence. * Indicates insufficient memory for creating the clone. **/ public DoubleArraySeq clone() { //Create a new DoubleArraySeq that will be returned as the clone of the invoked DoubleArraySeq. DoubleArraySeq answer;
  • 25. try{ //Clone the instance variables of the invoked DoubleArraySeq and assign them to the answer DoubleArraySeq. answer = (DoubleArraySeq) super.clone( ); }//end try catch (CloneNotSupportedException e){ // This exception should not occur. But if it does, it would probably indicate a programming error that made super.clone unavailable. // The most common error would be forgetting the "Implements Cloneable" clause at the start of this class. throw new RuntimeException ("This class does not implement Cloneable"); }//end catch catch (OutOfMemoryError e){ throw new OutOfMemoryError ("There is not enough memory available to clone this sequence!"); }//end catch //Copy the information in the data instance variable (double[]) from the invoked DoubleArraySeq to its clone. answer.data = data.clone( ); return answer; }//end DoubleArraySeq clone() method /** * A method to compare two DoubleArraySeq objects and determine if they are equivalent. **/ public boolean equals(Object obj) { boolean areEqual = false; //Verify 1) That obj is a DoubleArraySeq. if (obj instanceof DoubleArraySeq){ DoubleArraySeq candidate = (DoubleArraySeq) obj; //Verify 2) That candidate has the same number of elements as the invoked DoubleArraySeq. if (this.manyItems == candidate.manyItems){ //Verify 3) That the elements in candidate and the invoked DoubleArraySeq are the
  • 26. same elements, in the same order. boolean isEqual = true; for (int i = 0; i < manyItems && isEqual; i++){ if (this.data[i] != candidate.data[i]) isEqual = false; }//end for loop if (isEqual) areEqual = true; }//end if }//end if return areEqual; }//end equals(Object obj) /** * A method to print all elements of the sequence in order, separated by a space. **/ public String toString() { //Make a String containing all the elements of this sequence in order. StringBuilder dataString = new StringBuilder(""); if (manyItems > 0){ for(int i = 0; i < manyItems; i++) dataString.append(data[i] + " "); }//end if else throw new IllegalStateException ("There is nothing in this sequence!"); return dataString.toString(); }//end toString() method }//end DoubleArraySeq class