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.

Hello need help on this lab, what you need to do isadd a code to ArrayBag methods equals,duplicateAll,removeDuplicates and remove according to the

/** 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 implements BagInterface { private final T[] bag; private int numberOfEntries; private boolean integrityOK = false; private static final int DEFAULT_CAPACITY = 25; private static final int MAX_CAPACITY = 10000;

/** 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 T[] toArray() //OK public T[] toArray() // OK { checkIntegrity();

@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 aBag) { // COMPLETE THIS METHOD return false; } // end equals

/** 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 constructBag(int size) { return constructBag(size, size); }

ArrayBag constructBag(int size, int elements) { ArrayBag bag = new ArrayBag(size); for (int i = 0; i

@Test @Disabled void testRemove() { ArrayBag bag = constructBag(2); String removed = bag.remove(); assertEquals(1, bag.getCurrentSize()); }

@Test // @Disabled void testEqualsArrayBagOfT() { ArrayBag bag1 = constructBag(2), bag2 = constructBag(2); assertTrue(bag1.equals(bag1)); assertTrue(bag1.equals(bag2)); assertTrue(bag2.equals(bag1)); bag2.remove(); assertFalse(bag1.equals(bag2)); assertFalse(bag2.equals(bag1)); bag2.add("x"); assertFalse(bag1.equals(bag2)); assertFalse(bag2.equals(bag1)); }

@Test @Disabled void testDuplicateAll() { ArrayBag bag = constructBag(2, 1); assertTrue(bag.duplicateAll()); assertEquals(2, bag.getCurrentSize()); Object[] elements = bag.toArray(); assertEquals(elements[0], elements[1]); assertEquals(2, bag.getFrequencyOf("s0")); assertFalse(bag.duplicateAll()); }

@Test @Disabled void testRemoveDuplicates() { ArrayBag bag = constructBag(4, 1); bag.duplicateAll(); assertEquals(2, bag.getCurrentSize()); assertEquals(2, bag.getFrequencyOf("s0")); bag.duplicateAll(); assertEquals(4, bag.getCurrentSize()); assertEquals(4, bag.getFrequencyOf("s0")); bag.removeDuplicates(); assertEquals(1, bag.getCurrentSize()); assertEquals(1, bag.getFrequencyOf("s0")); }

}

The ArrayBag class is a working implementation of the BagInterface.java. The remove method already exists but needs to be modifed. The other three methods you will be working on already exist but do not function yet. Take a look at that code now if you have not done so already. Equals Step 1. Compile the classes BagExtensionsTest and ArrayBag. Run the main method in BagExtensionsTest. Checkpoint: If all has gone well, the program will run and the test cases for the four methods will execute. Don't worry about the results of the tests yet. The goal now is to finish the implementation of each of our methods one at a time. Step 2. In the equals method of ArrayBag, implement your algorithm from the pre-lab exercises. Some kind of iteration will be required in this method. Checkpoint: Compile and run BagExtensionsTest. The tests for equals should all pass. If not, debug and retest. Remove Step 1. Look at the results from the test cases from the previous run of BagExtensionsTest. Since this is an existing method we want to make sure that our extension does not break the correct function of the method. All but the last two test cases should result in passes. The last two tests are intended to show the new behavior. Step 2. In the remove method of ArrayBag, add the modifications from the pre-lab exercises. Checkpoint: Compile and run BagExtensionsTest. All of the tests in the test remove section should now pass. If not, debug and retest. Duplicate All Step 3. In the duplicateAll method of ArrayBag, implement your algorithm from the pre-lab exercises. Iteration is needed. Checkpoint: Compile and run BagExtensionsTest. All tests up through checkDuplicateAll should pass. If not, debug and retest. Remove Duplicates Step 4. In the removeduplicates method of ArrayBag, implement your algorithm from the pre-lab exercises. This method will require some form of iteration. If you use the technique recommended in the prelab, you will use nested iteration. Final checkpoint: Compile and run BagExtensionsTest. All tests should pass. If not, debug and retest

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!