Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.ArrayList;
- public class A2Q5
- {
- public interface Filter
- {
- boolean accept(Object x);
- }
- // class ShortWordFilter implements a Filter class for filtering a list of strings by char count
- public static class ShortWordFilter implements Filter
- {
- private static final ArrayList<String> theList = new ArrayList<>();
- // ShortWordFilter constructor gets word from main and sends to filter method for further processing
- public ShortWordFilter(Object word)
- {
- filter(word);
- }
- // filter method determines if string is less than 5 char long and assigns it to an arraylist if it is
- public void filter(Object word)
- {
- if (word.toString().length() < 5)
- {
- theList.add((String) word);
- }
- }
- // collectAll gets list of objects and data from Filter interface and assigns for main
- public static ArrayList<Object> collectAll(ArrayList<Object> objects, Filter f)
- {
- objects = new ArrayList<Object>(theList);
- return objects;
- }
- }
- // main method contains a loop asking for different words from the user
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- String word = "";
- ArrayList<Filter> filterList = new ArrayList<Filter>();
- while (!word.equals("0")) {
- System.out.println("Please input a word or '0' to end the loop:");
- word = in.nextLine();
- filterList.add(new ShortWordFilter(word) {});
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement