Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that reads in lines from the input. For every line, it counts ea

ID: 657247 • Letter: W

Question

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.You need to create and use and array that keeps track of the count for each letter.

The input will be one or more lines of characters that have been typed in by the user. Assume that all the letters are LOWERCASE.

The output can be desired by these 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 let space has a ZERO. Thus, Line 9 will be printed Line 09: but Line 99 will be printed as Line 99:

Note that is 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

Specifics on what 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 be 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 zerroLetterCount()

Zero out all the elements of the array.

4. private static void printLetterCounts()

Loop over the array and print the counts for each letter. 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().

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 the line number and make calls to "counts" method as needed.

Explanation / Answer

import java.util.Arrays;
import java.util.Scanner;
public class COUNTMETHODS
{
   static int letterCountArray[] = new int[26];

   private static void addLetter(char c)
   {
       letterCountArray[c-'a']++;
   }
   private static void zeroLetterCount()
   {
       for(int i=0;i<26;i++)
       {
           letterCountArray[i]=0;
       }
   }
   private static void printLetterCounts()
   {
       for(int i=0;i<26;i++)
       {
           if(letterCountArray[i]!=0)
           {
               System.out.printf("%c=%d ",i+'a',letterCountArray[i]);
           }
       }
   }
   private static int countVowels()
   {
       int sumi=0;
       sumi+=letterCountArray['a'-'a'];
       sumi+=letterCountArray['e'-'a'];
       sumi+=letterCountArray['i'-'a'];
       sumi+=letterCountArray['o'-'a'];
       sumi+=letterCountArray['u'-'a'];
       return sumi;
   }
   private static String anyVowels()
   {
       if(countVowels()>0)
           return "YES";
       else
           return "NO";
   }
   private static int countConsonants()
   {
       int sumi=0;
       for(int i=0;i<26;i++)
           sumi+=letterCountArray[i];
       return sumi- countVowels();
   }
   private static void counts(int lineNum, String s)
   {
       int other=0;
       zeroLetterCount();
       for(int i=0;i<s.length();i++)
       {
           if(Character.isLowerCase(s.charAt(i)))
           {
               addLetter(s.charAt(i));
           }
           else
           {
               other++;
           }
       }
       System.out.printf("Line %02d: ",lineNum);
       printLetterCounts();
       System.out.print(anyVowels()+" ");
       System.out.printf("vowels=%d consonants=%d others=%d ",countVowels(),countConsonants(),other);
   }
   public static void main(String[] args)
    {
       int lineNum=1;
        Scanner lineReader=new Scanner(System.in);
        while(lineReader.hasNextLine())
        {
           counts(lineNum,lineReader.nextLine());
           lineNum++;
        }
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote