// Program to test solutions to problem #10 on the cse142a final, winter 2019. // Fill in your solution to acronym, then compile and run the program. import java.util.*; public class FinalTest10 { public static String acronym(String s) { // fill in your solution here // you can also rename the parameter above } // this is the sample solution public static String acronym2(String s) { boolean inWord = false; s = s.toUpperCase(); String result = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == ' ' || ch == '-') { inWord = false; } else if (!inWord) { inWord = true; result += ch; } } return result; } private static int count, failCount; public static void main(String[] args) { test("self-contained underwater breathing apparatus"); test(" automatic teller machine "); test("personal IDENTIFICATION number"); test("merry-go-round"); test("All my Children"); test("troubled assets relief program"); test("computer science"); test("--quite-- confusing - punctuation-"); test(" loner "); test(" --- foo -- - u --- -nope--- ---"); test("hello"); test("a"); test("-a"); test("a-"); test("-a-"); test("d"); test(" d"); test("d "); test(" d "); test("-AB-"); test("all--Done--WITH--dAshES"); test("ab"); test("--ab"); test("ab--"); test("--ab--"); test("ab---bc"); test("---ab---bc"); test("ab---bc-"); test("--ab---bc---"); if (failCount == 0) { System.out.println("passed all tests"); } else { System.out.println("failed " + failCount + " of " + count + " tests"); } } public static void test(String s) { count++; System.out.println("testing acronym(\"" + s + "\")"); String s1 = null; boolean fail = false; try { s1 = acronym(s); } catch (RuntimeException e) { int line = e.getStackTrace()[1].getLineNumber(); System.out.println("threw " + e + " at line #" + line); fail = true; } String s2 = acronym2(s); System.out.println("correct = " + s2); if (!fail && !s1.equals(s2)) { System.out.println("yours = " + s1); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }