Exercise 9.4
- Assuming that the string s contains sentences that consist of words,
blanks, and punctuation marks (period, comma, exclamation mark,
question mark, colon, and semi-colon), construct a StringTokenizer
object whose tokens consist only of the words in s.
- Write a class method wordCount that has a single String parameter,
s. Assuming that s consists of words separated by whitespace, the
method should return the number of words in s.
- Write a class method value that will return, as an int, the value of a
simple arithmetic expression contained in a string. The string should
contain two integers surrounding one of the operators *, /, %, +, or
-. The string may also contain blanks. As an example, given that
s = " 13 + 2"
the method should return the value 15. The method should assume
that its argument contains a valid expression.
| |