Menu
support@authoritypapers.com
+1(805) 568 7317

write a removeevery method for the alist class themethod removes all occurrences of 4951051

Write a removeEvery method for the AList class. Themethod removes all occurrences of a specific entry from the listand returns the number of elements removed.
For full credit, write an efficient solution. To dothis, try to minimize the number of times you cycle through thearray. The method header is: public int removeEvery(T element)
Alist class is given as below: import java.util.Arrays; /** A class that implements a list of objects by using an array. Entries in a list have positions that begin with 1. Duplicate entries are allowed. @author Frank M. Carrano @author Timothy M. Henry @version 5.0 */ public class AList>implements ListInterface {    private T[] list; // Array of list entries; ignorelist[0]    private int numberOfEntries;    private boolean integrityOK;    private static final int DEFAULT_CAPACITY = 25;    private static final int MAX_CAPACITY = 10000;    public AList()    { this(DEFAULT_CAPACITY);    } // end default constructor    public AList(int initialCapacity)    { integrityOK = false;    // Is initialCapacity too small? if (initialCapacity < DEFAULT_CAPACITY)    initialCapacity = DEFAULT_CAPACITY; else // Is initialCapacity too big?    checkCapacity(initialCapacity);    // The cast is safe because the new array contains nullentries @SuppressWarnings(“unchecked”) T[] tempList = (T[])new Comparable[initialCapacity + 1]; list = tempList; numberOfEntries = 0; integrityOK = true;    } // end constructor public void add(T newEntry) { add(numberOfEntries + 1, newEntry); /* // Alternate code checkIntegrity(); list[numberOfEntries + 1] = newEntry; numberOfEntries++; ensureCapacity(); */ } // end add    public void add(int givenPosition, T newEntry)    { checkIntegrity(); if ((givenPosition >= 1) && (givenPosition <=numberOfEntries + 1)) {    if (givenPosition <= numberOfEntries) makeRoom(givenPosition);    list[givenPosition] = newEntry;    numberOfEntries++;    ensureCapacity(); // Ensure enough room for nextadd } else    throw new IndexOutOfBoundsException(“Given positionof add’s new entry is out of bounds.”);    } // end add    public T remove(int givenPosition)    { checkIntegrity(); if ((givenPosition >= 1) && (givenPosition <=numberOfEntries)) {    // Assertion: The list is not empty    T result = list[givenPosition]; // Get entry to beremoved    // Move subsequent entries towards entry to beremoved,    // unless it is last in list    if (givenPosition < numberOfEntries) removeGap(givenPosition);    list[numberOfEntries] = null;    numberOfEntries–;    return result; // Return reference to removedentry } else    throw new IndexOutOfBoundsException(“Illegalposition given to remove operation.”);    } // end remove public void clear() { checkIntegrity(); // Clear entries but retain array; no need to create a newarray for (int index = 1; index <= numberOfEntries; index++) //Loop is part of Q4 list[index] = null; numberOfEntries = 0; } // end clear public T replace(int givenPosition, T newEntry) { checkIntegrity(); if ((givenPosition >= 1) && (givenPosition <=numberOfEntries)) {    // Assertion: The list is not empty    T originalEntry = list[givenPosition];    list[givenPosition] = newEntry;    return originalEntry; } else    throw new IndexOutOfBoundsException(“Illegalposition given to replace operation.”);    } // end replace public T getEntry(int givenPosition) { checkIntegrity(); if (validPosition(givenPosition)) {    // Assertion: The list is not empty    return list[givenPosition]; } else    throw new IndexOutOfBoundsException(“Illegalposition given to getEntry operation.”); } // end getEntry    public T[] toArray()    { checkIntegrity();    // The cast is safe because the new array contains nullentries @SuppressWarnings(“unchecked”) T[] result = (T[])new Comparable[numberOfEntries]; // Uncheckedcast for (int index = 0; index < numberOfEntries; index++) {    result[index] = list[index + 1]; } // end for    return result;    } // end toArray    public boolean contains(T anEntry)    { checkIntegrity(); boolean found = false; int index = 1; while (!found && (index <= numberOfEntries)) {    if (anEntry.equals(list[index])) found = true;    index++; } // end while return found;    } // end contains    public int getLength()    { return numberOfEntries;    } // end getLength    public boolean isEmpty()    { return numberOfEntries == 0; // Or getLength() == 0    } // end isEmpty    // Doubles the capacity of the array list if it isfull.    // Precondition: checkIntegrity has beencalled.    private void ensureCapacity()    { int capacity = list.length – 1; if (numberOfEntries >= capacity) {    int newCapacity = 2 * capacity;    checkCapacity(newCapacity); // Is capacity toobig?    list = Arrays.copyOf(list, newCapacity + 1); } // end if    } // end ensureCapacity    // Makes room for a new entry at newPosition.    // Precondition: 1 <= newPosition <=numberOfEntries + 1; // numberOfEntries is list’s length before addition;    // checkIntegrity has been called.    private void makeRoom(int givenPosition)    { // Assertion: (newPosition >= 1) && (newPosition<= numberOfEntries + 1) int newIndex = givenPosition; int lastIndex = numberOfEntries;    // Move each entry to next higher index, starting at end of // list and continuing until the entry at newIndex is moved for (int index = lastIndex; index >= newIndex; index–)    list[index + 1] = list[index];    } // end makeRoom    // Shifts entries that are beyond the entry to beremoved to the    // next lower position.    // Precondition: 1 <= givenPosition

MAX_CAPACITY)    throw new IllegalStateException(“Attempt to createa list ” +    “whose capacity exceeds ” +    “allowed maximum.”);    } // end checkCapacity    public boolean moveToEnd() { if(numberOfEntries==1) { // no changes made for a singletonlist return true; } else if(!isEmpty()) { // changes made for lists larger thansize 1 T firstItem = remove(1); add(firstItem); return true; } else { // empty list return false; } }    private boolean validPosition(int position) { return position >= 1 && position getAllLessThan(Tentry) { AList lessThanList = new AList<>(); for(int i=1; i Attached

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now