Question: This last lab teaches you to think and solve problems in the functional programming framework of the Java 8 computation streams . Therefore in this
This last lab teaches you to think and solve problems in the functional programming framework of the Java 8 computation streams. 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. All computation must be implemented using only computation streams and their operations!
In this lab, we also check out the Java NIO framework for better file operations than those offered in the old package java.io and the class File. Your methods receive a Path as an argument, referring to the text file in your file system whose contents your methods will then process with computation streams. Write both of the following methods inside a new class named StreamExercises.
public int countLines(Path path, int thres) throws IOException This method should first use the utility method Files.lines to convert the given path into an instance of Stream
public List
This method should also use the same utility method Files.lines to first turn its parameter path into a Stream
*TEST FILE*
import java.util.*; import static org.junit.Assert.*; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.nio.file.*; import java.io.*; public class StreamExercisesTest { private static int TRIALS = 10000; private static int SEED = 9999; @Test public void testCountLines() { StreamExercises se = new StreamExercises(); try { assertEquals(se.countLines(Paths.get("warandpeace.txt"), 70), 21079); } catch(IOException e) { System.out.println("Unable to read warandpeace.txt!"); assertTrue(false); } } @Test public void testCollectWords() { StreamExercises se = new StreamExercises(); try { List words = se.collectWords(Paths.get("warandpeace.txt")); assertEquals(17465, words.size()); assertEquals("accomplished", words.get(100)); assertEquals("sundays", words.get(15000)); } catch(IOException e) { System.out.println("Unable to read warandpeace.txt!"); assertTrue(false); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
