Write a class with the following static methods: - wordCount. This method should
ID: 3643902 • Letter: W
Question
Write a class with the following static methods:- wordCount. This method should accept a reference to a String object as an argument and return the number of words contained in the object.
- arrayToString. This method accepts a char array as an argument and converts it to a String object. The method should return a reference to the String object.
- mostFrequent. This method accepts a reference to a String object as an argument and returns the character that occurs the most frequently in the object.
- replaceSubstring. This method accepts three references to String objects as arguments. Let's call them string1, string2, and string3. It searches string1 for all occurrences of string2. When it finds an occurrence of string2, it replaces it with string3.
For example, suppose the three arguments have the following values:
string1: "the dog jumped over the fence'
string2: ''the''
string3: "that" With these three arguments, the method would return a reference to a String object with the value "that dog jumped over that fence"
Demonstrate each of these methods in a complete program.
NOTE:THE REQUIREMENT FOR THIS ASSIGNMENT IS THAT I NEED TO CREATE 2 SEPARATE FILES, ONE CONTAINS THOSE METHODS THAT YOU JUST GAVE ME, AND ONE IS THE MAIN CLASS FOR THOSE METHODS. I ALSO NEED TO USE THE StringTokenizer CLASS FOR THIS ASSIGNMENT BUT I DON'T KNOW HOW TO DO. COULD ANYONE HELP?
Explanation / Answer
The string tokeniser class in Java would be very helpful (similar to strtok in C). A simple example of how to use this class is here : StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } prints the following output: this is a test Now, here are the functions that you need : wordCount(String str){ StringTokenizer st = new StringTokenizer("this is a test"); int count = 0; while (st.hasMoreTokens()) { count ++; } return count; } Array to string : Just take the input array and concatenate each array element into an initially empty string. Return when you have read the whole array. Mostfrequentchar: Make an array of 26 integers (A-Z). Tokenize the string into words (not necessary), and read each letter in the word and increase the count. return the one with the maximum count. replacesubctring : Tokenise the string. for each token, compare with string2, if it matches, concatenate string3, otherwise concatenate the read token. ***** If you want to use two files, create a file that contains all the methods and import that class into the file that has the main class, using the command import *** Please rate lifesaver!! Hope it helps!Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.