Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Let us try one more problem that uses higher-order functions. To make it easier, we are making the problem almost identical. Only the predicate function

Let us try one more problem that uses higher-order functions. To make it easier, we are making the problem almost identical. Only the predicate function you need to write should change.

The provided code (CountQuestions.java ) reads in an input file (input.txt) and reports how many lines of that file contain question marks. Or technically, it is supposed to do that. Instead it is reporting 0.

Edit the code so that it reports how many lines have question marks. There is only one statement to write which is the body of the countQs method.

As a brief overview of the code, the main body of the code opens the file (input.txt) and reads in every line storing it in a List of Strings. This is then passed to the function countQs which is supposed to return the number of lines containing question marks. The coundQs function should (ideally) perform a functional language approach. Use the same approach that was used for countOdd to do this but the only thing you need to change is the predicate (the x -> x.length < 20) part.

If you are curious about the Stream class, check out the Java API for it.

In addition, you might find the String class to be useful, since it has a method that determines if a String contains a specific substring (or character). Check out the Java API for it.

So, the question is. In the provided input.txt file, how many lines have question marks?

CountQuestions.java :

/********** * Author: Christian Duncan * CountQuestions * * This program takes an input file called "input.txt" and reads in * the file (one line at a time). It stores this as a list of Strings * It then proceeds to call a function that returns how many lines * in the file contain question marks (?) * * For example, suppose the file had the following lines: * * ab?c * 123?4 * a b c. * last one? * * Then the lines that have question marks are: * ab?c * 123?4 * last one? * So there are 3 lines with question marks. ***********/ import java.io.FileNotFoundException; import java.io.File; import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class CountQuestions { private static String FILE_NAME = "input.txt"; // Name of the file to process public static void main(String[] args) { try { // Open the file and read in the list of Strings. ArrayList lines = new ArrayList(); Scanner in = new Scanner(new File(FILE_NAME)); while (in.hasNext()) { lines.add(in.nextLine()); } // Count how many lines have question marks and output result long result = countQs(lines); System.out.println(result); } catch (FileNotFoundException e) { System.err.println("Error: File Not Found: " + e.getMessage()); System.err.println(" Be sure that the file " + FILE_NAME + " is located in the same directory as the Java class (CountQuestions.class) file."); System.err.println(" Aborting."); } } /*** * countQs: * @param list A list of Strings * @return The number of Strings in list with question marks * This works by converting the list to a Stream * list.stream() * It then takes that stream and filters it with the * given Predicate (function that returns a boolean) * This returns the same stream but only with elements that * the predicate returned true for. * It then returns the number of elements in that stream (count()) ***/ public static long countQs(List list) { return 0; // This time, YOU WRITE the whole part. // It is nearly identical to the countOdd. // HINT: There is a method in the String class that can determine // if a String contains another String (or a character for that matter) // Check out the Java String APIs. } }

input.txt file:

Here is the input file It has lots of lines The input file includes these lines as well. Do not try to count these manually Try to write the program to do it ... It might help to make your OWN test case with a smaller input ... that you know the answer to and so can check that it is working. --- Will that help? Perhaps. AHKDDREIDYFH DLU?WXLXSCTBHE HNRWMOESHYWMSOKCIHIRY JJCXUGMSSNPNCFMDKGWME?RC AIWSMOEOIILZZZMXJGUSXPFCTI YJZRLEIARBBCLIUSKRUVIWAUUWV WJSWJPXYCOXDYV?LLAYFUDIWHMWHVWHADTSFRPS FGJWTHUVVSA?RHCLIJWLOZEROU NRTY?RAKDDHZLTIMUCVMGHMUHPVALJ DKOLRDMRYNZKJVCSWDCO ?SOZBXVGFVRRHSND?AEPYFBTWAC CSPITLVL?JOJSLAWTMNZIYJA?AGSFTDMRFRKLCJ ?YBLHTTXOOBTBMXINHXLHWUBGA RLMZRIHILYKUEPGBDCOWFJPH THUINOADSRXYMWVYC YPV?FOLSAVZTVCUKKNYLIEXFCNPKYLHEWOAE HNGWKTMJRPRKGFW NRGCANWWUHMUBKFST MKIIFNDHSWIYOXSRIPYMFS RCSCSHY?DGV YIIIXZRMRZENYESLKURGVVNTNBOX?PGEJK FUSBYYAHH?YKNSFMETRRXFIXNJJSDEMYL E?RVHPNKYNTIB DUYBPMBHSHEOTPIEMMVHYRHSS CJZZZYBVUJYMJYXUJNOXPINO GYSUPDXUXNZ?TDHLRNK?BXULDXPVKRPWZFJYF HHVMRDVKBTOJIHTDPG?UBFLEWFAB KOIVHIWHGXLSIHBTLOBDLIPWHTDVSVKAUI NWOFFAGHBFCOFBBFBC CPXOPFIMDNSMYAEXGFFGMRCPDNAFYFV YMIYORWM??ZXAABUPPFNRHX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MIX?Z VBDJUKJVJZCHTWOHOOXZPNFGPVNIYPDLVSA FGEUFOXDYOJZRDZ BPPVWCHAVGCWGOAFJGUGOKV?ERTV BLTKD?GKKCNHPEXYABCAJZYDC?ES ZNMIZWPYACS YSZUYFBNOPKKISUZYMRWYMUTSOWVHJPUYFYRDA ?RGDXJDJAPOCFIYA?UYZUOMBRMG KYMOF?YXDOBMSIEK AHXBBEZYDWW LWLPOCEUVNVALESYTXSALMKNZAWJVLX? BHERVTA?BJKT?DKSBDZGSTMMV?LD UUCIYDSNNRLFMCAIFHPRFDEGFXRVI?UR VYZDXVNAXPESYJOLVECNMCGVWMKYNJD AOARMMNTOKSTFJMHVYSOJZMOIVBIG FTENALTFLIWWRHBF LRFB?WLVIXMBJG?NNANHXPINPWXOYY LNSPVZLFWVKBWDRURK?YGVSSN GXDDGUGATOXPZOGHSCESBLOCVSBZJ RRKTCVHCYVLMYFG DLY?WZE?JERUAITARFBRKWJPXVXEKBZALPRPUI VULTAXBKCZCVDGCUDROIIHPCYJPMDXT ZMDZCWPKBTBFNCFXALLVYPEZCYES LYGRGZOUUWZAUNTXUDWFOVBGN?E?WBG WTXCMCMISOZJCNIDHJKCVILA

Step by Step Solution

There are 3 Steps involved in it

Step: 1

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

Step: 3

blur-text-image

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

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions