//
Random term paper writer
// Java how to program, 5/e, Exercise 11.9
package StringsAndCharacters;
import javax.swing. * ;
import java.awt. * ;
import java.awt.event. * ;
public class RandomTermPaperWriter extends JFrame{
private String[] article = { " the " , " a " , " one " , " some " , " any " };
private String[] noun = { " boy " , " girl " , " dog " , " town " , " car " };
private String[] verb = { " drove " , " jumped " , " ran " , " walked " , " skipped " };
private String[] preposition = { " to " , " from " , " over " , " under " , " on " };
private String[] sentences = new String[ 20 ];
JTextArea output;
JScrollPane scroller;
public RandomTermPaperWriter()
{ super ( " Random term paper writer " );
Container container = getContentPane();
container.setLayout( new FlowLayout());
JLabel hint1Label = new JLabel( " Press " );
container.add(hint1Label);
JButton startButton = new JButton( " Start " );
container.add(startButton);
startButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
displayPaper();
}
}
);
JLabel hint2Label = new JLabel( " to write paper. " );
container.add(hint2Label);
output = new JTextArea( 20 , 40 );
scroller = new JScrollPane(output);
container.add(scroller);
setSize( 600 , 500 );
setVisible( true );
}
public static void main(String args[])
{
RandomTermPaperWriter paperWriter = new RandomTermPaperWriter();
paperWriter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void displayPaper(){
for ( int i = 0 ;i < 20 ;i ++ ){
int [] random = new int [ 6 ];
for ( int j = 0 ;j < 6 ;j ++ ){
random[j] = ( int )( 5 * Math.random());
}
sentences[i] = article[random[ 0 ]] + " " + noun[random[ 1 ]] + " " + verb[random[ 2 ]] + " "
+ preposition[random[ 3 ]] + " " + article[random[ 4 ]] + " " + noun[random[ 5 ]];
output.append(sentences[i] + "/n " );
}
}
}
// Java how to program, 5/e, Exercise 11.9
package StringsAndCharacters;
import javax.swing. * ;
import java.awt. * ;
import java.awt.event. * ;
public class RandomTermPaperWriter extends JFrame{
private String[] article = { " the " , " a " , " one " , " some " , " any " };
private String[] noun = { " boy " , " girl " , " dog " , " town " , " car " };
private String[] verb = { " drove " , " jumped " , " ran " , " walked " , " skipped " };
private String[] preposition = { " to " , " from " , " over " , " under " , " on " };
private String[] sentences = new String[ 20 ];
JTextArea output;
JScrollPane scroller;
public RandomTermPaperWriter()
{ super ( " Random term paper writer " );
Container container = getContentPane();
container.setLayout( new FlowLayout());
JLabel hint1Label = new JLabel( " Press " );
container.add(hint1Label);
JButton startButton = new JButton( " Start " );
container.add(startButton);
startButton.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event){
displayPaper();
}
}
);
JLabel hint2Label = new JLabel( " to write paper. " );
container.add(hint2Label);
output = new JTextArea( 20 , 40 );
scroller = new JScrollPane(output);
container.add(scroller);
setSize( 600 , 500 );
setVisible( true );
}
public static void main(String args[])
{
RandomTermPaperWriter paperWriter = new RandomTermPaperWriter();
paperWriter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void displayPaper(){
for ( int i = 0 ;i < 20 ;i ++ ){
int [] random = new int [ 6 ];
for ( int j = 0 ;j < 6 ;j ++ ){
random[j] = ( int )( 5 * Math.random());
}
sentences[i] = article[random[ 0 ]] + " " + noun[random[ 1 ]] + " " + verb[random[ 2 ]] + " "
+ preposition[random[ 3 ]] + " " + article[random[ 4 ]] + " " + noun[random[ 5 ]];
output.append(sentences[i] + "/n " );
}
}
}
A snapshot of program execution: