import java.util.ArrayList; interface Condition { public abstract boolean satisfiesCondition( String s ); } class WordsLen implements Condition { public WordsLen( int theLen ) { len = theLen; } public boolean satisfiesCondition( String s ) { return s.length( ) == len; } private int len; } class WordsTwiceInRow implements Condition { public boolean satisfiesCondition( String s ) { for( int i = 1; i < s.length( ); i++ ) if( s.charAt( i ) == s.charAt( i - 1 ) ) return true; return false; } } class WordsStartWithVowel implements Condition { public boolean satisfiesCondition( String s ) { char t = s.charAt( 0 ); if( t == 'a' || t == 'e' || t == 'i' || t == 'o' || t == 'u' ) return true; return false; } } class Day15 { public static ArrayList countWords( String [ ] arr, Condition f ) { ArrayList result = new ArrayList( ); for( String s : arr ) if( f.satisfiesCondition( s ) == true ) result.add( s ); return result; } public static void main( String [ ] args ) { String [ ] arr = { "hello", "world", "this", "if", "that", "too" }; // Count number of words of length 5 System.out.println( countWords( arr, new WordsLen( 5 ) ) ); // Count number of words of length 4 System.out.println( countWords( arr, new WordsLen( 4 ) ) ); // Count number of words contains the same letter twice in a row System.out.println( countWords( arr, new WordsTwiceInRow( ) ) ); // Count number of words starting with a vowel System.out.println( countWords( arr, new WordsStartWithVowel( ) ) ); } }