Question: Hello need help on this lab, what you need to do is add a code to ArrayBag methods equals,duplicateAll,removeDuplicates and remove according to the screenshots
Hello need help on this lab, what you need to do is add a code to ArrayBag methods equals,duplicateAll,removeDuplicates and remove according to the screenshots and then run it with the JUnit tests present in ArrayBagTest. I will leave both programs here.


/** A class of bags whose entries are stored in a fixed-size array. @author Frank M. Carrano, Timothy M. Henry * The toString method is overwritten to give a nice display of the items in * the bag in this format Bag{Size:# [1] [2] [3] [4] } * * Extra methods added for lab exercise by Charles Hoot * //- * @version 5.1
*/ 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++; }
return result; } // public
@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; }
/** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in this ba. */ public int getFrequencyOf(T anEntry) { checkIntegrity(); int counter = 0;
for (int index = 0; index
return counter; }
/** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if this bag contains anEntry, or false otherwise. */ public boolean contains(T anEntry) { checkIntegrity(); return getIndexOf(anEntry) > -1; // or >= 0 } // end contains
/** Removes all entries from this bag. */ public void clear() { while (!isEmpty()) remove(); } // end clear
/** Removes one unspecified entry from this bag, if possible. @return Either the removed entry, if the removal was successful, or null. */ public T remove() { checkIntegrity();
// MODIFY THIS METHOD TO REMOVE A RANDOM ITEM FROM THE BAG T result = removeEntry(numberOfEntries - 1);
return result; } // end remove
/** Removes one occurrence of a given entry from this bag. @param anEntry The entry to be removed. @return True if the removal was successful, or false if not. */ public boolean remove(T anEntry) { checkIntegrity();
int index = getIndexOf(anEntry); T result = removeEntry(index); return anEntry.equals(result); } private boolean isArrayFull() { return numberOfEntries >= bag.length; } private int getIndexOf(T anEntry) { int where = -1; boolean found = false; int index = 0;
while (!found && (index
return where; }
private T removeEntry(int givenIndex) { T result = null;
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
/** Duplicate all the items in a bag. * @return True if the duplication is possible. */ public boolean duplicateAll() { checkIntegrity(); // COMPLETE THIS METHOD return false; } // end duplicateAll
/** Remove all duplicate items from a bag */ public void removeDuplicates() { checkIntegrity();
// COMPLETE THIS METHOD
return; } // end removeDuplicates
} // end ArrayBag
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
Get step-by-step solutions from verified subject matter experts
