Answer:
Note: Driver class is not given to test the DoubleArraySeq.java
Modified Code:
public class DoubleArraySeq implements Cloneable
{
// Invariant of the DoubleArraySeq class:
// 1. The number of elements in the sequences is in the instance variable
// manyItems.
// 2. For an empty sequence (with no elements), we do not care what is
// stored in any of data; for a non-empty sequence, the elements of the
// sequence are stored in data[0] through data[manyItems-1], and we
// don care what in the rest of data.
// 3. If there is a current element, then it lies in data[currentIndex];
// if there is no current element, then currentIndex equals manyItems.
private double[ ] data;
private int manyItems;
private int currentIndex;
/**
* Initialize an empty sequence with an initial capacity of 10. Note that
* the addAfter and addBefore methods work
* efficiently (without needing more memory) until this capacity is reached.
* @param - none
* @postcondition
* This sequence is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[10].
**/
public DoubleArraySeq( )
{
//Set manyItems to 0.
manyItems=0;
//Initialize data
data=new double[10];
}
/**
* 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.
* @param initialCapacity
* the initial capacity of this sequence
* @precondition
* initialCapacity is non-negative.
* @postcondition
* This sequence is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[initialCapacity].
**/
public DoubleArraySeq(int initialCapacity)
{
//Check initialCapacity. If it is <0, throw EXCEPTION
if(initialCapacity < 0)
//Throw EXCEPTION
throw new IllegalArgumentException (\"initialCapacity IS NEGATIVE:\");
//Set manyItems to 0.
manyItems = 0;
try
{
data = new double[initialCapacity];
}
catch(OutOfMemoryError ea)
{
throw new RuntimeException
(\"NOT ENOUGH MEMORY\");
}
}
/**
* Add a new element to this sequence, after the current element.
* If the new element would take this sequence beyond its current capacity,
* then the capacity is increased before adding the new element.
* @param element
* the new element that is being added
* @postcondition
* A new copy of the element has been added to this sequence. If there was
* a current element, then the new element is placed after the current
* element. If there was no current element, then the new element is placed
* at the end of the sequence. In all cases, the new element becomes the
* new current element of this sequence.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the sequence\'s capacity.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause the sequence to fail with an.
AnswerNote Driver class is not given to test the DoubleArraySeq..pdf
1. Answer:
Note: Driver class is not given to test the DoubleArraySeq.java
Modified Code:
public class DoubleArraySeq implements Cloneable
{
// Invariant of the DoubleArraySeq class:
// 1. The number of elements in the sequences is in the instance variable
// manyItems.
// 2. For an empty sequence (with no elements), we do not care what is
// stored in any of data; for a non-empty sequence, the elements of the
// sequence are stored in data[0] through data[manyItems-1], and we
// don care what in the rest of data.
// 3. If there is a current element, then it lies in data[currentIndex];
// if there is no current element, then currentIndex equals manyItems.
private double[ ] data;
private int manyItems;
private int currentIndex;
/**
* Initialize an empty sequence with an initial capacity of 10. Note that
* the addAfter and addBefore methods work
* efficiently (without needing more memory) until this capacity is reached.
* @param - none
* @postcondition
* This sequence is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[10].
**/
public DoubleArraySeq( )
{
//Set manyItems to 0.
manyItems=0;
//Initialize data
data=new double[10];
2. }
/**
* 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.
* @param initialCapacity
* the initial capacity of this sequence
* @precondition
* initialCapacity is non-negative.
* @postcondition
* This sequence is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[initialCapacity].
**/
public DoubleArraySeq(int initialCapacity)
{
//Check initialCapacity. If it is <0, throw EXCEPTION
if(initialCapacity < 0)
//Throw EXCEPTION
throw new IllegalArgumentException ("initialCapacity IS NEGATIVE:");
//Set manyItems to 0.
manyItems = 0;
try
{
data = new double[initialCapacity];
}
catch(OutOfMemoryError ea)
{
throw new RuntimeException
("NOT ENOUGH MEMORY");
}
}
3. /**
* Add a new element to this sequence, after the current element.
* If the new element would take this sequence beyond its current capacity,
* then the capacity is increased before adding the new element.
* @param element
* the new element that is being added
* @postcondition
* A new copy of the element has been added to this sequence. If there was
* a current element, then the new element is placed after the current
* element. If there was no current element, then the new element is placed
* at the end of the sequence. In all cases, the new element becomes the
* new current element of this sequence.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the sequence's capacity.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause the sequence to fail with an
* arithmetic overflow.
**/
public void addAfter(double element)
{
//Check the capacity. If array is full, increase its capacity
if(manyItems == data.length)
{
//Increase capacity
ensureCapacity(2*manyItems+1);
}
//If enough space is available for new element
else
{
//If current element exists
if(isCurrent())
{
//Loop to make space
4. for(int kk = manyItems ;kk > currentIndex+1 ; kk--)
{
data[kk] = data[kk-1];
}
//Insert the new element
data[currentIndex+1] = element;
//Increment the current index
currentIndex +=1;
//Increment the element count
manyItems++;
}
//If current element not present
else{
//Add element at the last
data[manyItems] = element;
//Increment the element count
manyItems++;
}
}
}
/**
* Add a new element to this sequence, before the current element.
* If the new element would take this sequence beyond its current capacity,
* then the capacity is increased before adding the new element.
* @param element
* the new element that is being added
* @postcondition
* A new copy of the element has been added to this sequence. If there was
* a current element, then the new element is placed before the current
* element. If there was no current element, then the new element is placed
* at the start of the sequence. In all cases, the new element becomes the
* new current element of this sequence.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the sequence's capacity.
* @note
* An attempt to increase the capacity beyond
5. * Integer.MAX_VALUE will cause the sequence to fail with an
* arithmetic overflow.
**/
public void addBefore(double element)
{
//Check the capacity. If array is full, increase its capacity
if(manyItems == data.length)
{
ensureCapacity(2*manyItems+1);
}
else
{
//If current element exists
if(isCurrent())
{
//Loop to make space for new element
for(int kk = manyItems ;kk > currentIndex ; kk--)
{
data[kk] = data[kk-1];
}
//Place the new element at the current index
data[currentIndex] = element;
//Increment element counts
manyItems++;
}
//If current element doesn't exist then
else
{
//Loop to move elements
for(int kk = manyItems; kk>0;kk--)
{
data[kk] = data[kk-1];
}
//Place new element at the sequence begining
data[0] = element;
//Increment element counts
6. manyItems++;
}
}
}
/**
* Place the contents of another sequence at the end of this sequence.
* @param addend
* a sequence whose contents will be placed at the end of this sequence
* @precondition
* The parameter, addend, is not null.
* @postcondition
* The elements from addend have been placed at the end of
* this sequence. The current element of this sequence remains where it
* was, and the addend is also unchanged.
* @exception NullPointerException
* Indicates that addend is null.
* @exception OutOfMemoryError
* Indicates insufficient memory to increase the size of this sequence.
* @note
* 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)
{
//Ensure the capacity
ensureCapacity(addend.manyItems +manyItems);
//Copy the addend elements
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
//Set the manyItems
manyItems = manyItems + addend.manyItems;
}
7. /**
* Move forward, so that the current element is now the next element in
* this sequence.
* @param - none
* @precondition
* isCurrent() returns true.
* @postcondition
* If the current element was already the end element of this sequence
* (with nothing after it), then there is no longer any current element.
* Otherwise, the new element is the element immediately after the
* original current element.
* @exception IllegalStateException
* Indicates that there is no current element, so
* advance may not be called.
**/
public void advance( )
{
// If current element exists.
currentIndex +=1;
}
/**
* Generate a copy of this sequence.
* @param - none
* @return
* The return value is a copy of this sequence. Subsequent changes to the
* copy will not affect the original, nor vice versa.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public DoubleArraySeq clone( )
{
// Clone a DoubleArraySeq object.
DoubleArraySeq answer;
8. try
{
answer = (DoubleArraySeq) super.clone( );
}
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");
}
answer.data = data.clone( );
return answer;
}
/**
* Create a new sequence that contains all the elements from one sequence
* followed by another.
* @param s1
* the first of two sequences
* @param s2
* the second of two sequences
* @precondition
* Neither s1 nor s2 is null.
* @return
* a new sequence that has the elements of s1 followed by the
* elements of s2 (with no current element)
* @exception NullPointerException.
* Indicates that one of the arguments is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new sequence.
* @note
9. * An attempt to create a sequence with a capacity beyond
* Integer.MAX_VALUE will cause an arithmetic overflow
* that will cause the sequence to fail.
**/
public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2)
{
//Create new DoubleArraySeq with initial capacity to hold s1 and s2 elements
DoubleArraySeq newAry = new DoubleArraySeq(s1.getCapacity()+s2.getCapacity());
//Copy s1 elements
System.arraycopy(s1.data, 0, newAry.data, 0, s1.manyItems);
//Copy s2 elements
System.arraycopy(s2.data, 0, newAry.data, s1.manyItems, s2.manyItems);
//Set manyItems of newAry
newAry.manyItems = s1.manyItems+s2.manyItems;
//Set current element of newAry
newAry.currentIndex = newAry.manyItems;
//return newAry
return newAry;
}
/**
* Change the current capacity of this sequence.
* @param minimumCapacity
* the new capacity for this sequence
* @postcondition
* This sequence's capacity has been changed to at least minimumCapacity.
* If the capacity was already at or greater than minimumCapacity,
* then the capacity is left unchanged.
* @exception OutOfMemoryError
* Indicates insufficient memory for: new int[minimumCapacity].
**/
public void ensureCapacity(int minimumCapacity)
{
//Create a new array
double[] newBigAry;
//Check array's capacity is less than minimumCapacity. //If so, set array's capacity to
minimumCapacity
10. if(data.length < minimumCapacity)
{
//Initialize new array
newBigAry = new double[minimumCapacity];
//Copy elements into new array
System.arraycopy(data, 0, newBigAry, 0, manyItems);
//Set data to new array
data = newBigAry;
}
}
/**
* Accessor method to get the current capacity of this sequence.
* The add method works efficiently (without needing
* more memory) until this capacity is reached.
* @param - none
* @return
* the current capacity of this sequence
**/
public int getCapacity( )
{
// return the capacity of array
return data.length;
}
/**
* Accessor method to get the current element of this sequence.
* @param - none
* @precondition
* isCurrent() returns true.
* @return
* the current element of this sequence
* @exception IllegalStateException
* Indicates that there is no current element, so
* getCurrent may not be called.
**/
public double getCurrent( )
11. {
//If current element exists
if(isCurrent())
//Return the current element
return data[currentIndex];
//If current element doesn't exist
else
//throw EXCEPTION
throw new IllegalArgumentException
("no CURRENT ELEMENT");
}
/**
* Accessor method to determine whether this sequence has a specified
* current element that can be retrieved with the
* getCurrent method.
* @param - none
* @return
* true (there is a current element) or false (there is no current element at the moment)
**/
public boolean isCurrent( )
{
//If current element exist
if(currentIndex != manyItems)
//Return true
return true;
//if current element doesnt exist
else{
//Return false.
return false;
}
}
/**
* Remove the current element from this sequence.
* @param - none
* @precondition
12. * isCurrent() returns true.
* @postcondition
* The current element has been removed from this sequence, and the
* following element (if there is one) is now the new current element.
* If there was no following element, then there is now no current
* element.
* @exception IllegalStateException
* Indicates that there is no current element, so
* removeCurrent may not be called.
**/
public void removeCurrent( )
{
//If current element exists
if(isCurrent())
{
//If current element is the last-element
if(currentIndex == manyItems-1)
{
//Decrease the element count
manyItems -=1;
}
//If current-element is in middle of the sequence
else
{
//Loop to delete the current element by next element
for(int kk= currentIndex;kk
Solution
Answer:
Note: Driver class is not given to test the DoubleArraySeq.java
Modified Code:
public class DoubleArraySeq implements Cloneable
{
// Invariant of the DoubleArraySeq class:
// 1. The number of elements in the sequences is in the instance variable
// manyItems.
13. // 2. For an empty sequence (with no elements), we do not care what is
// stored in any of data; for a non-empty sequence, the elements of the
// sequence are stored in data[0] through data[manyItems-1], and we
// don care what in the rest of data.
// 3. If there is a current element, then it lies in data[currentIndex];
// if there is no current element, then currentIndex equals manyItems.
private double[ ] data;
private int manyItems;
private int currentIndex;
/**
* Initialize an empty sequence with an initial capacity of 10. Note that
* the addAfter and addBefore methods work
* efficiently (without needing more memory) until this capacity is reached.
* @param - none
* @postcondition
* This sequence is empty and has an initial capacity of 10.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[10].
**/
public DoubleArraySeq( )
{
//Set manyItems to 0.
manyItems=0;
//Initialize data
data=new double[10];
}
/**
* 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.
* @param initialCapacity
* the initial capacity of this sequence
* @precondition
14. * initialCapacity is non-negative.
* @postcondition
* This sequence is empty and has the given initial capacity.
* @exception IllegalArgumentException
* Indicates that initialCapacity is negative.
* @exception OutOfMemoryError
* Indicates insufficient memory for:
* new double[initialCapacity].
**/
public DoubleArraySeq(int initialCapacity)
{
//Check initialCapacity. If it is <0, throw EXCEPTION
if(initialCapacity < 0)
//Throw EXCEPTION
throw new IllegalArgumentException ("initialCapacity IS NEGATIVE:");
//Set manyItems to 0.
manyItems = 0;
try
{
data = new double[initialCapacity];
}
catch(OutOfMemoryError ea)
{
throw new RuntimeException
("NOT ENOUGH MEMORY");
}
}
/**
* Add a new element to this sequence, after the current element.
* If the new element would take this sequence beyond its current capacity,
* then the capacity is increased before adding the new element.
* @param element
* the new element that is being added
* @postcondition
* A new copy of the element has been added to this sequence. If there was
15. * a current element, then the new element is placed after the current
* element. If there was no current element, then the new element is placed
* at the end of the sequence. In all cases, the new element becomes the
* new current element of this sequence.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the sequence's capacity.
* @note
* An attempt to increase the capacity beyond
* Integer.MAX_VALUE will cause the sequence to fail with an
* arithmetic overflow.
**/
public void addAfter(double element)
{
//Check the capacity. If array is full, increase its capacity
if(manyItems == data.length)
{
//Increase capacity
ensureCapacity(2*manyItems+1);
}
//If enough space is available for new element
else
{
//If current element exists
if(isCurrent())
{
//Loop to make space
for(int kk = manyItems ;kk > currentIndex+1 ; kk--)
{
data[kk] = data[kk-1];
}
//Insert the new element
data[currentIndex+1] = element;
//Increment the current index
currentIndex +=1;
//Increment the element count
16. manyItems++;
}
//If current element not present
else{
//Add element at the last
data[manyItems] = element;
//Increment the element count
manyItems++;
}
}
}
/**
* Add a new element to this sequence, before the current element.
* If the new element would take this sequence beyond its current capacity,
* then the capacity is increased before adding the new element.
* @param element
* the new element that is being added
* @postcondition
* A new copy of the element has been added to this sequence. If there was
* a current element, then the new element is placed before the current
* element. If there was no current element, then the new element is placed
* at the start of the sequence. In all cases, the new element becomes the
* new current element of this sequence.
* @exception OutOfMemoryError
* Indicates insufficient memory for increasing the sequence's capacity.
* @note
* 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)
{
//Check the capacity. If array is full, increase its capacity
if(manyItems == data.length)
{
ensureCapacity(2*manyItems+1);
17. }
else
{
//If current element exists
if(isCurrent())
{
//Loop to make space for new element
for(int kk = manyItems ;kk > currentIndex ; kk--)
{
data[kk] = data[kk-1];
}
//Place the new element at the current index
data[currentIndex] = element;
//Increment element counts
manyItems++;
}
//If current element doesn't exist then
else
{
//Loop to move elements
for(int kk = manyItems; kk>0;kk--)
{
data[kk] = data[kk-1];
}
//Place new element at the sequence begining
data[0] = element;
//Increment element counts
manyItems++;
}
}
}
/**
* Place the contents of another sequence at the end of this sequence.
18. * @param addend
* a sequence whose contents will be placed at the end of this sequence
* @precondition
* The parameter, addend, is not null.
* @postcondition
* The elements from addend have been placed at the end of
* this sequence. The current element of this sequence remains where it
* was, and the addend is also unchanged.
* @exception NullPointerException
* Indicates that addend is null.
* @exception OutOfMemoryError
* Indicates insufficient memory to increase the size of this sequence.
* @note
* 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)
{
//Ensure the capacity
ensureCapacity(addend.manyItems +manyItems);
//Copy the addend elements
System.arraycopy(addend.data, 0, data, manyItems, addend.manyItems);
//Set the manyItems
manyItems = manyItems + addend.manyItems;
}
/**
* Move forward, so that the current element is now the next element in
* this sequence.
* @param - none
* @precondition
* isCurrent() returns true.
* @postcondition
* If the current element was already the end element of this sequence
19. * (with nothing after it), then there is no longer any current element.
* Otherwise, the new element is the element immediately after the
* original current element.
* @exception IllegalStateException
* Indicates that there is no current element, so
* advance may not be called.
**/
public void advance( )
{
// If current element exists.
currentIndex +=1;
}
/**
* Generate a copy of this sequence.
* @param - none
* @return
* The return value is a copy of this sequence. Subsequent changes to the
* copy will not affect the original, nor vice versa.
* @exception OutOfMemoryError
* Indicates insufficient memory for creating the clone.
**/
public DoubleArraySeq clone( )
{
// Clone a DoubleArraySeq object.
DoubleArraySeq answer;
try
{
answer = (DoubleArraySeq) super.clone( );
}
catch (CloneNotSupportedException e)
{
// This exception should not occur. But if it does, it would probably
// indicate a programming error that made super.clone unavailable.
20. // 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");
}
answer.data = data.clone( );
return answer;
}
/**
* Create a new sequence that contains all the elements from one sequence
* followed by another.
* @param s1
* the first of two sequences
* @param s2
* the second of two sequences
* @precondition
* Neither s1 nor s2 is null.
* @return
* a new sequence that has the elements of s1 followed by the
* elements of s2 (with no current element)
* @exception NullPointerException.
* Indicates that one of the arguments is null.
* @exception OutOfMemoryError
* Indicates insufficient memory for the new sequence.
* @note
* An attempt to create a sequence with a capacity beyond
* Integer.MAX_VALUE will cause an arithmetic overflow
* that will cause the sequence to fail.
**/
public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2)
{
//Create new DoubleArraySeq with initial capacity to hold s1 and s2 elements
DoubleArraySeq newAry = new DoubleArraySeq(s1.getCapacity()+s2.getCapacity());
//Copy s1 elements
21. System.arraycopy(s1.data, 0, newAry.data, 0, s1.manyItems);
//Copy s2 elements
System.arraycopy(s2.data, 0, newAry.data, s1.manyItems, s2.manyItems);
//Set manyItems of newAry
newAry.manyItems = s1.manyItems+s2.manyItems;
//Set current element of newAry
newAry.currentIndex = newAry.manyItems;
//return newAry
return newAry;
}
/**
* Change the current capacity of this sequence.
* @param minimumCapacity
* the new capacity for this sequence
* @postcondition
* This sequence's capacity has been changed to at least minimumCapacity.
* If the capacity was already at or greater than minimumCapacity,
* then the capacity is left unchanged.
* @exception OutOfMemoryError
* Indicates insufficient memory for: new int[minimumCapacity].
**/
public void ensureCapacity(int minimumCapacity)
{
//Create a new array
double[] newBigAry;
//Check array's capacity is less than minimumCapacity. //If so, set array's capacity to
minimumCapacity
if(data.length < minimumCapacity)
{
//Initialize new array
newBigAry = new double[minimumCapacity];
//Copy elements into new array
System.arraycopy(data, 0, newBigAry, 0, manyItems);
//Set data to new array
data = newBigAry;
}
22. }
/**
* Accessor method to get the current capacity of this sequence.
* The add method works efficiently (without needing
* more memory) until this capacity is reached.
* @param - none
* @return
* the current capacity of this sequence
**/
public int getCapacity( )
{
// return the capacity of array
return data.length;
}
/**
* Accessor method to get the current element of this sequence.
* @param - none
* @precondition
* isCurrent() returns true.
* @return
* the current element of this sequence
* @exception IllegalStateException
* Indicates that there is no current element, so
* getCurrent may not be called.
**/
public double getCurrent( )
{
//If current element exists
if(isCurrent())
//Return the current element
return data[currentIndex];
//If current element doesn't exist
else
//throw EXCEPTION
throw new IllegalArgumentException
23. ("no CURRENT ELEMENT");
}
/**
* Accessor method to determine whether this sequence has a specified
* current element that can be retrieved with the
* getCurrent method.
* @param - none
* @return
* true (there is a current element) or false (there is no current element at the moment)
**/
public boolean isCurrent( )
{
//If current element exist
if(currentIndex != manyItems)
//Return true
return true;
//if current element doesnt exist
else{
//Return false.
return false;
}
}
/**
* Remove the current element from this sequence.
* @param - none
* @precondition
* isCurrent() returns true.
* @postcondition
* The current element has been removed from this sequence, and the
* following element (if there is one) is now the new current element.
* If there was no following element, then there is now no current
* element.
* @exception IllegalStateException
* Indicates that there is no current element, so
* removeCurrent may not be called.
24. **/
public void removeCurrent( )
{
//If current element exists
if(isCurrent())
{
//If current element is the last-element
if(currentIndex == manyItems-1)
{
//Decrease the element count
manyItems -=1;
}
//If current-element is in middle of the sequence
else
{
//Loop to delete the current element by next element
for(int kk= currentIndex;kk