Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE IN JAVA FOR REMOVECURRENT METHOD I NEED HELP WITH A METHOD AT THE BOTTOM OF THE PAGE. ITS THE removeCurrent method. It says

PLEASE CODE IN JAVA FOR REMOVECURRENT METHOD I NEED HELP WITH A METHOD AT THE BOTTOM OF THE PAGE. ITS THE removeCurrent method. It says student works here. Can you please help me ASAP THIS IS IN JAVA. // CONSTRUCTORS

/* default constructor */ public LongArraySequence() { this.used = 0; this.cursor = 0; this.data = new long[1]; }

/* parameterized constructor */ public LongArraySequence(int initialCapacity) { if (initialCapacity < 1) throw new IllegalArgumentException("initialCapacity must be > 0"); this.used = 0; this.cursor = 0; try { this.data = new long[initialCapacity]; } catch (OutOfMemoryError err) { throw new OutOfMemoryError("Could not accommodate capacity request"); } }

/* copy constructor */ public LongArraySequence(LongArraySequence other) { if (other == null) { throw new NullPointerException("other must not be null"); }

this.data = new long[other.used]; this.cursor = other.cursor; this.used = other.used; for (int index = 0; index < this.used; index++) { this.data[index] = other.data[index]; } /* or use below System.arraycopy(other.data, 0, this.data, 0, other.used); */ } }

@Override public int hashCode() {

int hashValue = Objects.hash(this.used) + Objects.hash(this.cursor);

for (int index = 0; index < this.used; ++index) { hashValue += Objects.hash(this.data[index]); } return hashValue; }

@Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("LongArraySequence: "); sb.append(this.used); sb.append(" items"); sb.append("-->[ "); if (this.used > 0) { if (this.cursor == 0) sb.append("^"); sb.append(this.data[0]); for (int index = 1; index < this.used; index++) { sb.append(", "); if (index == this.cursor) sb.append("^"); sb.append(this.data[index]); } } sb.append(" ]"); sb.append(" Capacity: "); sb.append(this.data.length);

return sb.toString(); }

public int size() { return this.used; }

public int getCapacity() { return this.data.length; }

public boolean isCurrent() { return this.cursor < this.used && this.cursor >= 0; }

public long getCurrent() { if (!this.isCurrent()) { throw new IllegalStateException("There is no current element"); } return this.data[this.cursor]; }

// *************************************************** // MUTATORS // ***************************************************

public void ensureCapacity(int minimumCapacity) { if (minimumCapacity < 1) throw new IllegalArgumentException("minimumCapacity must be >= 1");

if (this.data.length < minimumCapacity) { try { long[] biggerArray = new long[minimumCapacity]; for (int index = 0; index < this.used; index++) biggerArray[index] = this.data[index]; this.data = biggerArray; } catch (OutOfMemoryError err) { throw new OutOfMemoryError("Could not expand capacity --" + "capacity remains the same"); } } }

public void trimToSize() { if (this.used == 0) { this.data = new long[1]; this.cursor = 1; return; } if (this.used < this.data.length) { try { long[] trimmedArray = new long[this.used]; for (int index = 0; index < this.used; index++) trimmedArray[index] = this.data[index]; this.data = trimmedArray; } catch (OutOfMemoryError err) { throw new OutOfMemoryError("Not enough dynamic memory available to trim --" + " capacity not changed"); } } }

public LongArraySequence concatenation(LongArraySequence other) { if (other == null) { throw new NullPointerException("other must be non-null"); } int newCapacity = this.used + other.used; LongArraySequence newSequence = new LongArraySequence(newCapacity); newSequence.cursor = 0; newSequence.used = newCapacity;

int toIndex, fromIndex; toIndex = fromIndex = 0;

while (fromIndex < this.used) { newSequence.data[toIndex] = this.data[fromIndex]; toIndex++; fromIndex++; }

fromIndex = 0; while (fromIndex < other.used) { newSequence.data[toIndex] = other.data[fromIndex]; toIndex++; fromIndex++; }

return newSequence; }

public void start() { this.cursor = 0; }

public void advance() { if (this.cursor == this.used) throw new IllegalStateException("Attempting to advance cursor beyond end of sequence"); else this.cursor++; }

public void addBefore(long newEntry) { if (this.data.length == this.used) this.ensureCapacity(this.used * 2); if (isCurrent() && (this.cursor != 0)) { this.cursor--; this.addAfter(newEntry); } else { System.arraycopy(this.data, 0, this.data, 1, this.used); this.start(); this.data[0] = newEntry; this.used++; }

}

public void addAfter(long newEntry) { if (this.data.length == this.used) this.ensureCapacity(this.used * 2); if (isCurrent() && (this.cursor != this.used - 1)) { //there is a cursor and it is not at the end // from start to from how many System.arraycopy(this.data, this.cursor + 1, this.data, this.cursor + 2, this.used - this.cursor); this.advance(); this.data[this.cursor] = newEntry; } else { this.data[this.used] = newEntry; this.cursor = this.used; } this.used++; }

public void removeCurrent() { if (!this.isCurrent()) throw new IllegalStateException("No current item defined");

// // STUDENT WORK HERE. CODE GOES HERE //

}

public Iterator iterator() { return new SequenceIterator(); }

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