Java Question.... (has to do with arrays) To give users the ability to remove a
ID: 3884327 • Letter: J
Question
Java Question.... (has to do with arrays)
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)Explanation / Answer
ArrayBag.java is the only class that was modified.
PROGRAM CODE:
ArrayBag.java
package bag;
import java.util.Random;
/*
* ArrayBag
*
* array-based implementation of a bag
*
* unordered collection with duplicates allowed
*/
public class ArrayBag<T> implements Bag<T>
{
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 < 0)
{
throw new IllegalArgumentException("bag can't be negative size!");
}
collection = (T []) new Object [capacity];
size = 0;
}
/*
* adds item to bag
*
* if at capacity, bag is resized
*/
public void add (T item)
{
checkForNull(item);
ensureSpace();
collection[size] = item;
size++;
}
/*
* removes item from bag
*
* returns removed item
*/
public T remove ()
{
if (size == 0)
{
return null;
}
T removed = collection[size-1];
collection[size-1] = null;
size--;
return removed;
}
/*
* returns item from bag
*/
public T get()
{
if (size == 0)
{
return null;
}
return collection[size-1];
}
/*
* returns true if item is in bag, false otherwise
*/
public boolean contains(T item)
{
for (int i = 0; i < size; i++)
{
if (collection[i].equals(item))
{
return true;
}
}
return false;
}
/*
* returns size of bag
*/
public int size()
{
return size;
}
/*
* returns string representation of contents of bag
*/
public String toString()
{
String s = "[";
for (int i = 0; i < size; i++)
{
s+= collection[i];
if (i < size -1)
{
s+= ", ";
}
}
return s + "]";
}
/* methods to be added */
/*
* removes specified item from bag
*
* returns false if item not present
*/
public boolean remove (T item)
{
for(int i=0; i<size; i++)
{
if(item.equals(collection[i]))
{
for(int j=i; j<size-1; j++)
{
collection[j] = collection[j+1];
}
collection[size-1] = null;
size--;
return true;
}
}
return false;
}
/*
* removes random item from bag
*
* returns null if bag is empty
*/
public T removeRandom ()
{
if(size == 0)
return null;
T item = collection[new Random().nextInt(size)];
remove(item);
return item;
}
/*
* returns random item from bag
*
* returns null if bag is empty
*/
public T getRandom ()
{
if(size == 0)
return null;
return collection[new Random().nextInt(size)];
}
/* end of added methods */
/*
* checks if item is null and throws exception if so
*/
private void checkForNull (T item)
{
if (item == null)
{
throw new IllegalArgumentException("null not a possible value!");
}
}
/*
* doubles size of array if at capacity
*/
private void ensureSpace()
{
if (size == collection.length)
{
@SuppressWarnings("unchecked")
T [] larger = (T []) new Object [size * 2];
for (int i = 0; i < size; i++)
{
larger[i] = collection[i];
}
collection = larger;
larger = null;
}
}
}
OUTPUT:
Here's what's 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
lime
Removing 'grape' from the bag
[orange, kiwi, coconut, lime]
Removing an item (always end one)
lime
[orange, kiwi, coconut]
Removing a random item
kiwi
[orange, coconut]
Let's empty the bag
[]
Trying to get a random item
null
Trying to remove a random item
null
Trying to remove 'kiwi'
false
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.