Created
September 11, 2017 09:05
-
-
Save ssaurel/2374585e234c465c36a70dfb7d5c90a5 to your computer and use it in GitHub Desktop.
Anagram class for a tutorial on the SSaurel's Blog
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
package com.ssaurel.anagram; | |
import java.util.Random; | |
/** | |
* Created by ssaurel on 05/09/2017. | |
*/ | |
public class Anagram { | |
public static final Random RANDOM = new Random(); | |
public static final String[] WORDS = {"ACCOUNT", "ADDITION", | |
"AGREEMENT", "ANGRY", "ANIMAL", "BEHAVIOUR", "BETWEEN", "BLACK", "CHEMICAL", "FOOLISH", | |
"FREQUENT", "GOVERNMENT", "GRAIN", "GRASS", "HOSPITAL", "PAYMENT", "POLITICAL", | |
"PROCESS", "SHAME", "SMASH", "SMOOTH", "STATEMENT", "SUBSTANCE", "TEACHING", "TENDENCY", | |
"TOMORROW", "TOUCH", "UMBRELLA", "WEATHER", "YESTERDAY"}; | |
public static String randomWord() { | |
return WORDS[RANDOM.nextInt(WORDS.length)]; | |
} | |
public static String shuffleWord(String word) { | |
if (word != null && !"".equals(word)) { | |
char a[] = word.toCharArray(); | |
for (int i = 0; i < a.length; i++) { | |
int j = RANDOM.nextInt(a.length); | |
char tmp = a[i]; | |
a[i] = a[j]; | |
a[j] = tmp; | |
} | |
return new String(a); | |
} | |
return word; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment