CS 150, Fall 2017
Programming Assignment - P9
File Statistics - Gettysburg Address
Due - November 14th, 2017 at 6:00pm
Late - November 15th, 2017 at 8:00am
Objectives of this Assignment
- To write a program which performs file input/output,
- calculate file statistics on a given text file, and
- review some of the Character wrapper class methods.
Create a Java program called P9 with a class named P9, which after created should contain a file named P9.java in the src folder. Please complete the code required from this week's lab first if you have not already done so. All of the Java code you write should be contained in the main method. In addition to determining the number of lines and words in the Gettysburg address file, in P9 you should also determine the following: number of characters, number of uppercase letters, number of lowercase letters, number of digits, number of spaces, and number of special characters.
- If you still need to import the text file to P9, right on the file here and click either 'Save as' or 'Download link as'. Then save the file to your P9 project directory. Do not save to the src or bin folder.
- First, declare and initialize six more integer variables to 0.
- Declare and initialize a String to the empty string.
- Declare a character variable.
- You will now need to add a block of code to determine the rest of the file statistics. This code will go after the nested while loops, but before your code that writes data to the output file.
- Write a for loop that iterates over all of the elements in the String array.
- First, assign the line at the current index to your String object.
- Next, write an if statement that breaks the loop if the String is null. Without this check, what exception will be generated?
- After the if statement, add the length of the String to your character count variable.
- Now, write an inner for loop to determine the type of characters in each String.
- First, the inner for loop should assign a single character from the String object to your character variable from step 4. This single character will then be sent through several conditionals.
- Following this line of code, write several conditionals that utilize methods of the Character wrapper class. If the character is of a certain type, increment the appropriate integer variable. One type of character you should be counting will be the default case (i.e. for which type of character is there not a wrapper class method?).
- You are now ready to print six more lines to the output file for this project, Statistics.txt.
- First, using your PrintWriter object, print the following to the file:
Number of Characters:
followed by your associated variable.
- Next print:
Number of Uppercase:
followed by the appropriate variable.
- The next four are as follows, and each print statement should include the variable with the associated statistic.
Number of Lowercase:
Number of Digits:
Number of Spaces:
Number of Special:
- Make sure the last print statement includes a new line.
- If you have not already done so, close your Scanner and PrintWriter objects.
Output File Contents
Number of Lines: 21
Number of Words: 274
Number of Characters: 1475
Number of Uppercase: 13
Number of Lowercase: 1146
Number of Digits: 6
Number of Spaces: 270
Number of Special: 40
- Work on your own, as always.
- The name of the source code file must be exactly
- Name the file exactly - upper and lower case matters!
- Comments at the top as shown above.
- Assignments should be implemented using Eclipse.
- Assignments should be implemented using Java, version 1.8.
- Make sure your code runs on machines in the CSB 120 lab.
- Turn in through the Checkin tab.
- Read the syllabus for the late policy.
- We will be checking programs for plagiarism, so please don't copy from anyone else.
- 100 points for perfect submission.
- 0 points for no submission, will not compile, submitted class file, etc.
- Preliminary Tests
Preliminary testing tests lines 1-5 .
- testCompile: checks that program compiles. (0 points)
- lineTests: verify correct output based on String method used.
- Final Tests
Final testing tests lines 6-8.
- Final grading includes the preliminary tests.
Submit P9.java to Checkin.