Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

/ 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 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")); }

}

image text in transcribed

In this lab you will explore the implementation of the ADT bag using arrays. Refer to the lab manual for specific instructions and completed algorithms. To get started, download the Eclipse Project Lab 3 Array Based Bag Implementation.zip and import into Eclipse. Students should focus on completing the implementation of the starting code following the steps in the Directed Lab Work section of the manual. Only the equals method is allowed to use other class methods to implement. When you have completed the lab, you should submit the Eclipse Project file ArrayBag.java as the solution to this assignment. If you are unable to complete the lab in class, you should still submit your files with a comment that the lab is incomplete. You are welcome to complete the lab either on your own device or in class during Office Hours. Hints - The lab manual contains detailed pseudocode of the solutions

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

Students also viewed these Databases questions

Question

What does this look like?

Answered: 1 week ago