CS 160, Fall 2013
Programming Assignment P5
File Statistics
Programming due Monday, Nov. 11 at noon; late deadline Nov. 11 at 10 p.m.
A Program to Gather File Statistics
This programming assignment has five objectives:
- to practice file input using a Scanner,
- to practice file output using a PrintWriter,
- to define and use class variables,
- to write methods that take arrays as parameters,
- and to use the Character wrapper class.
Description
The program takes the name of an input and output file on the command line. It reads the
input file into an array of strings, then gathers file statistics on the contents of the
file, and finally writes the statistics out to another file.
Instructions
For this assignment, you must follow directions exactly. Create a P5 project in Eclipse
then write a class P5 with the following components:
- Declare an array of type String as a class (static) variable and allocate 100 entries.
- Declare the following integers as class (static) variables: numberLines, numberWords, numberChars, numberUpper,
numberLower, numberDigits, numSpaces, numberTabs, numberSpecial, and initialize all of them to zero.
- Write a main program as follows:
- First, call the readFile method, passing the first program argument as a parameter.
- Second, call the gatherStatistics method, passing the String array as a parameter.
- Third, call the writeFile method, passing the the second program argument as a parameter.
- All methods are static and private, and have no return values.
- Implement readFile to open the specified file and read all its lines into to the String array class variable.
- The readFile method should also correctly set the numberLines variable to the number of lines read.
- Implement gatherStatistics with one parameter which is an array of Strings.
- Implement an outer loop in gatherStatistics to iterate the String array so you can examine each line.
- Figure out the number of characters in the line, and add it to numberChars.
- Figure out the number of words in the line, and add it to numberWords.
- Note: The StringTokenizer class may be especially useful for this, we will discuss it in class.
- Implement an inner loop in gatherStatistics to iterate each character in the current line.
- To this inner loop, add code to analyze each character, updating the associated counts:
- If the character is an upper case letter ('A'-'Z'), increment numUpper.
- If the character is a lower case character ('a'-'z'), increment numLower.
- If the character is a digit ('0'-'9'), increment numDigits.
- If the character is a space, increment numSpaces.
- If the character is a tab, increment numTabs.
- If the character is none of the above, increment numSpecial.
- Note: The Character wrapper class may be especially useful for this, we will discuss it in class.
- Implement writeFile to open the specified file and write the file statistics as shown below.
- The example below shows the correct output for the Declaration.txt, which you should use as the test input file.
Number of Lines: 42
Number of Words: 1335
Number of Characters: 8113
Number of Uppercase: 231
Number of Lowercase: 6374
Number of Digits: 5
Number of Spaces: 1305
Number of Tabs: 26
Number of Special: 172
Specifications
Your program must meet the following specifications:
- Use only integers as counters, there is no floating point in this assignment.
- Use variables to store everything, don't try to do all the calculation in a print statement!
- Do not hard code values, we will test your program with a different input.
- Do not hard code file names, we will test your program with different file names.
- Work on your own, as always.
- The name of the source code file must be exactly P5.java.
- Name the file exactly - upper and lower case matters!
- Don't forget the comment block at the top of the program.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java 1.5 or 1.6 or 1.7.
- Make sure your code runs on machines in the COMCS 120 lab.
- Submit your program to the Checkin tab as you were shown in the recitation.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
- compileTest: checks that program compiles. (0 points)
- Using the provided file "Declaration.txt" test the following:
- test1: checks the "Number of Lines: " prompt and line count. (5 points)
- test2: checks the "Number of Words: " prompt and word count. (5 points)
- test3: checks the "Number of Characters: " prompt and character count. (5 points)
- test4: checks the "Number of Uppercase: " prompt and uppercase count. (5 points)
- test5: checks the "Number of Lowercase: " prompt and lowercase count. (5 points)
- test6: checks the "Number of Digits: " prompt and digits count. (5 points)
- test7: checks the "Number of Spaces: " prompt and spaces count. (10 points)
- test8: checks the "Number of Tabs: " prompt and tabs count. (10 points)
- test9: checks the "Number of Special: " prompt and special character count. (10 points)
- Spelling or punctuation errors will cause you to fail tests, use preliminary testing to check!
- Final Tests
- Using a file of unspecified name and contents that we provide, test the following:
- test10: checks the "Number of Words: " prompt and word count. (5 points)
- test11: checks the "Number of Characters: " prompt and character count. (5 points)
- test12: checks the "Number of Uppercase: " prompt and uppercase count. (5 points)
- test13: checks the "Number of Lowercase: " prompt and lowercase count. (5 points)
- test14: checks the "Number of Digits: " prompt and digits count. (5 points)
- test15: checks the "Number of Spaces: " prompt and spaces count. (5 points)
- test16: checks the "Number of Tabs: " prompt and tabs count. (5 points)
- test17: checks the "Number of Special: " prompt and special character count. (5 points)
- Final grading includes the preliminary tests.
Submit your program to the Checkin tab on the course website, as you were shown in
the recitation, and read the syllabus for the late policy (if necessary).
© 2013 CS160 Colorado State University. All Rights Reserved.