AP Question Scramble Word
The Question: http://www.mathorama.com/apcs/scrambleWordQuestion.pdf
Tester for your code
import java.util.ArrayList; import java.util.List; public class Scrambler { /** Scrambles a given word. * * @param word the word to be scrambled * @return the scrambled word (possibly equal to word) * Precondition: word is either an empty string or contains only uppercase letters. * Postcondition: the string returned was created from word as follows: * - the word was scrambled, beginning at the first letter and continuing from left to right * - two consecutive letters consisting of "A" followed by a letter that was not "A" were swapped * - letters were swapped at most once */ public static String scrambleWord(String word) { //your code here return "NOT DONE YET"; } /** Modifies wordList by replacing each word with its scrambled * version, removing any words that are unchanged as a result of scrambling. * @param wordList the list of words * Precondition: wordList contains only non-null objects * Postcondition: * - all words unchanged by scrambling have been removed from wordList - each of the remaining words has been replaced by its scrambled version * - the relative ordering of the entries in wordList is the same as it was * before the method was called */ public static void scrambleOrRemove(List<String> wordlist) { //your code here } public static void main (String[] args) { System.out.println("Part a"); List<String> scramble = new ArrayList<String>(); List<String> expected = new ArrayList<String>(); scramble.add("TAN"); scramble.add("ABRACADABRA"); scramble.add("WHOA"); scramble.add("AARDVARK"); scramble.add("EGGS"); scramble.add("A"); scramble.add(""); expected.add("TNA"); expected.add("BARCADABARA"); expected.add("WHOA"); expected.add("ARADVRAK"); expected.add("EGGS"); expected.add("A"); expected.add(""); for(int i=0; i<scramble.size(); i++) { System.out.print("'"+scramble.get(i)+"' -> '"); System.out.print(scrambleWord(scramble.get(i))); System.out.println("' (expected '"+expected.get(i)+"')" ); } List<String> wordList = new ArrayList<String>(); System.out.println("Part b"); wordList.add("TAN"); wordList.add("ABRACADABRA"); wordList.add("WHOA"); wordList.add("APPLE"); wordList.add("EGGS"); System.out.println("wordList after the call to scrambleOrRemove should be"); System.out.println("TNA, BARCADABARA, PAPLE"); scrambleOrRemove(wordList); for(String word:wordList) { System.out.print("'"+word+"' "); } } }