// Program to test solutions to problem #8 on the cse142 midterm, winter 2019. // Fill in your solution to printStripped, then compile and run the program. import java.util.*; import java.io.*; public class Test8 { public static void printStripped(String s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static void printStripped2(String s) { boolean inComment = false; for (int i = 0; i < s.length(); i++) { char next = s.charAt(i); if (next == '<') { inComment = true; } else if (inComment && next == '>') { inComment = false; } else if (!inComment) { System.out.print(next); } } System.out.println(); } private static int count, failCount; public static final String END_TEST_TEXT = "end test"; public static final String TEMP_FILE_NAME = "CSE142_Test8_temporary_file.txt"; public static void main(String[] args) throws FileNotFoundException { int fail = 0; // create an output file with correct answers and student answers File temp = new File(TEMP_FILE_NAME); PrintStream out = new PrintStream(temp); PrintStream old = System.out; System.setOut(out); String[] data = {"this is plain text", "this has a normal comment to be removed", "this has multiple less-than in a comment <<<<", "this > has greater than outside a comment >>", "this has multiple comments<>.", "", "", "", "", "1234", "", "", "", "1234", " ... > > here?", "this is >> < > "}; for (String s : data) { System.out.println(s); try { printStripped2(s); } catch (Exception e) { System.out.println("threw " + e.getClass()); } try { printStripped(s); System.out.println(); } catch (Exception e) { System.out.println("threw " + e.getClass()); } System.out.println(END_TEST_TEXT); } System.out.close(); // go through the file to compare pairs of answers System.setOut(old); Scanner input = new Scanner(temp); while (input.hasNextLine()) { String s = input.nextLine(); String correct = input.nextLine() + "\\n"; String yours = ""; for (;;) { String line = input.nextLine(); if (line.equals(END_TEST_TEXT)) break; yours += line + "\\n"; } yours = yours.substring(0, yours.length() - 2); System.out.println("Testing \"" + s + "\""); System.out.println("Correct output:"); System.out.println(correct); if (correct.equals(yours)) { System.out.println("passed"); } else { fail++; System.out.println("Your output:"); System.out.println(yours); System.out.println("failed"); } System.out.println(); } // remove the temporary file input.close(); temp.delete(); if (fail == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + fail + " of " + data.length + " tests"); } } }