Complete the body of the following private static method. 1 2 3 4 5 6 7 8 9 10 11 12 13…
- User
Complete the body of the following private static method. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 /**
- Returns the first "word" (maximal length string of characters not in
- {@code SEPARATORS}) or "separator string" (maximal length string of
- characters in {@code SEPARATORS}) in the given {@code text} starting at
- the given {@code position}.
- @param text
-
code
the {@code String} from which to get the word or separator -
code
string - @param position
-
code
the starting index - @return the first word or separator string found in {@code text} starting
-
code
at index {@code position} - @requires 0 <= position < |text|
- @ensures
- nextWordOrSeparator =
- text[position, position + |nextWordOrSeparator|) and
- if entries(text[position, position + 1)) intersection entries(SEPARATORS) = {}
- then
- entries(nextWordOrSeparator) intersection entries(SEPARATORS) = {} and
- (position + |nextWordOrSeparator| = |text| or
- entries(text[position, position + |nextWordOrSeparator| + 1))
-
code
intersection entries(SEPARATORS) /= {}) - else
- entries(nextWordOrSeparator) is subset of entries(SEPARATORS) and
- (position + |nextWordOrSeparator| = |text| or
- entries(text[position, position + |nextWordOrSeparator| + 1))
-
code
is not subset of entries(SEPARATORS))
/ private static String nextWordOrSeparator(String text, int position) {...} This is a modified version of the method you wrote in a homework and lab in Software I. Feel free to reuse your own code (but not someone else's) for this homework. Note that the method here has one less parameter than in the Software I version. The "separators" are defined as a String constant as follows: /*
- Definition of whitespace separators. / private static final String SEPARATORS = " \t\n\r"; Complete the body of the following public static method. You should use nextWordOrSeparator in your solution because (except for END_OF_INPUT as explained below) "non-separator token" and "non-whitespace token" here mean "word" as used in nextWordOrSeparator. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 /*
- Assistant
To complete the body of the method, we can write a piece of code that checks each character from the starting position and categorizes it as either part of a word or a separator sequence. Below is a possible implementation, assuming the definition of is available within the class context: