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

The main goal of this first homework is to complete a relatively straightforward

ID: 3888413 • Letter: T

Question

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.'

Explanation / Answer

Here is your java program:

CountLetter.java :

public class CountLetter //function to count friquency of letter
{

void count(String str)
{
int i, j, k, l=0;

char c, ch;
i=str.length();
for(c='A'; c<='Z'; c++)
{
k=0;
for(j=0; j<i; j++)
{
ch = str.charAt(j);
if(Character.toLowerCase(ch) == Character.toLowerCase(c))
{
k++;
}
}
if(k>0)
{
System.out.println( c + " has occurred " + k + " times");
}
}
}

void split(String s) //function to split string by every , white space and dot
{
String[] result = s.split("[\s,.]");
for(int i=0;i!=result.length;i++)
System.out.print(" '"+result[i]+"', ");
}
public static void main(String[] args)
{
CountLetter ob=new CountLetter();
Scanner scan = new Scanner(System.in);
String str;
System.out.print("Enter a String : ");
str=scan.nextLine();
ob.count(str);
ob.split(str);
}
  
  
}

I hope this solves your problem

Comment if you have any problem in the above code

And please give it a thumbs up if it solved your problem :)