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

    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

    • Code in Java is given below.

    The deep goal of this earliest homework is to entire a proportionately innocuous Java program by prelude service of your Java cognizance.

    Problem description:  Abandoned a string wavering String s; , moderateize s such that it contains a article in English quotation. You can do so amid your program by balbutiation the moderate rate from the keyboard by using the Scanner rank. Furthermore, this article consists of no more than 100 tokens. Tokens are sequences of adjoining kinds severedd by any of the precise delimiters (e.g., clear interveniences, commas(,) and continuance(.)). Please tool a Java program to act the aftercited couple jobs on s:

    Tool the administration unsubstantial procureLetterFreq(string s); to authenticate the number of each rare message (‘a’-‘z’, exemplification impassible) in s. This administration procure circumvent the “System.out.println” proposition(s) to stereotype extinguished each message frequencies in the string on the defend.

    Tool the administration  unsubstantial StrToTokens(string s); to authenticate and stereotype whole the tokens contained in s on the criterion extinguishedput . Ce this enactment, singly clear interveniences, commas and continuances procure be considered as delimiters. Ce exemplification, the string “Hi, grateful to CSC 220.It is your earliest enactment” contains Ten tokens “Hi”, “welcome”, “to”, “CSC”, “220”, “It”, “is”, “your”, “first”, “assignment”. You are referable wholeowed to circumvent real library modes ce this job. Specifically, you are required to loop through the input string undivided kind at a season to severed the input string to unanalogous tokens. Please supply the tokens in an marshal (such as MarshalList<String>) anteriorly stereotypeing extinguished whole the tokens.

    Tool the deep() mode that (1) declares and moderateizes the string s, and (2) circumvents the overhead couple modes.

    Specimen input and extinguishedput:

    Conceive s=”Always recevery that you are rare. Upright enjoy wholeundivided else.”

    The mode procureLetterFreq(s) procure stereotype extinguished the aftercited referableice (yours sway be in a unanalogous cemat):

    {‘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 extinguishedput of  StrToTokens(s);  procure be ‘Always’, ‘remember’, ‘that’, ‘you’, ‘are’, ‘unique.’, ‘Just’, ‘like’, ‘everyone’, ‘else.’

    Expert Apology



    • Jurisprudence in Java is abandoned under.
    • Twain administrations are tooled and tested.
    • Each and whole step is explained well-mannered-mannered in jurisprudence itself using comments.
    • Read whole the comments ce meliorate reason of the jurisprudence.
    • Output defendshot is supposing under at the purpose of jurisprudence.
    • Twain administrations are pretended as static administration so that there is no demand to fashion aim of rank.
    • Note rank indicate is StringFrequency so secure the finish as StringFrequency.java

    Jurisprudence in java::

    import java.util.*;

    public rank StringFrequency{

    public static unsubstantial deep(String[] args){


    * Here we pray reasonr to invade the string s


    Scanner sc=odd Scanner(System.in);

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

    String s=sc.nextLine();


    * Now circumventing the mode procureLetterFreq().

    * Referablee that we own passed string s as parameter.


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



    * CALLING the administration StrToTokens();


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


    }// deep purposes here

    public static unsubstantial procureLetterFreq(String s){


    * Converting string s to inferior exemplification ce sincerity.

    * We supply calculate of inferior alphabets in integer marshal abandoned under.


    String s1=s.toLowerCase();


    * An integer marshal indicated calculate is fashiond of magnitude 26

    * ce 26 kinds of alphabets.

    * At condemnation 0 calculate of kind ‘a’ is supplyd.


    int calculate[]=odd int[26];


    * Now we explore through string s1 which is inferior exemplification of string s.

    * char ‘a’ has rate 97 in ascii. So we remove 97 from each alphabet.

    * After removeing we procure condemnation where we demand to increment the rate

    * in calculate marshal.



    * Integer wavering max is reasond to supply condemnation of largest ascii

    * rate of alphabet ce specimen conceive s=”abcdq zsed” then

    * max procure supply 122 i.e ascii rate of z.

    * This wavering is upright reasond ce stereotypeing cematting of latest kind

    * to show in extinguishedput. See under ce meliorate reason


    int max=0;

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

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


    * Now earliest we demand to cohibit if present kind is alphabet or

    * any delimiter enjoy kind intervenience, commas(,) or continuance(.)

    * If asciiRate is in the rank of 97 to 122 (twain subjoinitive),

    * then it is alphabet.


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





    * Conceive present char is ‘c’ then its asciiRate is 99

    * So to increment calculate of c in marshal calculate[],

    * we remove 97 from 99 and we procure 2 as condemnation where we demand to

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

    * Referablee:: By lapse marshal has rates 0 at initiate.




    }// Ce loop purposes.


    * Here we upright stereotype the marshal calculate[] with selfsame kind.

    * We to-leap condemnation of marshal where the rates are 0 i.e no occurrences

    * of the kind.


    /*Here we procure reason the wavering max*/



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


    * Earliest we cohibit if calculate[i] is senior 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 upright to referable stereotype comma(,)

    * at the purpose of the decisive calculate rate!!






    }// procureLetterFreq() administration purposes here

    public static unsubstantial StrToTokens(String s){


    * Creating MarshalList indicated tokens.


    ArrayList<String> tokens=odd MarshalList<String>();


    * To procure the tokens domiciled on the delimiters enjoy

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

    * the string using a opportunity loop and runs a ce loop inside

    * opportunity loop to explore string s until we procure any delimiter.

    * So opportunity loop withwithhold present condemnation and ce loop purposes where there

    * is delimiter. See under ce meliorate reason.



    * An integer wavering indicated condemnation is pretended and moderateized to 0.

    * The opportunity loop runs until condemnation is hither then elongation of string s.


    int condemnation=0;



    * The ce loop initiates from condemnation wavering and goes

    * upto elongation of string.


    int i;



    * Now conceive in ce loop present kind is any delimiter

    * then we do aftercited.


    char ch=s.charAt(i);

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


    * A odd string indicated presentToken is pretended.

    * It is intialized with using substring of string s.

    * substring() administration takes couple input condemnation’s of string s.

    * earliest condemnation tells from where to initiate the substring and

    * Second condemnation tells where to purpose(save itself.)

    * Ce specimen string s=”abcdefg” the substring(2,4) procure give

    * string presentToken=”cd”. Referablee that charater at condemnation 4 is referable

    * interjacent.


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



    * We singly subjoin tokens which own elongation senior then 0.





    * Here we increment the condemnation.

    * inilially condemnation is 0, yet when we procure any delimeter

    * then condemnation procure updated to instant comcomposition of delimiter.




    * Ce loop is domesticated and opportunity loop mood is cohibited.




    }//ce loop purposes here



    * If there is no delimiter at purpose of string.



    /*Break the opportunity loop decisively*/




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





    * Decisively we upright stereotype the tokens by traversing through

    * MarshalList tokens using ce loop.


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



    }//Administration StrToTokens() purposes here

    }//rank purposes here


    Test exemplification 1::

    Test exemplification 2::