Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Two roads diverged in a yellow wood , And sorry I could not travel both And be one traveler , long I stood And looked

Two roads diverged in a yellow wood , And sorry I could not travel both And be one traveler , long I stood And looked down one as far as I could To where it bent in the undergrowth ; Then took the other , as just as fair , And having perhaps the better claim Because it was grassy and wanted wear ; Though as for that , the passing there Had worn them really about the same , And both that morning equally lay In leaves no step had trodden black . Oh , I marked the first for another day ! Yet knowing how way leads on to way I doubted if I should ever come back . I shall be telling this with a sigh Somewhere ages and ages hence : Two roads diverged in a wood , and I , I took the one less traveled by , And that has made all the difference .

Provide the Java code for these requests. STARTING with the code BELOW. The poem to be used in the assignment is in file "poem1.txt" and its contents is copied ABOVE.

public class WordCount { public static void tokenCount(String text) { java.util.Scanner scan; // open the file for reading using Scanner try { scan = new java.util.Scanner(new java.io.File(text)); } catch(java.io.FileNotFoundException e) { scan = null; } // process the file, token by token int i = 0; while(scan.hasNext()) { String token = scan.next(); i ++; } // close the file scan.close(); // output statistics System.out.println("Token count: " + i); System.out.println(); } public static void main(String[] args) { tokenCount(args[0]); } }

Instructions

The task is to alter WordCount so that it counts the number of instances of each of the types in the given file. This is a good task for a Map. Clearly, the key for the map will be the types, that is, the words from the poem. The value will be the count of occurrences of the types. Now, we must use a reference type for the generic formal parameter. Use the Counter class that was used when introducing class structure in Unit 3.

Minimal Version

Update WordCount so that it uses java.util.HashMap, which is an implementation of java.util.Map, to count the types in the input file. The words should be counted ignoring case. That is, "The" and "the" should be counted together.

Print out the counts of tokens in the input file, count-colon-type. Only print out counts which are greater than two. The order that the key-value pairs are printed does not matter. The output should include a header line giving the name of the input file and the token count. A blank line after the type counts will aid in readability.

Let's say we have a file numbers.txt:

one One Two oNe tWo tHrEe ONE TWO THREE FOUR

If we run this file through WordCount:

java WordCount numbers.txt

would give the following output:

numbers.txt: 10 tokens 4 : one 3 : two

It is just a happy coincidence that the keys print out in alphabetical order. [fodder: What does the adjective happy mean here?]

Please note: in this output, the counts are right justified, that is, the ones-column values are aligned. Also note that the counts are printed count-space-colon-space-type. For alignment purposes, you may assume that none of the counts will be larger than 9,999.

Here is another example: Note: this is a partial output, that is, not all the types are shown; also, the order of the types has been altered.

poems1.txt: 161 tokens 4 : in 9 : and 3 : that 3 : . 10 : , 5 : as 3 : one

Please note that the Minus Version of this application is very "fragile" ... it will crash ungracefully if it is not given an appropriate filename. You will fix this shortcoming in the Standard Version.

Standard Version

There are some enhancements for the standard version.

Answering some questions about program design.

Enhancing the file processing

Questions about the Starting-Point Code

Answer the following questions, including the answers as part of the report for this assignment.

How does this program know the name of the file to be opened?

How does this program deal with an error in the filename?

Hint: Trying to open a non-existent file will throw a FileNotFoundException.

Enhanced File Handling

Add handling for a variable number of filenames on the command line. If no filename is provided, print out an error message and exit the program. If multiple filenames are given on the command-line, process through each of the command-line arguments. The counts reported should be separate for each file.

Also, add handling for files which cannot be opened. Currently, the program will crash if a bogus filename is given. Update the code to report the error and continue processing, rather than crashing with an unhandled exception.

For example, if the command line was:

java WordCount numbers.txt junk.txt numbers.txt

the generated output should be:

numbers.txt: 10 tokens 4 : one 3 : two

junk.txt cannot be opened

numbers.txt: 10 tokens 4 : one 3 : two

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

The Database Experts Guide To SQL

Authors: Frank Lusardi

1st Edition

0070390029, 978-0070390027

More Books

Students also viewed these Databases questions