WhatRain's Java 2 Examples
I am a student studying java right now. Could you guys help me with
this.
I have to write an applet using random number generation to create
sentence. I have to create 4 arrays of strings called "article",
"Noun", "Verb", and "Preposition"
IFrom those arrays i have to create a sentence in the order of: article,
noun, verb, preposition,article,and noun.
The words are separated by spaces. It should start with capital letter
and ends with period.
20 sentences should be generated and put to the text area.
the arrays should be filled as follows:
article array contains: "the","a","one,"some","and "any"
Noun array contains: "boy","girl","dog","town",and "car"
Verb array contains: "drove","jumped","ran","walked","skipped"
Preposition array contains: "to","from","over","under", and "on"
I really apreciate if you could help me with this applet.
Thank you very much
from,
kecebonk
|
Source
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
* Random No Sense
* An Applet to Generate random word
* sentence in the order of: article,
* noun, verb, preposition,article,and noun.
* The words are separated by spaces.
* It should start with capital letter
* and ends with period.
* articles are : "the","a","one,"some","and "any"
* Nouns are : "boy","girl","dog","town",and "car"
* Verbs are : "drove","jumped","ran","walked","skipped"
* Prepositions are : "to","from","over","under", and "on"
*
* @author http://www.whatrain.com/
* @version beta 0.1
* @author Timothy M. Radonich
* @author WhatRain Internet Services
* @see WordLists.class
* @see RSGenerator.class
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class RandomSentence extends Applet implements
ActionListener
{
Panel controlPanel = new Panel();
TextArea theText = new TextArea( "" );
Button startButton = new Button( "New List");
RSGenerator makeSentences = new RSGenerator();
public void init()
{
RandomSentence theApp = new RandomSentence( );
}
public RandomSentence( )
{
setSize(350,350);
controlPanel.setLayout(new BorderLayout() );
controlPanel.add( "North",startButton );
controlPanel.setBackground( Color.white );
startButton.addActionListener(this);
setLayout(new BorderLayout());
add( "West",controlPanel);
theText.setBackground( Color.white );
theText.setForeground( Color.black );
theText.setEditable( false );
add( "Center", theText );
setVisible(true);
theText.setText
( makeSentences.getRandomSentences(20) );
}
public void actionPerformed( ActionEvent theEvent )
{
if ( theEvent.getSource( ) == startButton )
{
theText.setText
( makeSentences.getRandomSentences(20) );
}
}
}
// RSGenerator.java
// april 1999 Timothy M. Radonich
// www.whatrain.com
// timmy@whatrain.com
import java.util.Random;
/**
* An utility to Generate random word
* sentence in the order of: article,
* noun, verb, preposition,article,and noun.
* The words are separated by spaces.
* It should start with capital letter
* and ends with period.
*
* @author http://www.whatrain.com/
* @version beta 0.01
* @author Timothy M. Radonich
* @author WhatRain Internet Services
* @see WordLists.class
* @see RandomSentence.class
*/
public class RSGenerator
{
// ========================================
// CONSTRUCTOR
// ========================================
RSGenerator ( )
{
article = wordlists.article ;
noun = wordlists.noun ;
verb = wordlists.verb ;
preposition = wordlists.preposition ;
}
// ========================================
// PUBLIC METHODS
// ========================================
/**
* @see WordLists.class
* @param int numberOfSentences
* @return String output
* @exception exceptions no exceptions thrown
*/
public String getRandomSentences( int numberOfSentences)
{
String output = new String("");
int nos = numberOfSentences ;
for ( int i=0; i < nos; i++)
output = output + getSentence();
return output;
}
// ========================================
// PRIVATE METHODS
// ========================================
private String getSentence()
{
String output = new String("");
output = output + capitalize(article[(int)
(random.nextFloat() * (float)
(article.length))]) + " ";
output = output + noun[(int)
(random.nextFloat() *
(float) (noun.length))] + " ";
output = output + verb[(int)
(random.nextFloat() *
(float) (verb.length))] + " ";
output = output + preposition[(int)
(random.nextFloat() *
(float) (preposition.length))] + " ";
output = output + article[(int)
(random.nextFloat() *
(float) (article.length))] + " ";
output = output + noun[(int) (random.nextFloat() *
(float) (noun.length))] + ".\n";
return output;
}
private String capitalize(String a)
{
String word = new String(a);
String output = new String( "");
String first = new String(word.substring(0,1));
String last = new String(word.substring(1));
first = first.toUpperCase();
output = first + last ;
return output;
}
// ========================================
// CLASS ATTRIBUTES
// ========================================
// THE WORDS ARE STORED IN THE WORDLISTS CLASS
private WordLists wordlists = new WordLists();
// ARRAYS TO HOLD EACH WORD LIST
private String article[] ;
private String noun[] ;
private String verb[] ;
private String preposition[] ;
// RANDOM FOR SELECTING WORDS FROM ARRAYS
private Random random = new Random();
}
// WordLists.java
// april 1999 Timothy M. Radonich
// www.whatrain.com
// timmy@whatrain.com
/**
* Random No Sense
* An Applet to Generate random word
* sentence in the order of: article,
* noun, verb, preposition,article,and noun.
* The words are separated by spaces.
* It should start with capital letter
* and ends with period.
* articles are : "the","a","one,"some","and "any"
* Nouns are : "boy","girl","dog","town",and "car"
* Verbs are : "drove","jumped","ran","walked","skipped"
* Prepositions are : "to","from","over","under", and "on"
*
* @author http://www.whatrain.com/
* @version beta 0.1
* @author Timothy M. Radonich
* @author WhatRain Internet Services
* @see RandomSentenceUI.class
* @see RSGenerator.class
*/
public class WordLists
{
String article[] = { "the","a","one","some", "any" };
String noun[] = { "boy","girl","dog","town","car" };
String verb[] = { "drove","jumped","ran","walked","skipped" };
String preposition[] = { "to","from","over","under", "on" };
}
|