// Program to test solutions to problem #10 on the cse142a final, winter 2013. // Fill in your solution to acronym, then compile and run the program. import java.util.*; public class FinalTestA10 { 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--- ---"); 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("actual = " + s2); if (!fail && !s1.equals(s2)) { System.out.println("theirs = " + s1); fail = true; } if (fail) { System.out.println("failed"); failCount++; } else { System.out.println("passed"); } System.out.println(); } }