/** * @Homework: Assignment 5 * @Exercise: 8.9 in textbook, page 461 * @Filename: Sentence.java * @School: UCSD, Summer 98 Java 1 class * @author: Richard Parry * @version: 1.0 - August 5, 1998 * * Description: * This applet uses random numbers to generate sentences which are comprised * of: article, noun, verb, and preposition from a predefined group or * words. It also creates a short story consisting of randomly generated * sentences. The number of sentences, the length of the paragraph, and other * parameters are constants which can be changed in the source code to * to slightly modify the application output. **/ // import packages import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class Sentence extends Applet implements ActionListener { private String[] article = { "the", "a", "one", "some", "any" }; private String[] noun = { "boy", "girl", "dog", "town", "car" }; private String[] preposition = { "to", "from", "over", "under", "on" }; private String[] verb = { "drove", "jumped", "ran", "walked", "skipped" }; // constants for sentence generation private static final int NUMBER_OF_SENTENCES = 20; // constants for paragraph (short story) generation private static final int SENTENCES_IN_PARAGRAPH = 15; private static final int MAX_WORDS_PER_LINE = 14; private StringBuffer sentBuffer, paraBuffer; private String sentArray[], paraArray[], paraString; private Button sentenceButton, paragraphButton; private int lineNumber = 0; // clear line counter /** * This init method initializes the buttons and places them in the * output window. * * @param - none * @return - none **/ public void init() { // initialize the button sentenceButton = new Button( "Make Sentences" ); sentenceButton.addActionListener( this ); add( sentenceButton ); paragraphButton = new Button( "Make Paragraph" ); paragraphButton.addActionListener( this ); add( paragraphButton ); //instantiate objects sentBuffer = new StringBuffer(); paraBuffer = new StringBuffer(); sentArray = new String[ NUMBER_OF_SENTENCES ]; paraArray = new String[ SENTENCES_IN_PARAGRAPH ]; } // end init method /** * This paint method prints the sentences and short story in the window. * It is called each time the user presses one of the mouse buttons. * * @param - Graphics ojbect to draw * @return - none **/ public void paint ( Graphics g ) { int xposition = 10; int yposition = 50; int sentCounter; // display sentence, one per line for ( int i = 0; i < NUMBER_OF_SENTENCES; i++ ) { // start new column of sentences if half of sentences are displayed if ( i == NUMBER_OF_SENTENCES / 2 ) { xposition = 220; yposition = 50; } sentCounter = i + 1; g.drawString( sentCounter + ". " + sentArray[i], xposition, yposition); yposition += 15; } // display paragraph xposition = 10; yposition = 220; g.drawString( "SHORT STORY", 170, yposition); yposition += 15; for (int i = 0; i < lineNumber; i++ ) { g.drawString( paraArray[i], xposition, yposition); yposition += 15; } } // end paint method /** * This method catches mouse button events and performs the specified * action such as generating sentences and storying them in an array. * This method also creates the short story. * * The sentences are generated by selecting at random individual words * and using a StringBuffer to append each word and then storing the * sentence in a String Array. * * The paragraph (Short Story) is also generated by generating words * at random. However, the words are counted as they are stored in * the StringBuffer. When the number of words specified for each line * is reached, that StringBuffer is saved to a StringArray and counters * are reset. * * @param - Mouse event * @return none **/ public void actionPerformed( ActionEvent e ) { String firstChar, firstWord, wordSuffix; // create sentence if ( e.getSource() == sentenceButton ) { for ( int i = 0; i < NUMBER_OF_SENTENCES; i++ ) { sentBuffer = new StringBuffer(); // clear buffer // select first word and change first character to uppercase // there is probably a more elegant solution, but the "data type" // requirements of the methods places limitations on the algorithm. firstWord = article[(int) (Math.random() * 5)]; firstChar = firstWord.substring(0, 1); // extract first char of word firstChar = firstChar.toUpperCase(); // change first char to upper wordSuffix = firstWord.substring(1); // extract all but first char firstWord = firstChar.concat( wordSuffix ); // put word together sentBuffer.append ( firstWord ); // put first word in buffer // create remainder of sentence sentBuffer.append ( ' ' ); sentBuffer.append ( noun[(int) (Math.random() * 5)] ); sentBuffer.append ( ' ' ); sentBuffer.append ( verb[(int) (Math.random() * 5)] ); sentBuffer.append ( ' ' ); sentBuffer.append ( preposition[(int) (Math.random() * 5)] ); sentBuffer.append ( ' ' ); sentBuffer.append ( article[(int) (Math.random() * 5)] ); sentBuffer.append ( ' ' ); sentBuffer.append ( noun[(int) (Math.random() * 5)] ); sentBuffer.append ( '.' ); sentArray[i] = new String ( sentBuffer ); } } // create paragraph if ( e.getSource() == paragraphButton ) { paraBuffer = new StringBuffer(); // create new empty buffer int wordCounter = 0; // word counter lineNumber = 0; // clear line counter0 for ( int i = 0; i <= SENTENCES_IN_PARAGRAPH; i++ ) { // select first word and change first character to uppercase // there is probably a more elegant solution, but the "data type" // requirements of the methods places limitations on the algorithm. firstWord = article[(int) (Math.random() * 5)]; firstChar = firstWord.substring(0, 1); // extract first char of word firstChar = firstChar.toUpperCase(); // change first char to upper wordSuffix = firstWord.substring(1); // extract all but first char firstWord = firstChar.concat( wordSuffix ); // put word together paraBuffer.append ( firstWord ); // put first word in buffer paraBuffer.append ( ' ' ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } // create remainder of sentence paraBuffer.append ( noun[(int) (Math.random() * 5)] ); paraBuffer.append ( ' ' ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } paraBuffer.append ( verb[(int) (Math.random() * 5)] ); paraBuffer.append ( ' ' ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } paraBuffer.append ( preposition[(int) (Math.random() * 5)] ); paraBuffer.append ( ' ' ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } paraBuffer.append ( article[(int) (Math.random() * 5)] ); paraBuffer.append ( ' ' ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } paraBuffer.append ( noun[(int) (Math.random() * 5)] ); paraBuffer.append ( ". " ); // after every word, check for if a new line should be created wordCounter++; if( wordCounter == MAX_WORDS_PER_LINE ) { paraArray[lineNumber] = new String ( paraBuffer ); // save line wordCounter = 0; // reset word counter lineNumber++; // increment line counter, we have another line paraBuffer = new StringBuffer(); // create new empty buffer } } } repaint(); } // end actionPerformed method } // end class