Question: Java Objective Write a program that reads in lines from the input. For every line, it counts each letter of the English alphabet. It also
Java
Objective
Write a program that reads in lines from the input. For every line, it counts each letter of the English alphabet. It also counts the number of vowels, consonants, and others. It then prints out this information.
The important part of this assignment is to code the methods given below and to use them. You will need to create and use an array that keeps track of the count for each letter.
Input
The input will be one or more lines of characters that have been typed in by the user. Assume that all the letters are all LOWERCASE.
Output
The output can be best described using some examples:
Example output:
Line 01: d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 consonants=9 others=0
The line number is followed by number of non-zero occurrences of each letter in the English alphabet. This is followed by a YES or NO based on if there were ANY vowels in the line. Next, it shows the number of vowels, followed by the number of consonants, followed by others (i.e. not a letter).
Example output:
Line 02: h=2 r=3 w=2 NO vowels=0 consonants=7 others=10
Note that in this example, there are NO vowels. Note that the line number uses TWO spaces and that for ONE digit numbers, the left space has a ZERO. Thus, Line 9 will be printed as Line 09: but Line 99 will be printed as Line 99:
Note that it prints the letter counts in alphabetical order.
Sample Input
hevdhfewfewfe
hhwwrrr73##$6%&%7
Sample Output
Line 01: d=1 e=4 f=3 h=2 v=1 w=2 YES vowels=4 consonants=9 others=0
Line 02: h=2 r=3 w=2 NO vowels=0 consonants=7 others=10
Now that you know what the program is to do, let us give specifics of what you are to do.
1. You will need to declare a static int array variable that will keep track of the counts for each letter. This array should have length 26. Array Index 0 will be used to store count for the letter 'a'. Array Index 1 will be used to store count for the letter 'b'. Array Index 25 will be used to store count for the letter 'z'. Suppose the array is named letterCountArray. Then to access the index for character c, you will need to do c 'a'. Thus, if c is 'm', your code will access index 'm' 'a' - which is 12. Note that 'a' is 97 in Unicode (and ascii). This array will need to zeroed out when you begin with a new line. This array should be declared as a CLASS variable (and not inside the main method). This way, this variable can be used inside any method in the class.
2. private static void addLetterCount(char c)
This method is to add to the count for the letter indicated by the variable c. Basically, index into the array declared above and add to the count for the letter indicated by variable c.
3. private static void zeroLetterCount()
Zero out all the elements of the array.
4. private static void printLetterCounts()
Loop over the array and print the counts for each letter (as indicated in the sample output). Thus, if there are only two counts that are not zero (say 'a' has count 5 and 'z' has count 10, then this method will print "a=5 z=10 " (note the space after 10).
5. private static int countVowels()
This method returns the sum of the count of all the vowels (i.e. for 'a', 'e', 'i','o','u').
6. private static String anyVowels()
This method returns the String "NO" if countVowels() returns 0. Otherwise, it returns the String "YES"
7. private static int countConsonants()
This method returns the sum of counts of all the consonants. (Just add all elements of the array and subtract countVowels(). COM S 207 CountCharsA
8. private static void counts(int lineNum, String s)
This method is the "workhorse" of the program. It is given a line number and the line as input. It first loops over each character of the line and adds to the appropriate array element if it is a letter. Otherwise, it adds to "Other". Then, it prints the information for the line as described earlier. To print the information, make use of the methods described earlier. Use printf with formal "Line %02d: " to be able to print the line number with LEADING zeroes.
9. main method.
In the main method, keep count of line number and make calls to "counts" method as needed.
Thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
