...

/

Methods That Remove Strings

Methods That Remove Strings

In this lesson, we define the remaining methods for the class BagOfStrings, those that remove entries.

We have postponed the methods to remove strings from a bag until now because defining a method to remove a specific string is somewhat more difficult and involves a search like the one we performed in the method contains. We begin, however, with the two methods that are not as difficult to define.

The method clear

The method clear removes all the strings from a bag, one at a time. The following definition of clear calls the method remove until the bag is empty:

/** Removes all strings from this bag. */
public void clear()
{
   while (!isEmpty())
      remove();
} // End clear

Exactly which string is removed by each cycle of the loop is unimportant. Thus, we call the remove method that removes an unspecified string. Moreover, we do not save the string that the method returns.

Checkpoint question

1.

Revise the previous definition of the method clear so that it does not call isEmpty, and so the while statement has an empty body. Hint: What does remove return when the bag is empty?

Show Answer
Q1 / Q1
Did you find this helpful?
Press + to interact
// Write your definition of the method clear here:

We cannot test the method clear yet, because we have not defined the method remove.

But must clear call remove?

Checkpoint questions

1.

Can you think of a better way to implement clear, one that does not involve removing entries from the bag one at a time using the method remove? Describe such an approach.

Show Answer
Q1 / Q2
Press + to interact
BagDemo.java
BagOfStrings.java
/**
A class that partially implements a bag of strings by using an array.
@author Frank M. Carrano
*/
public class BagOfStrings
{
private static final int DEFAULT_CAPACITY = 25;
private String[] bag;
private int numberOfStrings;
/** Creates an empty bag whose capacity is 25. */
public BagOfStrings()
{
this(DEFAULT_CAPACITY);
} // End default constructor
/** Creates an empty bag having a given capacity.
@param capacity The integer capacity desired. */
public BagOfStrings(int capacity)
{
bag = new String[capacity];
numberOfStrings = 0;
} // End constructor
// Define the method clear here:
/** Removes all strings from this bag. */
public void clear()
{
} // End clear
/** Adds a new string to this bag.
@param newString The string to be added.
@return true if the addition is successful, or false if not. */
public boolean add(String newString)
{
boolean result = true;
if (isFull())
{
result = false;
}
else
{ // Assertion: result is true here
bag[numberOfStrings] = newString;
numberOfStrings++;
} // End if
return result;
} // End add
/** Retrieves all strings that are in this bag.
@return a newly allocated array of all the strings in this bag. */
public String[] toArray()
{
String[] result = new String[numberOfStrings];
for (int index = 0; index < numberOfStrings; index++)
{
result[index] = bag[index];
} // End for
return result;
// Note: The body of this method could consist of one return statement,
// if you call Arrays.copyOf
} // End toArray
/** Sees whether this bag is full.
@return true if this bag is full, or false if not */
public boolean isFull()
{
return numberOfStrings == bag.length;
} // End isFull
/** 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
} // End BagOfStrings

Removing an unspecified string

The method remove that has no parameter removes any string from a bag, as long as the bag is not empty. Recall from its specification in the lesson Specifying a Bag that the method returns the string it removes. If the method cannot remove a string because the bag is empty, it returns null.

Removing a string from a bag involves removing it from an array. While we can access any string in the array bag, the last one is easy to remove. To do so, we:

  • Get a reference to the string in bag[numberOfStrings - 1] so it can be returned
  • Set bag[numberOfStrings - 1] to null
  • Decrement numberOfStrings

Decrementing numberOfStrings causes the last entry to be ignored, meaning that ...

Access this course and 1400+ top-rated courses and projects.