import java.util.Random; /** * Write a description of class RandomSentences here. * * @author John Sloan * @version 03/11/08 */ public class RandomSentences { private String [ ] how; private String [ ] who; private String [ ] what; private String [ ] when; private String howString; private String whoString; private String whatString; private String whenString; private Random generator; /** * Constructor for objects of class RandomSentences */ public RandomSentences( ) { //initializes the string to the field above howString = "Suddenly:Without warning:Slowly:Silently:Sheepishly:Hurridily:" + "Confusingly:Abruptly:Deliberately:Carelessly:Ominously:Inexplicably"; //initializes the string to the field above whoString = "he:She:it:I:they:we:someone:everyone:all:no one"; //initializes the string to the field above whatString = "ran:talked:slept:ate:laughed:cried:yelled:sang:played:walked:" + "crawled:waved"; //initializes the string to the field above whenString = "yesterday:earlier:Tuesday:at once:after a while:in the house:" + "in the garden:for some time:intermittently:on and on:oncue:" + "fervently"; //uses the colon to allpy sections of verbage above to elements of array how = howString.split( ":" ); who = whoString.split( ":" ); what = whatString.split( ":" ); when = whenString.split( ":" ); //initializes generator with a new space in memory of the Random package generator = new Random ( ); } /** * The makeSentence Method. * create a sentence by randomly choosing four words or phrases, one from each array of string. * */ public void makeSentence( ) { int howIndex; int whoIndex; int whatIndex; int whenIndex; int howString; //assigns one phrase to the local variable above howIndex = generator.nextInt(how.length); String howWord = how [howIndex]; whoIndex = generator.nextInt(who.length); String whoWord = how [whoIndex]; whatIndex = generator.nextInt(what.length); String whatWord = how [whatIndex]; whenIndex = generator.nextInt(when.length); String whenWord = how [whenIndex]; System.out.println(howWord + ", " + whoWord + " " + whatWord + " " + whenWord + "."); } /** * The "Babble method calls the makeSentence method a specified (num) of times specified by the parameter. */ public void babble ( int num ) { if(num > 0) for(int i = 0; i < num; i++) { makeSentence( ); } } }