This homework is necessary preparation for the lab. Make sure you type your answers in a file you bring to the lab so that you will be able to quickly copy and paste them into the file provided in the lab.
Implement the static method generateElements declared as follows:
/** * Generates the set of characters in the given {@code String} into the * given {@code Set}. * * @param str * the given {@code String} * @param charSet * the {@code Set} to be replaced * @replaces charSet * @ensures charSet = entries(str) */ private static void generateElements(String str, Set<Character> charSet) {...}
Informally, generateElements extracts the characters from the given String and puts them in the given Set. Note that there is no requires clause to rule out repeated characters in str and that charSet is a replaces-mode parameter.
Implement the static method nextWordOrSeparator declared as follows:
1 /** 2 * Returns the first "word" (maximal length string of characters not in 3 * {@code separators}) or "separator string" (maximal length string of 4 * characters in {@code separators}) in the given {@code text} starting at 5 * the given {@code position}. 6 * 7 * @param text 8 * the {@code String} from which to get the word or separator 9 * string 10 * @param position 11 * the starting index 12 * @param separators 13 * the {@code Set} of separator characters 14 * @return the first word or separator string found in {@code text} starting 15 * at index {@code position} 16 * @requires 0 <= position < |text| 17 * @ensures <pre> 18 * nextWordOrSeparator = 19 * text[position, position + |nextWordOrSeparator|) and 20 * if entries(text[position, position + 1)) intersection separators = {} 21 * then 22 * entries(nextWordOrSeparator) intersection separators = {} and 23 * (position + |nextWordOrSeparator| = |text| or 24 * entries(text[position, position + |nextWordOrSeparator| + 1)) 25 * intersection separators /= {}) 26 * else 27 * entries(nextWordOrSeparator) is subset of separators and 28 * (position + |nextWordOrSeparator| = |text| or 29 * entries(text[position, position + |nextWordOrSeparator| + 1)) 30 * is not subset of separators) 31 * </pre> 32 */ 33 private static String nextWordOrSeparator(String text, int position, 34 Set<Character> separators) {...}
A "word" is defined as a string of characters that are not in the separators set, and a "separator string" instead is defined as a string of characters that are in the separators set. nextWordOrSeparator is expected to return the longest word or separator string that starts at the given position in the given String.
Informally, the ensures clause states that if the character at the given position in text is not in the set of separators (lines 20-21)—that is, we are extracting a word—then the String returned by nextWordOrSeparator must be the substring of text that starts at the given position (lines 18-19), is made up entirely of characters not in the set of separators (line 22), and it is either at the end of text (line 23) or it is followed in text by a character that is in the set of separators (lines 24-25). On the other hand, if the character at the given position in text is in the set of separators (lines 20 and 26)—that is, we are extracting a separator string—then the String returned by nextWordOrSeparator must be the substring of text that starts at the given position (lines 18-19), is made up entirely of characters in the set of separators (line 27), and it is either at the end of text (line 28) or it is followed in text by a character that is not in the set of separators (lines 29-30).