public class StringProcessorTest { public static void main(String[] args) { KeyboardReader kb = new KeyboardReader(); kb.prompt("Input a String or - stop - to quit: "); String str = kb.getKeyboardInput(); while (!str.equals("stop")){ kb.display("Testing printLetters()\n"); StringProcessor.printLetters(str); kb.display("testing countChars()\n"); kb.display("Total occurences of e = "); kb.display(StringProcessor.countChar(str,'e') + "\n"); kb.display("Testing reverse()\n"); kb.display(StringProcessor.reverse(str)+ "\n"); kb.display("Testing capitalize()\n"); kb.display(StringProcessor.capitalize(str) + "\n\n"); kb.prompt("Input a String or stop to quit: "); str = kb.getKeyboardInput(); } // while } // main() } // StringProcessorTest class
Solution 7.13
Method to remove all blanks from a string:
// Pre: s is a non null string// Post: s is returned with all its blanks removedpublic String removeBlanks(String s) { StringBuffer result = new StringBuffer(); for (int k = 0; k < s.length(); k++) if (s.charAt(k) != ' ') // If this is not a blank result.append(s.charAt(k)); // append it to resultreturn result.toString(); }
Solution 7.14
A Alpha Z Zero Zeroes a alpha bath bin z zero
Solution 7.15
To modify precedes so that it also returns true when its two string arguments are equal, just change the operator in the final return statement to <=:
if (s1.charAt(k) <= s2.charAt(k) ) return true;
Solution 7.16
true
true
false
false
false
true
false
false
false
[Page 344]
Solution 7.17
The variables in TestStringEquals are declared static because they are used in static methods. Whenever you call a method directly from main(), it must be static because main() is static. Remember that static elements are associated with the class, not with its instances. So main() can only use static elements because they don't depend on the existence of instances.