Implementing More Methods

In this lesson, we will define some more of our class's methods.

Now that we can add strings to a bag, we can implement the remaining methods, beginning with the easiest ones. However, we will postpone the definitions of remove momentarily until we see how to search a bag.

The methods isEmpty, getCapacity, and getCurrentSize

The methods isEmpty, getCapacity, and getCurrentSize have straightforward definitions, as we can see here:

/** Sees whether this bag is empty.
    @return true if this bag is empty, or false if not. */
public boolean isEmpty()
{
   return numberOfStrings == 0;
} // End isEmpty

/** Gets the capacity of this bag.
    @return the integer number of strings that this bag can hold. */
public int getCapacity()
{
   return bag.length;
} // End getCapacity

/** Gets the number of strings currently in this bag.
    @return the integer number of strings currently in this bag. */
public int getCurrentSize()
{
   return numberOfStrings;
} // End getCurrentSize

Get hands-on with 1400+ tech skills courses.