Question
Need help with this one thank you in advance. public final class ArrayBag implements BagInterface { private final T[] bag; private int numberOfEntries; private boolean
Need help with this one thank you in advance.
public final class ArrayBag
/** Creates an empty bag whose initial capacity is 25. */ public ArrayBag() { this(DEFAULT_CAPACITY); } // end default constructor
/** Creates an empty bag having a given capacity. @param desiredCapacity The integer capacity desired. */ public ArrayBag(int desiredCapacity) { if (desiredCapacity
/** Adds a new entry to this bag. @param newEntry The object to be added as a new entry. @return True if the addition is successful, or false if not. */ public boolean add(T newEntry) { checkIntegrity(); boolean result = true; if (isArrayFull()) { result = false; } else { // Assertion: result is true here bag[numberOfEntries] = newEntry; numberOfEntries++; } // end if
return result; } // end add
/** Retrieves all entries that are in this bag. @return A newly allocated array of all the entries in this bag. */ // public
// The cast is safe because the new array contains null entries. @SuppressWarnings("unchecked") T[] result = (T[]) new Object[numberOfEntries]; // Unchecked cast
for (int index = 0; index
public boolean isEmpty() { return numberOfEntries == 0; }
public int getCurrentSize() { return numberOfEntries; }
public int getFrequencyOf(T anEntry) { checkIntegrity(); int counter = 0;
for (int index = 0; index
return counter; }
public boolean contains(T anEntry) { checkIntegrity(); return getIndexOf(anEntry) > -1; // or >= 0 }
public void clear() { while (!isEmpty()) remove(); }
public T remove() { checkIntegrity();
// MODIFY THIS METHOD TO REMOVE A RANDOM ITEM FROM THE BAG T result = removeEntry(numberOfEntries - 1);
return result; }
public boolean remove(T anEntry) { checkIntegrity();
int index = getIndexOf(anEntry); T result = removeEntry(index); return anEntry.equals(result); } // end remove
// Returns true if the array bag is full, or false if not. private boolean isArrayFull() { return numberOfEntries >= bag.length; } // end isArrayFull
// Locates a given entry within the array bag. // Returns the index of the entry, if located, // or -1 otherwise. // Precondition: checkInitialization has been called. private int getIndexOf(T anEntry) { int where = -1; boolean found = false; int index = 0;
while (!found && (index
// Assertion: If where > -1, anEntry is in the array bag, and it // equals bag[where]; otherwise, anEntry is not in the array.
return where; } // end getIndexOf
// Removes and returns the entry at a given index within the array. // If no such entry exists, returns null. // Precondition: 0
if (!isEmpty() && (givenIndex >= 0)) { result = bag[givenIndex]; // Entry to remove int lastIndex = numberOfEntries - 1; bag[givenIndex] = bag[lastIndex]; // Replace entry to remove with last entry bag[lastIndex] = null; // Remove reference to last entry numberOfEntries--; } // end if
return result; } // end removeEntry
// Throws an exception if this object is not initialized. private void checkIntegrity() { if (!integrityOK) throw new SecurityException("ArrayBag object is corrupt."); } // end checkIntegrity
/** Override the toString() method so that we get a more useful display of * the contents in the bag. * @return a string representation of the contents of the bag */ public String toString() {
String result = "Bag[ ";
for (int index = 0; index
result += "]"; return result; } // end toString
/********************************************************************* * * METHODS TO BE COMPLETED * * ************************************************************************/
/** Check to see if two bags are equals. * @param aBag Another object to check this bag against. * @return True the two bags contain the same objects with the same frequencies. */ public boolean equals(ArrayBag
/ public boolean duplicateAll() { checkIntegrity(); // COMPLETE THIS METHOD return false; }
/** Remove all duplicate items from a bag */ public void removeDuplicates() { checkIntegrity();
// COMPLETE THIS METHOD
return; }
}
(Test Code)
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test;
class ArrayBagTest {
ArrayBag
ArrayBag @Test @Disabled void testRemove() { ArrayBag @Test // @Disabled void testEqualsArrayBagOfT() { ArrayBag @Test @Disabled void testDuplicateAll() { ArrayBag @Test @Disabled void testRemoveDuplicates() { ArrayBag }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started