Skip to content

Instantly share code, notes, and snippets.

@ClassCubeGists
Last active June 22, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ClassCubeGists/6222c5c5cb8d0988c4549b207a41bf50 to your computer and use it in GitHub Desktop.
Save ClassCubeGists/6222c5c5cb8d0988c4549b207a41bf50 to your computer and use it in GitHub Desktop.
Solution for RandomStringChooser FRQ from 2016
String[] wordArray = {"wheels", "on", "the", "bus"};
RandomStringChooser sChooser = new RandomStringChooser(wordArray);
for (int k = 0; k < 6; k++) {
System.out.print(sChooser.getNext() + " ");
}
// Possible output
// bus the wheels on NONE NONE
RandonLetterChooser letterChooser = new RandomLetterChooser("cat");
for (int k=0; k<4; k++) {
System.out.print(letterChooser.getNext());
}
// Possible output
// actNONE
public class RandomLetterChooser extends RandomStringChooser {
public RandomLetterChooser( String str ) {
super( getSingleLetters( str ) );
}
public static String[] getSingleLetters( String str ) {
String[] out = new String[str.length()];
for (int i=0; i<str.length(); i++) {
out[i] = str.substring(i, i+1);
}
return out;
}
}
public class RandomLetterChooser extends RandomStringChooser {
public RandomLetterChooser(String str) {
// To be implemented in Part (B)
}
public static String[] getSingleLetters(String str) {
// Implementation not shown
}
}
import java.util.ArrayList;
public class RandomStringChooser {
private ArrayList<String> wordList;
public RandomStringChooser( String[] words ) {
wordList = new ArrayList<>();
for ( String word : words ) {
wordList.add( word );
}
}
public String getNext() {
if ( wordList.size() == 0 ) {
return "NONE";
}
int rnd = (int)(Math.random() * wordList.size());
return wordList.remove( rnd );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment