P9.17 (For Java, and can you please explain aswell.)
Declare an interface Filter as follows:
public interface Filter
{
boolean accept(Object x);
}
Write a method: public static ArrayList collectAll(ArrayListobjects, Filter f) that returns all objects in the objects listthat are accepted by the given filter.
Provide a class ShortWordFilter whose filter method accepts allstrings of length < 5.
Then write a program that asks the user for input and outputtextfile names, reads all words from an input file, puts them intoan ArrayList, calls collectAll, and prints a list of the shortwords to the output file.
Answer
import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Scanner;interface Filter{boolean accept(Object x);}class ShortWordFilter implements