Java Reference
In-Depth Information
@author Frank M. Carrano
*/
public class
ArrayBagDemo1
{
public static void
main(String[] args)
{
// a bag that is not full
BagInterface<String> aBag =
new
ArrayBag<String>();
// tests on an empty bag
testIsFull(aBag,
false
);
// adding strings
String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
testAdd(aBag, contentsOfBag1);
testIsFull(aBag,
false
);
// a bag that will be full
aBag =
new
ArrayBag<String>(7);
System.out.println("\nA new empty bag:");
// tests on an empty bag
testIsFull(aBag,
false
);
// adding strings
String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D"};
testAdd(aBag, contentsOfBag2);
testIsFull(aBag,
true
);
}
// end main
// Tests the method add.
private static void
testAdd(BagInterface<String> aBag,
String[] content)
{
System.out.print("Adding to the bag: ");
for
(
int
index = 0; index < content.length; index++)
{
aBag.add(content[index]);
System.out.print(content[index] + " ");
}
// end for
System.out.println();
displayBag(aBag);
}
// end testAdd
// Tests the method isFull.
// correctResult indicates what isFull should return.
private static void
testIsFull(BagInterface<String> aBag,
boolean
correctResult)
{