In-Class Project
Class WordCount
Program Name: WordCount.java
Create a java class named WordCount counts the words in a file. See #1 under
Programming Projects in your text. The program is going to count the number of
words, lines, and total characters (not including white space) in the file. Assume that
consecutive words are separated either by spaces or the end-of-line character.
Your program is going to have 2 methods:
Your main method should:
- Prompt the user for the name of the file to be examined
- Echo the file name.
- Create a new Scanner object for the file.
- Call examineFile passing the file scanner object
examineFile takes a file scanner object as a parameter. It should:
- Loop while data remains in the file
- Read a line from the file
- Create a line Scanner object
- Parse the line counting the number of words and non-whitespace characters
- After processing each line in the file, print the statistics:
- Number of lines
- Number of words
- Total characters
- Average word length
turnin: WordCount
Out-of-Class Projects
1. CompareFiles
Program Name: CompareFiles.java
SEE #2 Under Programming Projects in your text. Write a program that compares two files and prints out information about the differences between them. See the example in the text for
the format of the output.
If there are no differences, print a message that there are no differences.
Your program should have 2 methods:
- main method
- compareFiles
To improve the modularity of your code, you may break this method down further.
main method:
- Create a Scanner object for reading from the keyboard.
- Create 2 Scanner objects, one for each file.
- Call compareFiles passing the 2 file Scanner objects.
compareFiles: takes 2 Scanner file objects.
- Loop until one of the files is empty, counting line numbers.
- Read the next line from each file.
- Compare the 2 lines read. If different, print the line number and each line (see text). If
they are the same, print a message that they are the same.
turnin CompareFiles
2. ReWrite
ReWrite.java
Modify the WordCount.java you wrote for the In-class project.
- main method will also prompt the user for the name of an output file. Echo the file name.
The main method will call examineFile passing the input file as well as the outputfile.
- examineFile will be modified to take two file parameters. When it reads a line from
the input file, it should create a new String from the line read with "!!" appended
to the line. It should write the newly created String to the output file. It should then
to proceed to calculate the statistics o the original String read.
turnin: ReWrite
This page is maintained by Barbara
Bracken Last Modified 8/5/2022