A Dictionary
tree (tree)
monkey (mon key)
bear (bear)
prepare (pre pare)
Types of Select:
It would select any one of the words at random.
It would select either:
monkey (mon key)
or
prepare (pre pare)
d.select (``tree'', '' ``key'')
|
It would return a random word ending in one of these strings (syllables).
d.select (1, ``key'', ``tree'')
|
It would return a random word of one syllable ending in either ``key'' or ``tree''.
On operating random numbers in the blue.math package is a class called RandomGenerator which you can use to generate random numbers.
SPECIFICATION
new RandomGenerator () -- < RandomGenerator >
|
METHOD
< RandomGenerator > .uniform (< int >, < int >) -- < int >
|
This method generates a random integer between the first < int > and the second < int >, inclusive.
EXAMPLE:
RandomGenerator rg =
new RandomGenerator ();
for (int x = 1; x < = 10; x + +)
{
int rn = rg.uniform (0, 100);
IO.println (rn);
|
Will print out 10 random numbers between 0 and 100..
vs
IMPLEMENT
|
< Dictionary > .select () -- < Word >
|
in the dictionary class.
Recall the instance variables...
private String name;
private Word [] words;
private int nrElements;
|
public Word select ()
{
RandomGenerator rg =
new RandomGenerator ();
int rn = rg.uniform (0, nrElements-1);
return words [rn];
|
Part two of programming challenge:
(1) Create several (like 3 or 4 or 5) small dictionaries, each containing a ``part of speech'' (article, noun, verb, adjective). Do this by creating a data file for each dictionary and then ``reading'' the dictionary using the appropriate functionality.
(2) Write a number of methods, each of which will generate a ``fixed'' sort of phrase at random. It will be ``fixed'' in that each time you call it, it will generate a similar sentence in terms of parts of speech, rhythm, and rhyme.
EXAMPLE
simplePhaseOfType1 ()
might print a sentence consisting of...
- an article
- a noun
- a verb
- an article
- an adjective
- a noun
with particular ``rhythm and rhyme.''
Suppose article, noun, verb, and adjective are dictionaries (static global dictionary variables).
static private void simplePhaseOfType1 ()
{
Word article1 = article.select ();
Word noun1 = noun.select (2);
Word verb = verb.select ();
Word article2 = article.select ();
Word adjective = adjective.select ();
Word noun2 = noun.select (2);
printSentence (article1);
printSentence (noun1);
printSentence (verb);
printSentence (article2);
printSentence (adjective);
printSentence (noun2);
|
(3) Use the few methods in some interesting ways.