3. Object-Oriented Programming
Activity 1: Adding the Frequency-of-Symbol Calculation to WordTool
Solution
Add a method to the previously created WordTool
class to calculate the frequency of a certain symbol. To do so, perform the following steps:
- Add a method to count the number of words in a string.
public int wordCount ( String s ) { int count = 0; // variable to count words // if the entry is empty or is null, count is zero // therefore we evaluate it only otherwise if ( !(s == null || s.isEmpty()) ) { // use the split method from the String class to // separate the words having the whitespace as separator String[] w = s.split("\\s+"); count = w.length; ...