Read the nouns, verbs, and phrases for the SentenceGenerator class from files. Use an ArrayList instead of an array for each of these.
What will be an ideal response?
```
import j a v a . u t i l . ? ;
import j a v a . i o . ? ;
/? ?
? C l a s s t o g e n e r a t e s e n t e n c e s u s i n g l i s t s o f nouns ,
? v e r b s , and p h r a s e s
? @author Barb E r i c s o n
? C o p y r i g h t 2006
?/
public c l a s s S e n t e n c e G e n L i s t
{
// ///////// f i e l d s /////////////
/? ? l i s t o f nouns ?/
private L i s t n o u n L i s t ;
/? ? l i s t o f v e r b s ?/
private L i s t v e r b L i s t ;
/? ? l i s t o f p h r a s e s ?/
private L i s t p h r a s e L i s t ;
/? ? random number g e n e r a t o r ?/
private Random randGen = new Random ( ) ;
// ////////////// methods ///////////////////////////////////
/? ?
? Method t o c r e a t e and r e t u r n a l i s t from t h e p a s s e d f i l e
? @param f i l e N a m e t h e name o f t h e f i l e t o r e a d from
? @return a l i s t o f S t r i n g s r e a d from t h e f i l e
?/
public L i s t c r e a t e L i s t F r o m F i l e ( S t r i n g f i l e N a m e )
{
L i s t r e t u r n L i s t = new A r r a y L i s t ( ) ;
S t r i n g l i n e = null ;
// t r y t o do t h e f o l l o w i n g
try {
// c r e a t e t h e b u f f e r e d r e a d e r
BufferedReader reader =
new B u f f e r e d R e a d e r (new F i l e R e a d e r ( f i l e N a m e ) ) ;
// Loop w h i l e t h e r e i s more d a t a
while ( ( l i n e = r e a d e r . r e a d L i n e ( ) ) != null )
{
// add t h e l i n e t o t h e l i s t
r e t u r n L i s t . add ( l i n e ) ;
}
// c l o s e t h e r e a d e r
reader . close ( ) ;
} catch ( FileNotFoundException ex ) {
SimpleOutput . showError ( ” Couldn ’ t f i n d ” + f i l e N a m e +
” please pick i t . ” ) ;
fileName = FileChooser . pickAFile ( ) ;
return c r e a t e L i s t F r o m F i l e ( f i l e N a m e ) ;
} catch ( E x c e p t i o n ex ) {
SimpleOutput . showError ( ” E r r o r r e a d i n g f i l e ” + f i l e N a m e ) ;
ex . p r i n t S t a c k T r a c e ( ) ;
}
return r e t u r n L i s t ;
}
/? ?
? Method t o
? files
?/
private void
{
nounList =
verbList =
phraseList
}
fill
t h e noun , v e r b , and p h r a s e l i s t s from
f i l l L i s t s ()
c r e a t e L i s t F r o m F i l e ( ” nouns . t x t ” ) ;
createListFromFile ( ” verbs . txt ” ) ;
= createListFromFile (” phrases . txt ” ) ;
/? ?
? Method t o g e n e r a t e a random s e n t e n c e
? @return a random s e n t e n c e
?/
public S t r i n g generateRandomSentence ( )
{
S t r i n g s e n t e n c e = n o u n L i s t . g e t ( randGen . n e x t I n t ( n o u n L i s t . s i z e ( ) ) ) + ” ” +
v e r b L i s t . g e t ( randGen . n e x t I n t ( v e r b L i s t . s i z e ( ) ) ) + ” ” +
p h r a s e L i s t . g e t ( randGen . n e x t I n t ( p h r a s e L i s t . s i z e ( ) ) ) + ” . ” ;
return s e n t e n c e ;
}
/? ?
? Main f o r t e s t i n g
?/
public s t a t i c void main ( S t r i n g [ ] a r g s )
{
S e n t e n c e G e n L i s t sentenceGen = new S e n t e n c e G e n L i s t ( ) ;
sentenceGen . f i l l L i s t s ( ) ;
f o r ( i n t i = 0 ; i < 5 ; i ++)
System . out . p r i n t l n ( sentenceGen . generateRandomSentence ( ) ) ;
}
}
```
You might also like to view...
All of the following EXCEPT ________ are common delimiters
A) slashes B) commas C) tabs D) spaces
Rick unpacks and sets up a new Windows 7 64-bit PC he bought from the store. The PC runs great, but it takes a long time to boot up. What can he do to speed up the boot processor?
a. Update the antivirus signature files. b. Stop some of the autostarting programs from loading c. Defragment the hard drive d. Run CheckDisk on the hard drive.