THINK FIRST·CODE LATER

← All labs

Building rules from functional interfaces

Problem

Write TextRules. The first input is a minimum length; the rest are words. Write a method

static List<String> transform(List<String> words, Predicate<String> keep, Function<String, String> change)

and use lambdas and method references of these types: Predicate<String> (long enough; starts with an upper-case letter), Function<String, String> (String::toUpperCase), Function<String, Integer> (String::length), UnaryOperator<String> (add !), BinaryOperator<String> (the longer of two words, the first on a tie) and Supplier<String> ((none)). Combine them with and, or, negate and andThen.

For the input

4 Java is Fun and Powerful today

the program prints

long enough: [Java, Powerful, today]
long AND capitalised: [Java, Powerful]
NOT long enough: [is, Fun, and]
short OR capitalised, shouted: [JAVA!, IS!, FUN!, AND!, POWERFUL!]
first word: Java has 4 letters
longest word: Powerful

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

d3-6-data-1.txt

4 Java is Fun and Powerful today
What your program must print for d3-6-data-1.txt
long enough: [Java, Powerful, today]
long AND capitalised: [Java, Powerful]
NOT long enough: [is, Fun, and]
short OR capitalised, shouted: [JAVA!, IS!, FUN!, AND!, POWERFUL!]
first word: Java has 4 letters
longest word: Powerful

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < d3-6-data-1.txt

Windows PowerShell does not understand <:

Get-Content d3-6-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.