import components.set.Set; import components.set.Set1L; import components.simplereader.SimpleReader; import components.simplereader.SimpleReader1L; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; /** * Program to test static methods {@code generateElements} and * {@code nextWordOrSeparator}. * * @author Put your name here * */ public final class NextWordOrSeparatorTest { /** * Private constructor so this utility class cannot be instantiated. */ private NextWordOrSeparatorTest() { } /** * 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 charSet) { assert str != null : "Violation of: str is not null"; assert charSet != null : "Violation of: charSet is not null"; // TODO - fill in body } /** * 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 * the {@code String} from which to get the word or separator * string * @param position * the starting index * @param separators * the {@code Set} of separator characters * @return the first word or separator string found in {@code text} starting * at index {@code position} * @requires 0 <= position < |text| * @ensures
     * nextWordOrSeparator =
     *   text[position, position + |nextWordOrSeparator|)  and
     * if entries(text[position, position + 1)) intersection separators = {}
     * then
     *   entries(nextWordOrSeparator) intersection separators = {}  and
     *   (position + |nextWordOrSeparator| = |text|  or
     *    entries(text[position, position + |nextWordOrSeparator| + 1))
     *      intersection separators /= {})
     * else
     *   entries(nextWordOrSeparator) is subset of separators  and
     *   (position + |nextWordOrSeparator| = |text|  or
     *    entries(text[position, position + |nextWordOrSeparator| + 1))
     *      is not subset of separators)
     * 
*/ private static String nextWordOrSeparator(String text, int position, Set separators) { assert text != null : "Violation of: text is not null"; assert separators != null : "Violation of: separators is not null"; assert 0 <= position : "Violation of: 0 <= position"; assert position < text.length() : "Violation of: position < |text|"; // TODO - fill in body /* * This line added just to make the program compilable. Should be * replaced with appropriate return statement. */ return ""; } /** * Main method. * * @param args * the command line arguments */ public static void main(String[] args) { /* * Open input and output streams */ SimpleReader in = new SimpleReader1L(); SimpleWriter out = new SimpleWriter1L(); /* * Create set of separator characters for test */ Set separatorSet = new Set1L<>(); /* * Add spurious characters to separator set--these should not be part of * the actual separator set */ final String spuriousStr = "abcde123"; generateElements(spuriousStr, separatorSet); /* * Construct actual separator set--these are the only characters that * should be in the separator set */ final String separatorStr = " \t, "; generateElements(separatorStr, separatorSet); /* * Output separator set */ out.print("Separators: {"); boolean first = true; for (char ch : separatorSet) { if (first) { first = false; } else { out.print(","); } out.print("'" + ch + "'"); } out.println("}"); /* * Ask for test inputs */ out.println(); out.print("New test case (y/n)? "); String response = in.nextLine(); while (response.equals("y")) { /* * Output heading */ out.print("Test input: "); String testStr = in.nextLine(); out.println(); out.println("----Generated output----"); out.println(); /* * Process test case */ int position = 0; while (position < testStr.length()) { String token = nextWordOrSeparator(testStr, position, separatorSet); if (separatorSet.contains(token.charAt(0))) { out.print(" Separator: <"); } else { out.print(" Word: <"); } out.println(token + ">"); position += token.length(); } /* * Ask user whether to continue */ out.println(); out.print("New test case (y/n)? "); response = in.nextLine(); } /* * Close input and output streams */ in.close(); out.close(); } }