Question: Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Write a method: public static ArrayList collectAll(ArrayList objects, Filter f) that

Declare an interface Filter as follows:

public interface Filter { boolean accept(Object x); }

Write a method: public static ArrayList collectAll(ArrayList objects, Filter f) that returns all objects in the objects list that are accepted by the given filter.

Provide a class ShortWordFilter whose filter method accepts all strings of length < 5.

Then write a program that asks the user for input and output textfile names, reads all words from input file, puts them into an ArrayList, calls collectAll, and prints a list of the short words to the output file.

Write another program which uses the interface and method written above, but this time it writes the results to a binary file using UTF-8. Also, this time it will append to the file if it already exists.

for both programs End text files with .txt, and binary files with .dat

Please provide the sample output.

I'm up to here:

public interface Filter {

boolean accept(Object x);

public static ArrayList collectAll(ArrayList objects, Filter f) {

ArrayList acceptedList = new ArrayList();

for (Object eachObj : acceptedList) {

if(f.accept(eachObj)) {

acceptedList.add(eachObj);

}

}

return acceptedList;

}

}

public class ShortWordFilter implements Filter{

@Override public boolean accept(Object x) { String s = x+""; return s.length()<5; } }

Now, please write the two programs that question asked for. And also provide a sample output.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!