Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am having trouble with the addAfter, addBefore, addAll, removecurrent, Start, and equals method.they wont pass the test.the test class is too large to post

I am having trouble with the addAfter, addBefore, addAll, removecurrent, Start, and equals method.they wont pass the test.the test class is too large to post here

package solution; public class DoubleArraySeq implements Cloneable { public final static int DEFAULT_CAPACITY = 10

private double[] data; private int manyItems; private int currentIndex; public DoubleArraySeq() throws OutOfMemoryError { try { int INITAL_CAPACITY = 10; data = new double[INITAL_CAPACITY]; manyItems = 0; currentIndex = 0; } catch (OutOfMemoryError e) { throw new OutOfMemoryError ("There is not enough memory to create a new sequence!"); } } public DoubleArraySeq(int initialCapacity) throws OutOfMemoryError { // TODO try { data = new double[initialCapacity]; currentIndex = 0; manyItems = 0; } catch (OutOfMemoryError e) { throw new OutOfMemoryError ("There is not enough memory to create a new sequence of capacity " + initialCapacity + "!"); } } public void addAfter(double element) throws OutOfMemoryError { if (data.length == manyItems) ensureCapacity(manyItems*2 + 1);

if (isCurrent()) { for (int i = manyItems; i > (currentIndex + 1); i--) data[i] = data[i-1]; currentIndex++; data[currentIndex] = element; manyItems++; } else { currentIndex = manyItems; data[currentIndex] = element; manyItems++; } } public void addBefore(double element) throws OutOfMemoryError { // TODO if (data.length == manyItems) ensureCapacity(manyItems*2 + 1);

if (isCurrent()) { for (int i = manyItems; i > currentIndex; i--) data[i] = data[i-1]; data[currentIndex] = element; manyItems ++; } else { for (int i = manyItems; i > 0; i--) data[i] = data[i-1]; currentIndex = 0; data[currentIndex] = element; manyItems ++; } } public void addAll(DoubleArraySeq addend) throws NullPointerException, OutOfMemoryError { // TODO if(addend == null) { throw new NullPointerException("addend Array is Null Point."); } this.ensureCapacity(manyItems + addend.manyItems); System.arraycopy(addend.data, 0, this.data, this.manyItems, addend.manyItems); manyItems += addend.manyItems; } public void advance() throws IllegalStateException { // TODO if (isCurrent()) currentIndex++; else throw new IllegalStateException ("There is no current element! Advance may not be called."); } @Override public DoubleArraySeq clone() throws OutOfMemoryError, RuntimeException { // TODO: change this. DoubleArraySeq answer; try { answer = (DoubleArraySeq) super.clone( ); } catch (CloneNotSupportedException e) { throw new RuntimeException ("This class does not implement Cloneable"); } catch (OutOfMemoryError e) { throw new OutOfMemoryError ("There is not enough memory available to clone this sequence!"); } answer.data = data.clone( ); return answer; } public static DoubleArraySeq concatenation(DoubleArraySeq s1, DoubleArraySeq s2) throws NullPointerException, OutOfMemoryError { // TODO: change this. try { DoubleArraySeq newSequence = new DoubleArraySeq(s1.manyItems + s2.manyItems); 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; } catch (OutOfMemoryError e) { throw new OutOfMemoryError ("The sequences are too large! There is not enough memory to concatenate these sequences!"); } } public void ensureCapacity(int minimumCapacity) throws OutOfMemoryError { try{ if (getCapacity() < minimumCapacity){ double[] expandData = new double[minimumCapacity]; System.arraycopy(data, 0, expandData, 0, manyItems); data = expandData; } } catch (OutOfMemoryError e){ throw new OutOfMemoryError ("This sequence capacity is too large! There is not enough memory to store a sequence of capacity " + minimumCapacity + "! "); } } public int getCapacity() { // TODO: change this return data.length; } public double getCurrent() throws IllegalStateException { // TODO: change this if (isCurrent()) return data[currentIndex]; else throw new IllegalStateException ("There is no current element! Please specify a current element first."); } public boolean isCurrent() { // TODO return (currentIndex < manyItems); } public void removeCurrent() throws IllegalStateException { if(manyItems - 1 < currentIndex) { throw new IllegalStateException("There is no current element."); } for(int i = currentIndex; i < manyItems; i++) { try { data[i] = data[i + 1]; } catch(ArrayIndexOutOfBoundsException e) { } } data[manyItems-- - 1] = 0; } public int size() { // TODO: change this. return manyItems; }

public void start() { if (manyItems > 0) currentIndex = 0; else throw new IllegalStateException("This sequence is empty!"); }

public void trimToSize() throws OutOfMemoryError { // TODO try { if (data.length > manyItems) { double[] trimmedArray = new double[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; } } catch (OutOfMemoryError e) { throw new OutOfMemoryError ("There is not enough memory left to alter the capacity of this sequence!"); } }

@Override public String toString() { // TODO: change this. StringBuilder dataString = new StringBuilder(""); if (manyItems > 0) { for(int i = 0; i < manyItems; i++) dataString.append(data[i] + " "); } else throw new IllegalStateException ("There is nothing in this sequence!"); return dataString.toString(); }

@Override public boolean equals(Object other) { // TODO: change this. boolean areEqual = false; if (other instanceof DoubleArraySeq){ DoubleArraySeq candidate = (DoubleArraySeq) other; if (this.manyItems == candidate.manyItems){ boolean isEqual = true; for (int i = 0; i < manyItems && isEqual; i++){ if (this.data[i] != candidate.data[i]) isEqual = false; } if (isEqual) areEqual = true; } } return areEqual; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Secrets Of Analytical Leaders Insights From Information Insiders

Authors: Wayne Eckerson

1st Edition

1935504347, 9781935504344

More Books

Students also viewed these Databases questions

Question

What must a creditor do to become a secured party?

Answered: 1 week ago

Question

When should the last word in a title be capitalized?

Answered: 1 week ago