Homework Solution: The main goal of this first homework is to complete a relatively straightforward Jav…

    The main goal of this first homework is to complete a relatively straightforward Java program by taking advantage of your Java knowledge. Problem description:  Given a string variable String s; , initialize s such that it contains a paragraph in English text. You can do so within your program by reading the initial value from the keyboard by using the Scanner class. Furthermore, this paragraph consists of no more than 100 tokens. Tokens are sequences of contiguous characters separated by any of the specified delimiters (e.g., white spaces, commas(,) and period(.)). Please implement a Java program to perform the following two tasks on s: Implement the function void getLetterFreq(string s); to identify the frequency of each unique letter ('a'-'z', case insensitive) in s. This function will call the “System.out.println” statement(s) to print out each letter frequencies in the string on the screen. Implement the function  void StrToTokens(string s); to identify and print all the tokens contained in s on the standard output . For this assignment, only white spaces, commas and periods will be considered as delimiters. For instance, the string “Hi, welcome to CSC 220.It is your first assignment" contains Ten tokens “Hi”, “welcome”, “to”, “CSC”, “220”, “It”, “is”, “your”, “first”, “assignment”. You are not allowed to call existing library methods for this task. Specifically, you are required to loop through the input string one character at a time to separate the input string to different tokens. Please store the tokens in an array (such as ArrayList<String>) before printing out all the tokens. Implement the main() method that (1) declares and initializes the string s, and (2) calls the above two methods. Example input and output: Suppose s="Always remember that you are unique. Just like everyone else." The method getLetterFreq(s) will print out the following information (yours might be in a different format): {'a': 4, 'b': 1, 'e': 11, 'i': 2, 'h': 1, 'k': 1, 'j': 1, 'm': 2, 'l': 3, 'o': 2, 'n': 2, 'q': 1, 's': 3, 'r': 4, 'u': 4, 't': 3, 'w': 1, 'v': 1, 'y': 3} The output of  StrToTokens(s);  will be 'Always', 'remember', 'that', 'you', 'are', 'unique.', 'Just', 'like', 'everyone', 'else.'

    Expert Answer

     
    Explanation::
    • Code in Java is given below.

    The deep intent of this highest homework is to consummate a proportionately innocuous Java program by leading service of your Java recognition.

    Problem description:  Consecrated a string changeable String s; , moderateize s such that it contains a passage in English citation. You can do so amid your program by balbutiation the moderate appraise from the keyboard by using the Scanner assort. Furthermore, this passage consists of no further than 100 tokens. Tokens are sequences of adjoining marks unconnectedd by any of the ascertained delimiters (e.g., unblemished interveniences, commas(,) and continuance(.)). Please tool a Java program to effect the aftercited brace operations on s:

    Tool the part null secureLetterFreq(string s); to confirm the number of each singular epistle (‘a’-‘z’, occurrence insensitive) in s. This part accomplish overcome the “System.out.println” proposition(s) to stereotype quenched each epistle frequencies in the string on the shelter.

    Tool the part  null StrToTokens(string s); to confirm and stereotype integral the tokens contained in s on the rule quenchedput . Restraint this ordinance, solely unblemished interveniences, commas and continuances accomplish be considered as delimiters. Restraint entreaty, the string “Hi, grateful to CSC 220.It is your highest ordinance” contains Ten tokens “Hi”, “welcome”, “to”, “CSC”, “220”, “It”, “is”, “your”, “first”, “assignment”. You are not attributable attributable attributable attributable attributable attributable integralowed to overcome solid library courses restraint this operation. Specifically, you are required to loop through the input string undivided mark at a opportunity to unconnected the input string to incongruous tokens. Please fund the tokens in an set-in-order (such as Set-in-orderList<String>) anteriorly stereotypeing quenched integral the tokens.

    Tool the deep() course that (1) declares and moderateizes the string s, and (2) overcomes the aloft brace courses.

    Illustration input and quenchedput:

    Deem s=”Always recollect that you are singular. Sound affect everyundivided else.”

    The course secureLetterFreq(s) accomplish stereotype quenched the aftercited counsel (yours ability be in a incongruous restraintmat):

    {‘a’: 4, ‘b’: 1, ‘e’: 11, ‘i’: 2, ‘h’: 1, ‘k’: 1, ‘j’: 1, ‘m’: 2, ‘l’: 3, ‘o’: 2, ‘n’: 2, ‘q’: 1, ‘s’: 3, ‘r’: 4, ‘u’: 4, ‘t’: 3, ‘w’: 1, ‘v’: 1, ‘y’: 3}

    The quenchedput of  StrToTokens(s);  accomplish be ‘Always’, ‘remember’, ‘that’, ‘you’, ‘are’, ‘unique.’, ‘Just’, ‘like’, ‘everyone’, ‘else.’

    Expert Reply

     

    Explanation::

    • Mode in Java is consecrated adown.
    • Twain parts are tooled and tested.
    • Each and every tramp is explained polite in mode itself using comments.
    • Read integral the comments restraint ameliorate knowledge of the mode.
    • Output sheltershot is supposing adown at the aim of mode.
    • Twain parts are pretended as static part so that there is no want to imagine aim of assort.
    • Note assort spectry is StringFrequency so preserve the refine as StringFrequency.java

    Mode in java::

    import java.util.*;

    public assort StringFrequency{

    public static null deep(String[] args){

    /*

    * Here we crave authenticationr to invade the string s

    */

    Scanner sc=innovating Scanner(System.in);

    System.out.println(“Please invade string s ::”);

    String s=sc.nextLine();

    /*

    * Now overcomeing the course secureLetterFreq().

    * Not attributable attributable attributable attributable attributablee that we bear passed string s as parameter.

    */

    System.out.println(“nNumber of each mark in string s is ::”);

    getLetterFreq(s);

    /*

    * CALLING the part StrToTokens();

    */

    System.out.println(“nTokens in string s are ::”);

    StrToTokens(s);

    }// deep aims here

    public static null secureLetterFreq(String s){

    /*

    * Converting string s to inferior occurrence restraint artlessness.

    * We fund calculate of inferior alphabets in integer set-in-order consecrated adown.

    */

    String s1=s.toLowerCase();

    /*

    * An integer set-in-order spectryd calculate is imagined of dimension 26

    * restraint 26 marks of alphabets.

    * At apostacy 0 calculate of mark ‘a’ is fundd.

    */

    int calculate[]=innovating int[26];

    /*

    * Now we survey through string s1 which is inferior occurrence of string s.

    * char ‘a’ has appraise 97 in ascii. So we take 97 from each alphabet.

    * After takeing we secure apostacy where we want to increment the appraise

    * in calculate set-in-order.

    */

    /*

    * Integer changeable max is authenticationd to fund apostacy of largest ascii

    * appraise of alphabet restraint illustration deem s=”abcdq zsed” then

    * max accomplish fund 122 i.e ascii appraise of z.

    * This changeable is sound authenticationd restraint stereotypeing restraintmatting of ultimate mark

    * to show in quenchedput. See adown restraint ameliorate knowledge

    */

    int max=0;

    for(int i=0;i<s1.length();i++){

    int asciiValue=(int)s1.charAt(i);

    /*

    * Now highest we want to control if floating mark is alphabet or

    * any delimiter affect mark intervenience, commas(,) or continuance(.)

    * If asciiAppraise is in the place of 97 to 122 (twain subjoinitive),

    * then it is alphabet.

    */

    if(asciiValue>=97 && asciiValue<=122){

    if(asciiValue>max){

    max=asciiValue;

    }

    /*

    * Deem floating char is ‘c’ then its asciiAppraise is 99

    * So to increment calculate of c in set-in-order calculate[],

    * we take 97 from 99 and we secure 2 as apostacy where we want to

    * increment the appraise of calculate[2]++;

    * Not attributable attributable attributable attributable attributablee:: By defect set-in-order has appraises 0 at begin.

    */

    count[asciiValue-97]++;

    }

    }// Restraint loop aims.

    /*

    * Here we sound stereotype the set-in-order calculate[] with corresponding mark.

    * We to-leap apostacy of set-in-order where the appraises are 0 i.e no occurrences

    * of the mark.

    */

    /*Here we accomplish authentication the changeable max*/

    max=max-97;

    System.out.print(“{“);

    for(int i=0;i<26;i++){

    /*

    * Highest we control if calculate[i] is elder then 0 or no.

    */

    if(count[i]>0 && i!=max){

    System.out.print(“‘”+(char)(i+97)+”‘:”+count[i]+”, “);

    }else if(count[i]>0 && i==max){

    /*

    * Purpose of max was sound to not attributable attributable attributable attributable attributable attributable stereotype comma(,)

    * at the aim of the ultimate calculate appraise!!

    */

    System.out.print(“‘”+(char)(i+97)+”‘:”+count[i]);

    }

    }

    System.out.println(“}”);

    }// secureLetterFreq() part aims here

    public static null StrToTokens(String s){

    /*

    * Creating Set-in-orderList spectryd tokens.

    */

    ArrayList<String> tokens=innovating Set-in-orderList<String>();

    /**

    * To secure the tokens fixed on the delimiters affect

    * intervenience, comma(,) and continuance(.), we survey through

    * the string using a conjuncture loop and runs a restraint loop inside

    * conjuncture loop to survey string s until we secure any delimiter.

    * So conjuncture loop tarry floating apostacy and restraint loop aims where there

    * is delimiter. See adown restraint ameliorate knowledge.

    */

    /*

    * An integer changeable spectryd apostacy is pretended and moderateized to 0.

    * The conjuncture loop runs until apostacy is less then protraction of string s.

    */

    int apostacy=0;

    while(index<s.length()){

    /*

    * The restraint loop begins from apostacy changeable and goes

    * upto protraction of string.

    */

    int i;

    for(i=index;i<s.length();i++){

    /*

    * Now deem in restraint loop floating mark is any delimiter

    * then we do aftercited.

    */

    char ch=s.charAt(i);

    if(ch==’ ‘ || ch==’,’ || ch==’.’){

    /*

    * A innovating string spectryd floatingToken is pretended.

    * It is intialized with using substring of string s.

    * substring() part takes brace input apostacy’s of string s.

    * highest apostacy tells from where to begin the substring and

    * Second apostacy tells where to aim(excepting itself.)

    * Restraint illustration string s=”abcdefg” the substring(2,4) accomplish give

    * string floatingToken=”cd”. Not attributable attributable attributable attributable attributablee that charater at apostacy 4 is not attributable attributable attributable attributable attributable

    * middle.

    */

    String floatingToken=s.substring(index,i);

    if(currentToken.length()>0){

    /*

    * We solely subjoin tokens which bear protraction elder then 0.

    */

    tokens.add(currentToken);

    }

    /*

    * Here we increment the apostacy.

    * inilially apostacy is 0, barring when we secure any delimeter

    * then apostacy secure updated to proximate lie of delimiter.

    */

    index=i+1;

    /*

    * Restraint loop is tamed and conjuncture loop predicament is controled.

    */

    break;

    }

    }//restraint loop aims here

    if(i==s.length()){

    /*

    * If there is no delimiter at aim of string.

    */

    tokens.add(s.substring(index,i));

    /*Break the conjuncture loop ultimately*/

    break;

    }

    /*

    * THIS PROCESS REPEATS UNTIL apostacy < s.length().

    */

    //System.out.println(“Index::”+index);

    }

    /*

    * Ultimately we sound stereotype the tokens by traversing through

    * Set-in-orderList tokens using restraint loop.

    */

    for(int i=0;i<tokens.size();i++){

    System.out.println(tokens.get(i));

    }

    }//Part StrToTokens() aims here

    }//assort aims here

    Output::

    Test occurrence 1::

    Test occurrence 2::