Created
November 28, 2018 16:32
-
-
Save CompSciRocks/9b26d86e4e51f4c25b296c046933d1d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RandomLetterChooser letterChooser = new RandomLetterChooser("cat"); | |
for (int k=0; k<4; k++) { | |
System.out.print(letterChooser.getNext()); | |
} | |
// Possible output | |
// actNONE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RandomLetterChooser extends RandomStringChooser { | |
public RandomLetterChooser(String str) { | |
// To be implemented in Part (B) | |
} | |
public static String[] getSingleLetters(String str) { | |
// Implementation not shown | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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