import java.util.ArrayList;
public class WordList
{
private ArrayList<String> myList;
public WordList()
{
}// end of default constructor
public WordList( ArrayList<String> a )
{
myList = new ArrayList<String>( a );
}// end of default constructor
public int numWordsOfLength( int len )
{
int temp = 0;
for( String s : myList )
{
if( s.length() == len )
temp++;
}// check each String in the list
return temp;
}// end of numWordsOfLength
public void removeWordsOfLength( int len )
{
for(int i = 0; i < myList.size(); i++)
{
String s = myList.get( i );
if( s.length() == len )
{
myList.remove( i );
i--;
}// remove the word if it is the correct size
}// end of removing all the words
}// end of removeWordsOfLength
public String toString()
{
//use a for each loop to create the master String
String master = new String();
for( String s : myList )
master += ( s + " " );
return master;
}// end of toString
// public void add(String a)
// {
// myList.add(a);
// }// end of add (an animal)
// note used because of constructor
}// end of class WordList
********************************************************************************
import java.util.ArrayList;
public class WordListDriver
{
public static void main(String[] arg)
{
// 0. create a source of data for the WordList object
ArrayList<String> temp = new ArrayList<String>();
temp.add( new String( "cat" ) );
temp.add( new String( "mouse" ) );
temp.add( new String( "frog" ) );
temp.add( new String( "dog" ) );
temp.add( new String( "dog" ) );
System.out.println( "temp array is: " + temp );
// 1. create a WordList object
WordList animals = new WordList( temp );
// 2. populate the WordList object
// there are two ways
// A. constructor - better, see above
// B. create and add method
// animals.add("asdasd"); // this is B.
// 3. print the WordList object
System.out.println( animals );// the toString() is not necessary though you could put it in
// 4. call the new methods
System.out.println( "how many have a length of 2: " + animals.numWordsOfLength( 2 ) );
System.out.println( "how many have a length of 2: " + animals.numWordsOfLength( 3 ) );
System.out.println( "how many have a length of 2: " + animals.numWordsOfLength( 4 ) );
System.out.println( "how many have a length of 2: " + animals.numWordsOfLength( 5 ) );
System.out.println( "how many have a length of 2: " + animals.numWordsOfLength( 6 ) );
System.out.println( animals );
animals.removeWordsOfLength( 4 );
System.out.println( animals );
animals.removeWordsOfLength( 3 );
System.out.println( animals );
animals.removeWordsOfLength( 2 );
System.out.println( animals );
// 5. print the WordList object to verify methods worked
}// end of main
}// end of WordListDriver
*****************************************************************************************************************