Due: Tuesday, 9 June 2009 at 11:59 PM
Write a program that prompts the user to enter a sentence from the keyboard using JOptionPane.showInputDialog.
The program will print the characters back with the first letter of each word changed from lower case into upper case. If you have a capital letter in the original line and it is not the first letter of a word, then this letter should be switched from upper case to lower case. The only capital letters that should appear in the line must be the begining letter of every word in the line. All other characters will remain the same. At the end, the program will print a summary including:
- The original sentence
- The number of upper case letters in the original sentence (before any changes)
- The number of lower case letters in the orginal sentence (before any changes)
- The number of blank spaces in the sentence.
- The number of other characters (i.e., not letters or spaces) in the sentence.
- The total number of characters in the sentence.
- The modified sentence.
If the user types in the following string:
This is JUst a Sample Run of the stupid homeWork.
Then the program should print out the following:
Original Sentence: This is JUst a Sample Run of the stupid homeWork.
Upper case letters : 6
Lower case letters : 33
Blank spaces : 9
Other characters : 1
Grand total : 49
Changed Sentence: This Is Just A Sample Run Of The Stupid Homework.
Extra Credit (5 points):Extend your program so that the following words are not capitalized in the output unless they appear as the first word:
a, an, and, are, in, is, of, theThe above example would become:
But if the input were
This is Just a Sample Run of the Stupid Homework.
the changed sentence would be
the extra credit is OK I guess.
The Extra Credit is Ok I Guess.
Hint and Notes:
- Use the String.charAt(position) method to break the string into individual characters within a loop structure.
- Also use the String.length() method to get the length of your string
- Lower case letters are within a range of characters, as are upper case letters based on ASCII values. See Appendix B in the textbook. A space has an ASCII value of 32. Do not write a gigantic switch statement with a case for each lower case letter and a case for each upper case letter. You should be able to test for the characters based on the ranges of values presented in the ASCII table in appendix B.
- Extra credit for excellent work!
- You should use comments throughout the source code to explain key steps and calculations
- Choose intuitive names for variables
- You should use proper indentation (2-4 spaces) and blank lines to make your program easy to read.
- Do not mix tab and space characters in your indentation—configure your editor to replace tabs with spaces. (Instructions here.)
- Do not exceed 80 characters per line.
Your assignment should be submitted BY EMAIL
to our class grader:
- Name your .java file
Lastname3.java. For example:
- Attach your .java file—and only your .java file—to an email with the subject line "Homework #3".
- Each .java file you submit should begin with a comment of the form:
/********************************** * [NAME OF YOUR PROGRAM AND A SHORT DESCRIPTION OF ITS PURPOSE] * Written by [YOUR NAME] * Date: [MONTH DAY, YEAR] * NYU ID: [YOUR NYU NETID] **********************************/Any submission without such a comment will lose 1 point for style.