Guest User

Untitled

a guest
Jul 22nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. /* Mariya Korostil
  2.  * 03/31/2012
  3.  * Sentences
  4.  * D-5
  5.  * Write an applet that uses random number generation to create sentences. The program should generate 20 sentences and display them on an applet window.
  6.  * For extra credit I created the button that generates 20 new sentences (repaints), and made so that when you resize the window, sentences aren't generated.
  7.  */
  8. import java.applet.Applet;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. public class Sentence extends Applet implements ActionListener {
  13.     Button textButton;
  14.     Font f = new Font("Comic", Font.BOLD, 18);
  15.     String noun[] = { "boy", "girl", "dog", "town", "car" };
  16.     String article[] = { "the", "a", "one", "some", "any" };
  17.     String verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
  18.     String preposition[] = { "to", "from", "over", "under", "on" };
  19.     String sentence[];
  20.  
  21.     public void init() {
  22.         textButton = new Button("Press to generate 20 new sentences");
  23.         add(textButton);
  24.         textButton.addActionListener(this);
  25.         sentence = new String[20];
  26.         generateSentence();
  27.     }
  28.  
  29.     public void actionPerformed(ActionEvent e) {
  30.         Object source = e.getSource();
  31.  
  32.         if (source == textButton)
  33.             generateSentence();
  34.         repaint();
  35.  
  36.     }
  37.  
  38.     public void paint(Graphics g) {
  39.         for (int i = 0; i < 20; i++)
  40.             g.drawString((i + 1) + ". " + sentence[i], 20, 60 + i * 15);
  41.     }
  42.  
  43.     public void generateSentence() {
  44.         {
  45.             sentence = new String[20];
  46.             for (int i = 0; i < 20; i++) {
  47.                 String Sentence;
  48.                 Sentence = article[(int) (Math.random() * 5)] + " "
  49.                         + noun[(int) (Math.random() * 5)] + " "
  50.                         + verb[(int) (Math.random() * 5)] + " "
  51.                         + preposition[(int) (Math.random() * 5)] + " "
  52.                         + article[(int) (Math.random() * 5)] + " "
  53.                         + noun[(int) (Math.random() * 5)] + ".";
  54.                 Sentence = Sentence.substring(0, 1).toUpperCase()
  55.                         + Sentence.substring(1);
  56.                 sentence[i] = Sentence;
  57.             }
  58.  
  59.         }
  60.     }
  61.  
  62. }
Add Comment
Please, Sign In to add comment