//CS1501 Fall 2006 Assignment 1: Anagrams import java.io.*; import java.util.*; public class MakeString { private final String FILENAME = "letters"; private final int NUMLETTERS = 26; //percentage of occurrences of each letter private final double E = .1174; private final double I = .0865; private final double S = .0815; private final double A = .0797; private final double R = .0744; private final double N = .0742; private final double T = .0676; private final double O = .0594; private final double L = .0535; private final double C = .0413; private final double D = .0381; private final double U = .0322; private final double G = .0284; private final double P = .0277; private final double M = .0270; private final double H = .0215; private final double B = .0203; private final double Y = .0165; private final double F = .0135; private final double V = .0109; private final double K = .0088; private final double W = .0085; private final double Z = .0045; private final double X = .0029; private final double J = .0020; private final double Q = .0019; //put the percentages into an array private final double freq[] = {E,I,S,A,R,N,T,O,L,C,D,U,G,P,M,H,B,Y,F,V,K,W,Z,X,J,Q}; //a parallel array that holds the letters themselves private final String letters[] = {"e","i","s","a","r","n","t","o","l","c", "d","u","g","p","m","h","b","y","f","v","k","w","z","x","j","q"}; //yet another parallel array that will hold the sums of the probabilities, i.e., //the Upper Bound of the random number that corresponds to a particular letter, //e.g., UB[1] = E+I, freq[1] = I, and letter[1] = "i" private double UB[]; //set up the upper bound array for the randomLetter function public MakeString(){ UB = new double[NUMLETTERS]; //initialize array UB[0] = E; for (int index = 1; index < NUMLETTERS; index++){ UB[index] = UB[index - 1] + freq[index]; } }//end constructor //returns a random letter according to the probabilities provided in assignment sheet public String randomLetter(){ Random generator = new Random(); double rnum = generator.nextDouble(); //a random value in the interval [0,1) if (rnum < UB[0]) //if rnum is less than E = .1174 return letters[0]; //return the letter "e" for (int index = 0; index < NUMLETTERS; index++){ //for the rest of the letters //check that rnum is within the appropriate bounds if (rnum >= UB[index] && rnum < UB[index+1]) return letters[index+1]; } return "?"; //if a random letter was not returned (should never happen) }//end randomLetter //creates a random string of length k public void generate(int k) { try { BufferedWriter out = new BufferedWriter(new FileWriter(FILENAME)); //out.write(k + "\n"); //for each slot in the board, generate a random letter and print it out for (int i = 1; i <= k; i++){ String rletter = randomLetter(); out.write(rletter); } out.close(); } catch (IOException e) { System.out.println(e); } }//end generate public static void main (String [] args) { MakeString ms = new MakeString(); System.out.println("Please enter the length of the string you would like to generate."); System.out.print(">> "); BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); try { String strSize = keyboard.readLine(); int size = Integer.parseInt(strSize); //convert user input to an integer ms.generate(size); //use input to generate string of that length System.out.println("Your string has been written in the file \"" + ms.FILENAME + "\""); } catch (Exception e) { System.out.println(e); } } }