Source of ArrayBagDemo1.java


  1: /** 
  2:     A test of the constructors and the methods add and toArray,
  3:     as defined in the first draft of the class ArrayBag.
  4:     @author Frank M. Carrano, Timothy M. Henry
  5:     @version 5.0
  6: */
  7: public class ArrayBagDemo1
  8: {
  9:         public static void main(String[] args) 
 10:         {
 11:       // Adding to an initially empty bag with sufficient capacity
 12:       System.out.println("Testing an initially empty bag with " +
 13:                          " sufficient capacity:");
 14:                 BagInterface<String> aBag = new ArrayBag1<>();
 15:                 String[] contentsOfBag1 = {"A", "A", "B", "A", "C", "A"};
 16:                 testAdd(aBag, contentsOfBag1);

 18:       // Filling an initially empty bag to capacity
 19:       System.out.println("\nTesting an initially empty bag that " +
 20:                          " will be filled to capacity:");
 21:                 aBag = new ArrayBag1<>(7);
 22:                 String[] contentsOfBag2 = {"A", "B", "A", "C", "B", "C", "D",
 23:                                  "another string"};
 24:                 testAdd(aBag, contentsOfBag2);
 25:         } // end main
 26:         
 27:    // Tests the method add.
 28:         private static void testAdd(BagInterface<String> aBag, String[] content)
 29:         {
 30:                 System.out.print("Adding the following strings to the bag: ");
 31:                 for (int index = 0; index < content.length; index++)
 32:                 {
 33:                         if (aBag.add(content[index]))
 34:             System.out.print(content[index] + " ");
 35:          else
 36:             System.out.print("\nUnable to add " + content[index] +
 37:                              " to the bag.");
 38:                 } // end for
 39:       System.out.println();
 40:       
 41:                 displayBag(aBag);
 42:         } // end testAdd

 44:    // Tests the method toArray while displaying the bag.
 45:         private static void displayBag(BagInterface<String> aBag)
 46:         {
 47:                 System.out.println("The bag contains the following string(s):");                
 48:                 Object[] bagArray = aBag.toArray();
 49:                 for (int index = 0; index < bagArray.length; index++)
 50:                 {
 51:                         System.out.print(bagArray[index] + " ");
 52:                 } // end for
 53:                 
 54:                 System.out.println();
 55:         } // end displayBag
 56: } // end ArrayBagDemo1
 57: /*
 58:  Testing an initially empty bag with  sufficient capacity:
 59:  Adding the following strings to the bag: A A B A C A
 60:  The bag contains the following string(s):
 61:  A A B A C A
 62:  
 63:  Testing an initially empty bag that  will be filled to capacity:
 64:  Adding the following strings to the bag: A B A C B C D
 65:  Unable to add another string to the bag.
 66:  The bag contains the following string(s):
 67:  A B A C B C D 
 68: */