Other 2008 FR Questions FR other years Be Prepared Home
AB-4
Part (a)
public class OrFilter implements Filter
{
  private List<Filter> filters;

  public OrFilter(Filter f1, Filter f2)
  {
    filters = new LinkedList<Filter>(); 1
    filters.add(f1);
    filters.add(f2);
  }

  public void add(Filter f)
  {
    filters.add(f);
  }

  public boolean accept(String text)
  {
    for (Filter f : filters)
      if (f.accept(text))
        return true;
    return false;
  }
}
Notes:
  1. Or ArrayList<Filter>

Part (b)
  /** @param desirable contains strings that are allowed
   *         Precondition: desirable.length > 1
   *  @param notAllowed the string that is not allowed
   *  @return a Filter that accepts strings that contain at least one string
   *          in desirable and do not contain notAllowed.
   */
  public static Filter buildFilter(String[] desirable,
                                   String notAllowed)
  {
    OrFilter orF = new OrFilter(new SimpleFilter(desirable[0]),
                                new SimpleFilter(desirable[1]));
    for (int i = 2; i < desirable.length; i++)
      orF.add(new SimpleFilter(desirable[i]));

    NotFilter notF = new NotFilter(new SimpleFilter(notAllowed));

    return new AndFilter(orF, notF);
  }

Other 2008 FR Questions | Back to Contents

Copyright © 2008 by Skylight Publishing
support@skylit.com