/* Anthony Larrain */ class WordList { private String [] list; private String word; public WordList(){ list = new String [] {"guitar", "drums", "banjo", "flute", "bass", "mandolin","congas", "fluegelhorn","bassoon", "trombone","recorder","piccolo" }; } public String scramble( ) { // get number of characters in the String (word) int len = word.length(); // create a character array to hold the letters of the word char [] temp = new char[len]; // copy the characters of the string into the array for (int i = 0; i < len; i++){ temp[i] = word.charAt(i); } /* * Note: the String class has a method that * will return a reference to an array of characters. * * char [] temp = W.toCharArray(); * */ /* * One way to scramble is for each cell of the array, * randomly generate the index * of another character in the array, and swap. * */ for(int pos, j = 0; j < len; j++) { char temp_cell = temp[j]; pos = (int)(Math.random() * len); temp[j] = temp[pos]; temp[pos] = temp_cell; } /* The string class has a one parameter constructor * that takes a reference to a character array and * forms a string object out of the characters in the array. * */ return new String(temp); } public String pickWord(){ word = list[(int) (Math.random() * list.length)]; return word; } public String getWord() { return word; } }