2004 Word List

<< 2005Average | APQuestionsTrailIndex | 2004 Pet >>

Question from the College Board

WordList.java

import java.util.ArrayList;
public class WordList 
{
	private ArrayList myList;

	public int numWordsOfLength(int len){
		//part (a)
	}
	public void removeWordsOfLength(int len){
		//part(b)
	}
	public WordList(String[] list){
		myList=new ArrayList();
		for (String s:list)
			myList.add(s);
	}
	public String toString(){
		return myList.toString();
	}
}

WordListTester.java

import java.util.ArrayList;


public class WordListTester 
{
	public static void main(String[] args){
		String[] a={"cat", "mouse", "frog","dog", "dog"};
		WordList animals = new WordList(a);
		System.out.println("Part A \nanimals="+animals.toString());
		System.out.println("animals.numWordsOfLength(4)="+
				animals.numWordsOfLength(4)+" - (should be 1)"
		);
		System.out.println("animals.numWordsOfLength(3)="+
				animals.numWordsOfLength(3)+" - (should be 3)"
		);
		System.out.println("animals.numWordsOfLength(2)="+
				animals.numWordsOfLength(2)+" - (should be 0)"
		);
		System.out.println("Part B ");
		animals.removeWordsOfLength(4);
		System.out.println("After removing length 4: \nanimals="+animals.toString()+
				" - should be [cat mouse dog dog]");
		animals.removeWordsOfLength(3);
		System.out.println("After removing length 3: \nanimals="+animals.toString()+
				" - should be [ mouse ]");
		animals.removeWordsOfLength(2);
		System.out.println("After removing length 2: \nanimals="+animals.toString()+
				" - should be [ mouse ]");
	}
}