Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2) Given the following definition for BagInterface and skeleton for an ArrayBag

ID: 3870621 • Letter: 2

Question

2) Given the following definition for BagInterface and skeleton for an ArrayBag class, complete the add method and helper isArrayFull method. When the array is full, the add method will return false, otherwise the addition should be successful. Write the header and body for both methods for your response.

/**

   An interface that describes the operations of a bag of objects.

*/

public interface BagInterface<T>

{

/** Gets the current number of entries in this bag.

@return  The integer number of entries currently in the bag. */

public int getCurrentSize();

/** Sees whether this bag is empty.

@return  True if the bag is empty, or false if not. */

public boolean isEmpty();

/** 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);

/** Removes one unspecified entry from this bag, if possible.

     @return  Either the removed entry, if the removal was successful, or null. */

public T 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);

/** Removes all entries from this bag. */

public void clear();

/** 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 the bag. */

public int getFrequencyOf(T anEntry);

/** Tests whether this bag contains a given entry.

@param anEntry  The entry to locate.

@return  True if the bag contains anEntry, or false if not. */

public boolean contains(T anEntry);

/** Retrieves all entries that are in this bag.

@return  A newly allocated array of all the entries in the bag.

Note: If the bag is empty, the returned array is empty. */

public T[] toArray();

//public <T> T[] toArray();  // Alternate

//public Object[] toArray(); // Alternate

  

} // end BagInterface

/**

  A class of bags whose entries are stored in an array.

*/

public final class ArrayBag<T> implements BagInterface<T>

{

   private final T[] bag;

   private int numberOfEntries;

   private static final int DEFAULT_CAPACITY = 25;

/** Creates an empty bag whose 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)

{

// The cast is safe because the new array contains null entries

@SuppressWarnings("unchecked")

  T[] tempBag = (T[])new Object[desiredCapacity]; // Unchecked cast

bag = tempBag;

numberOfEntries = 0;

} // end constructor

/** 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)

{

WRITE CODE FOR THIS METHOD

} // end add

   // Returns true if the array bag is full, or false if not.

private boolean isArrayFull()

{

WRITE CODE FOR THIS METHOD

} // end isArrayFull

...

} // end ArrayBag1

  

Your Answer:

Explanation / Answer

private boolean isArrayFull()
{
     return numberOfEntries == bag.length;
} // end isArrayFull

public boolean add(T newEntry)
{
     if (isArrayFull)
         return false;
     bag[numberOfEntries++] = newEntry;
     return true;
} // end add

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote