Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab is about computational problems and then implement them in the functional programming framework of computation streams introduced in Java 8. Therefore in this




This lab is about  computational problems and then implement them in the functional programming framework of computation streams introduced in Java 8. Therefore in this lab, you are absolutely forbidden to use any conditional statements (either if or switch), loops (either for, while or do-while) or even recursion. Instead, all computation must be implemented using only Java 8 computation streams and their core operations!

In this lab, we shall also brieYly check out the Java NIO framework for improved operations on Yiles than those that were offered in the old package java.io and the class File therein. Your methods receive some Path as an argument, guaranteed to refer to some text Yile in your Yile system whose contents your methods will then process with computation streams. In the JUnit test, this will again be the example text corpus of warandpeace.txt. Create new class named StreamExercises, inside which you write the following two static methods.


public static int countLines(Path path, int thres) throws IOException


This method should Yirst use the utility method Files.lines to convert the given path into an instance of Stream that produces the lines of this text Yile as a stream of strings, one line at the time. The method should then use the stream transformation method filter to keep only those whose length is greater or equal to the threshold value thres, and in the end, return the count of how many such lines the Yile contains.


public static List collectWords(Path path) throws IOException


This method should also use the same utility method Files.lines to Yirst turn its parameter path into a Stream. Each line should be converted to lowercase and broken down to individual words that are passed down the stream as separate String objects (the stream operation flatMap will be handy here). Split each line into its individual words with the aid of the method split in String with the word separator regex "[^a-z]+". Then in the stages of the computation stream that follow, discard all empty words with another filter, and sort the remaining words in alphabetical order. Multiple consecutive occurrences of the same word should be removed (you can simply use the stream operation distinct, or if you want to do this yourself the hard way as an exercise, write a custom Predicate subclass whose method test accepts its argument if and only if it was the Yirst one or was distinct from the argument of the previous call). To wrap up this computation, collect it into a List as the Yinal answer.


As this new century shapes up to be a absurdist reboot of its predecessor during the worldwide pandemic, can rediscovery of principles of futurism be far away? After all, the original futurists certainly were the original "tech bros" of their time! At some point computer users will also notice

computers work subjectively no they did two decades ago. What Andy Grove giveth, Bill Gates still taketh away...

TEST CODE:

import org.junit.Test; import java.io.IOException; import java.nio.file.Paths; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class StreamExercisesTest { @Test public void testCountLines() { try { assertEquals(StreamExercises.countLines(Paths.get("warandpeace.txt"), 70), 21079); } catch(IOException e) { System.out.println("Unable to read warandpeace.txt!"); fail(); } } @Test public void testCollectWords() { try { List words = StreamExercises.collectWords(Paths.get("warandpeace.txt")); assertEquals(17465, words.size()); assertEquals("a", words.get(0)); assertEquals("accomplished", words.get(100)); assertEquals("adjoining", words.get(200)); assertEquals("ambled", words.get(500)); assertEquals("burdening", words.get(2000)); assertEquals("elevate", words.get(5000)); assertEquals("moscovites", words.get(9876)); assertEquals("sundays", words.get(15000)); } catch(IOException e) { System.out.println("Unable to read warandpeace.txt!"); fail(); } } }



Step by Step Solution

3.51 Rating (151 Votes )

There are 3 Steps involved in it

Step: 1

Solution for the above question is import javaioIOException import javaniofileFiles import javaniofilePath import javautilArrays import javautilList i... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Management

Authors: Richard L. Daft

9th Edition

0324595840, 978-0324595840

More Books

Students also viewed these Programming questions