18. Unit Testing
Activity 1: Counting the Words in the String
Solution
- Create a class named
WordCount
. - Define a method named
countWords()
that takes as input aString
. The method will count the words in theString
.public int countWords(String text) { int count = 0; return count; }
- In
countWords()
, check if the input String is null. If not, trim any spaces at the beginning and end of the text. Then, split theString
into words.if (text != null) { String trimmed = text.trim(); if (trimmed.length() > 0) { String[] words = trimmed.split("...