Question: Java Question.... (has to do with arrays) The 3 things I have: /* * Bags * * program for testing methods in ArrayBag and LinkedBag
Java Question.... (has to do with arrays)


The 3 things I have: /* * Bags * * program for testing methods in ArrayBag and LinkedBag */ public class Bags { public static void main (String [] args) { try { Bag words = new ArrayBag(); //Bag words = new LinkedBag(); // adding to bag String [] fruits = {"orange", "grape", "kiwi", "coconut", "lime"}; for (int i = 0; i
/* * ArrayBag * * array-based implementation of a bag * * unordered collection with duplicates allowed */ public class ArrayBag implements Bag { public static final int DEFAULT_CAPACITY = 10; private T [] collection; private int size; /* * default constructor * * bag initially set to 10 items */ public ArrayBag() { this(DEFAULT_CAPACITY); } /* * argument constructor * * size of bag specified by user */ @SuppressWarnings("unchecked") public ArrayBag(int capacity) { if (capacity
public interface Bag { void add (T item); T remove(); T get(); boolean contains (T item); int size(); public String toString(); boolean remove (T item); T removeRandom(); T getRandom(); } thanks in advance!!
To give users the ability to remove a particular item, or to remove or retrieve a random item, we will add the following methods to our Bag interface: boolean remove (T item); TremoveRandom); T getRandom); Implement these three methods for Array Bag. Then test them using the Bags driver program. Your output should look like this: Here's whats in our bag (orange, grape, kiwi, coconut, lime] Does our bag contain the word "kiwi yes Does our bag contain the word 'mango'? no Selecting an item (always same) lime lime Selecting a random item (varies) coconut kiwi Removing 'grape' from the bag (orange, lime, kiwi, coconut Removing an item (always end one) coconut (orange, lime, kiwi) Removing a random item lime (orange, kiwi)
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
