Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project files For this assignment you are going to implement two classes. TextStatistics will be the class that reads a text file, parses it, and

Project files

For this assignment you are going to implement two classes. TextStatistics will be the class that reads a text file, parses it, and stores the information about the words and characters in the file. ProcessText is the driver class that gets a list of one or more filenames from the command line and collects statistics on each of the files using an instance of the TextStatistics object.

Classes that you will create: ProcessText.java, TextStatistics.java

Existing interface and class that you will use: TextStatisticsInterface.java, TextStatisticsTest.java

You should be able to develop this program incrementally in such a way that you can turn in a program that runs even if you don't succeed in implementing all the specified functionality.

ProcessText.java

The driver class with the main method which processes one or more files to determine some interesting statistics about them.

Command-line validation

The names of the files to process will be given as command line arguments. Make sure to validate the number of command line arguments. There should be at least one file name given.

If no files are given on the command line, your program must print a usage message and exit the program immediately. The message should read as follows.

Usage: java ProcessText file1 [file2 ...] 

This lets the user know how they should run the program without having to go look up the documentation.

Processing command-line arguments

If valid filenames are given on the command line, your program will process each command line argument by creating a File object from it and checking to see that the file actually exists.

If a file does exist, your program will create a TextStatistics object for that file and print out the statistics for the file to the console.

If a file does not exist, a meaningful error message needs to be printed to the user. Continue processing the next file. An invalid file in the list should not result in the program crashing or exiting before all files have been processed.

The example, CmdLineArgs.java, shows how to use command line arguments in your program. The args parameter of the main method is an array of String objects that contains the command line arguments to the program. For your program, the array should contain the names of the files to be processed.

TextStatistics.java

An instantiable class that reads a given text file, parses it, and stores the generated statistics.

Implement the Interface

Your TextStatistics class must implement the given TextStatisticsInterface (don't modify the interface, it just provides a list of methods that your class must include).

To implement an interface, you must modify your class header as follows

public class TextStatistics implements TextStatisticsInterface { } 

Adding "implements TextStatisticsInterface" will cause an error in Eclipse. Select the quick fix option to "Add unimplemented methods" and it will stub out the required methods for you.

Instance variables

Include a reference to the processed File. Include variables for all of the statistics that are computed for the file. Look at the list of accessor methods in the TextStatisticsInterface to determine which statistics will be stored.

Constructor

Takes a File object as a parameter. The constructor should open the file and read the entire file line-by-line, processing each line as it reads it.

You should only have to read through each file once if you are doing this program properly. By the end of the constructor, the TextStatistics object should have collected all of its statistics and calls to its accessor methods will simply return the stored values.

Your constructor needs to handle the FileNotFoundException that can occur when the File is opened in a Scanner. Use a try-catch statement to do this. Don't just throw the exception.

As each line is read, collect the following statistics:

The number of characters and lines in the file. The number of characters should include all whitespace characters, punctuation, etc. The number of lines should include any blank lines in the file.

The number of words in the file.

You must use a Scanner on each line to count the number of words in each line of the text file.

To ensure everyone's results are consistent, you must use the exact delimiter given below rather than making up your own.

private static final String DELIMITERS = "[\\W\\d_]+"; 

Use useDelimiter(DELIMITERS) on your line Scanner to set the delimiters that the Scanner will use for separating words in the file.

The scanner will not return any of the delimiter characters. For example, using lineScan.next() on the string.

scheme, and the "plan" (for us) 

will give the following tokens.

scheme and the plan for us 

A note on words: Using the above delimiters might lead to strange words occasionally. For example, the contraction weve results in two words we and ve. This is okay and your program does not have to do anything to fix this.

The UseScannerDelimiter.java example shows the different results you get using the default delimiters and user-specified delimiters.

The number of words of each length that appears in the file. Assume that the maximum word length is 23. You do not need to print lengths that have a count of zero.

The average word length for the file.

The number of each letter that appears in the file - do not separate upper and lower case, just convert all characters to lower case before counting.

See LetterCount.java for a similar approach.

Getter (accessor) methods

Implement the accessor methods for the number of characters, number of words, number of lines, average word length and for the arrays that contain the number of words of each length and the number of times each letter occurs in the file.

If you implemented the interface correctly, you should already have methods for these. Just make sure they are returning the correct values. If you are doing your program correctly, most (if not all) of your accessor methods will just contain a single return statement.

toString method

Write a toString() method that generates and returns a String that can be printed to summarize the statistics for the file as shown in the sample output.

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

Learn To Program Databases With Visual Basic 6

Authors: John Smiley

1st Edition

1902745035, 978-1902745039

More Books

Students also viewed these Databases questions

Question

Strengthen your personal presence.

Answered: 1 week ago

Question

14-18 Compare the two major types of planning and control tools.

Answered: 1 week ago