Write a class with the following static methods: - Wordcount: This method should
ID: 3641679 • 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.
- replace Substring: 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.
Explanation / Answer
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Scanner; public class StringOperations { static int wordCount(String s) { String[] str = s.split(" "); return str.length; } String arrayToString(char[] carray) { return carray.toString(); } static char mostFrequent(String theString) { HashMap map = new HashMap(); String unique = new String(); for (int i = 0; iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.