In this assignment, you will write a Java program that counts the 26 letter frequencies for a given string. You may name your Java file name as LetterCounter ArrayApp.java, and write methods that use arrays to store the frequencies. The detailed descriptions are as follows: • Method 1: countLetters() /* * Return an array of 26 letters’ frequencies for a given string * * Note: You only need count the frequency for 26 letters * i.e., "abcdefghijklmnopqrstuvwxyz". * you should count both uppercase and lower case letters */ public static int [] countLetters(String str) { // your work } • Method 2: printLetterFreq() /* * Print the letters, their frequencies, and frequency representations * @countArray: an array of 26 letters frequencies in an alphabetic order * * Note: Only print those frequencies > 0. */ public static void printLetterFreq(int [] countArray) { // your work } 1 • Method 3: numToStar(int num) /* * Return a string with a number of stars (*) corresponding to the input * "num", For instance, if num = 5, then you should return "*****" * * Note: This method will be invoked in the method of printLetterFreq() */ public static String numToStar(int num) { // your work here } • Method 4: mostFreqLetter(). /* * Find the most frequent letter in all 26 letter frequencies. * @countArray: an array of 26 letters frequencies in an alphabetic order * * Note: If there are more than one letters with highest frequencies, * then return the first most frequent letter. */ public static char mostFreqLetter(int [] countArray) { // your work here } In your main() method, you apply these methods on each of four strings stored in a file inputstring.txt, and display the letter frequencies for each string(See a sample screenshot of the first input string). Particularly, you should do the following • Declare a string variable, and initialize it using the first string from inputstring.txt (Hint: Just copy the string from the file and paste it in your program); • Compute the 26 letter frequencies using the method of counteLetters(); • Display the 26 letter frequencies using the method of printLetterFreq(); • Display the most frequent letter using the method of mostFreqLetter(); • Repeat the above steps on the second, third and fourth string from the input file. 2 2 Additional Requirements 1. Your program should have good style (indentation, whitespace, comments, vertical alignment, ...). 2. Your program outputs should be neat (See the screenshot of the first one shown in Figure 1). Particularly: • Only print out letters with some numbers of frequencies. In other words, don’t print out the letters with zero frequencies. • The letter frequencies should be placed inside the parenthesis.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

In this assignment, you will write a Java program that counts the 26 letter frequencies for a given string. You may name your Java file name as LetterCounter ArrayApp.java, and write methods that use arrays to store the frequencies. The detailed descriptions are as follows: • Method 1: countLetters() /* * Return an array of 26 letters’ frequencies for a given string * * Note: You only need count the frequency for 26 letters * i.e., "abcdefghijklmnopqrstuvwxyz". * you should count both uppercase and lower case letters */ public static int [] countLetters(String str) { // your work } • Method 2: printLetterFreq() /* * Print the letters, their frequencies, and frequency representations * @countArray: an array of 26 letters frequencies in an alphabetic order * * Note: Only print those frequencies > 0. */ public static void printLetterFreq(int [] countArray) { // your work } 1 • Method 3: numToStar(int num) /* * Return a string with a number of stars (*) corresponding to the input * "num", For instance, if num = 5, then you should return "*****" * * Note: This method will be invoked in the method of printLetterFreq() */ public static String numToStar(int num) { // your work here } • Method 4: mostFreqLetter(). /* * Find the most frequent letter in all 26 letter frequencies. * @countArray: an array of 26 letters frequencies in an alphabetic order * * Note: If there are more than one letters with highest frequencies, * then return the first most frequent letter. */ public static char mostFreqLetter(int [] countArray) { // your work here } In your main() method, you apply these methods on each of four strings stored in a file inputstring.txt, and display the letter frequencies for each string(See a sample screenshot of the first input string). Particularly, you should do the following • Declare a string variable, and initialize it using the first string from inputstring.txt (Hint: Just copy the string from the file and paste it in your program); • Compute the 26 letter frequencies using the method of counteLetters(); • Display the 26 letter frequencies using the method of printLetterFreq(); • Display the most frequent letter using the method of mostFreqLetter(); • Repeat the above steps on the second, third and fourth string from the input file. 2 2 Additional Requirements 1. Your program should have good style (indentation, whitespace, comments, vertical alignment, ...). 2. Your program outputs should be neat (See the screenshot of the first one shown in Figure 1). Particularly: • Only print out letters with some numbers of frequencies. In other words, don’t print out the letters with zero frequencies. • The letter frequencies should be placed inside the parenthesis.

Expert Solution
Step 1

Code:

import java.util.*;
import java.io.*;

class LetterCounterArrayApp
{
   // Method 1: countLetters() 
   /* * Return an array of 26 letters’ frequencies for a given string * 
   * Note: You only need count the frequency for 26 letters *
   *  i.e., "abcdefghijklmnopqrstuvwxyz". * 
   * you should count both uppercase and lower case letters 
   * */ 
   public static int [] countLetters(String str) 
   { 
       //Declare letters array to store frequencies of alphabets
       int[] letters = new int[26];
       //Declare varaiable "ch"
       int ch;

       //Converting to Lower case
       str = str.toLowerCase();
      
       //Iterating over string
       for(int i=0; i<str.length(); i++)
       {
           ch = (int)str.charAt(i);
           if(ch>=97 && ch<=122)
               //Extracting each character and finding its position
               letters[ ch - 97 ] += 1;
       }
      //return letters array
       return letters;
    } 

    // Method 2: printLetterFreq()
    /* * Print the letters, their frequencies, and frequency representations *
     *@countArray: an array of 26 letters frequencies in an alphabetic order * 
     * Note: Only print those frequencies > 0. 
     * */ 
    public static void printLetterFreq(int [] countArray) 
    {
        //Traversing array
       for(int i=0; i<26; i++)
       {
           //Checking value at ith location of array
           //If value is greater than 0 then print statemt executed
           if(countArray[i] > 0)
               System.out.print((char)(i+97) + " ( " + countArray[i] + " ) :" + numToStar(countArray[i])+"\n");
       }
    } 

    // Method 3: numToStar(int num)
     /* * Return a string with a number of stars (*) corresponding to the input * 
     * "num", For instance, if num = 5, then you should return "*****" *
     *  Note: This method will be invoked in the method of printLetterFreq() 
     * */ 
    public static String numToStar(int num) 
    {
        //Declare empty string "str"
        String str="";
       //Looping from 0 to "num"
       for(int i=0; i<num; i++)
       {
           //Appending string "str" with "*"
           str = str + "*";
       }
       //return str
       return str;
    } 

    // Method 4: mostFreqLetter().
     /* * Find the most frequent letter in all 26 letter frequencies. *
     * @countArray: an array of 26 letters frequencies in an alphabetic order * 
     *  Note: If there are more than one letters with highest frequencies, * 
     * then return the first most frequent letter. 
     * */ 
    public static char mostFreqLetter(int [] countArray) 
    { 
        //Initially assume that starting value is maximum
       int most_freq = 0;
      
       //Iterating over countArray
       for(int i=1; i<countArray.length; i++)
       {
           //Comparing frequencies of most_freq & i
           if(countArray[most_freq] < countArray[i])
           {
               //Updating most_freq value with i
               most_freq = i;
           }
       }
      
       return ((char)(most_freq+'a')); 
    } 

   //Main method
   public static void main(String[] args) throws FileNotFoundException
   {
       //SCanner object
       Scanner sc = new Scanner(new File("inputstring.txt"));
      
       //Looping to Read strings  from file
       while(sc.hasNext())
       {
           //Reading a string from file
           String str = sc.nextLine();
          
           //Printing string
           System.out.println("\nString: '" + str + "'");
          
           //Calling countLetters() function
           int[] cnt = countLetters(str);
           //Calling printLetterFreq() function
           printLetterFreq(cnt);
           //Displaying most frequent latter by calling mostFrequentLetter() function
           System.out.println("\nMost frequent letter in string: " + mostFreqLetter(cnt) + "\n");
       }
   }
}
  
   
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 7 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY