SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler 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

Mais conteúdo relacionado

Semelhante a 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 .pdfsiennatimbok52331
 
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.pdfflashfashioncasualwe
 
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.pdfmail931892
 
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.pdffirstchoiceajmer
 
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.pdfezonesolutions
 
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.pdfmail931892
 
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.pdfkostikjaylonshaewe47
 
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.pdfARCHANASTOREKOTA
 
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.docxajoy21
 
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. .pdfaravlitraders2012
 
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.docxVictorXUQGloverl
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
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.pdfamrishinda
 
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.pdfrajat630669
 
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.pdfadianantsolutions
 
(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.pdff3apparelsonline
 
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.pdfaksahnan
 
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.pdfsudhirchourasia86
 
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 .pdfinfo309708
 
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 .pdfarshin9
 

Semelhante a 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
 

Mais de annaimobiles

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.pdfannaimobiles
 
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.pdfannaimobiles
 
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 .pdfannaimobiles
 
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.pdfannaimobiles
 
please post the figure.... .pdf
                     please post the figure....                       .pdf                     please post the figure....                       .pdf
please post the figure.... .pdfannaimobiles
 
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.pdfannaimobiles
 
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.pdfannaimobiles
 
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+.pdfannaimobiles
 
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.pdfannaimobiles
 
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.pdfannaimobiles
 
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 .pdfannaimobiles
 
wait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfwait doingSolutionwait doing.pdf
wait doingSolutionwait doing.pdfannaimobiles
 
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 .pdfannaimobiles
 
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.pdfannaimobiles
 
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..pdfannaimobiles
 
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.pdfannaimobiles
 
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.pdfannaimobiles
 
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.pdfannaimobiles
 
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.pdfannaimobiles
 

Mais de 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
 

Último

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
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)Jisc
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 

Último (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
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)
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 

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