Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Mariya Korostil
- * 03/31/2012
- * Sentences
- * D-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.
- * 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.
- */
- import java.applet.Applet;
- import java.awt.*;
- import java.awt.event.*;
- public class Sentence extends Applet implements ActionListener {
- Button textButton;
- Font f = new Font("Comic", Font.BOLD, 18);
- String noun[] = { "boy", "girl", "dog", "town", "car" };
- String article[] = { "the", "a", "one", "some", "any" };
- String verb[] = { "drove", "jumped", "ran", "walked", "skipped" };
- String preposition[] = { "to", "from", "over", "under", "on" };
- String sentence[];
- public void init() {
- textButton = new Button("Press to generate 20 new sentences");
- add(textButton);
- textButton.addActionListener(this);
- sentence = new String[20];
- generateSentence();
- }
- public void actionPerformed(ActionEvent e) {
- Object source = e.getSource();
- if (source == textButton)
- generateSentence();
- repaint();
- }
- public void paint(Graphics g) {
- for (int i = 0; i < 20; i++)
- g.drawString((i + 1) + ". " + sentence[i], 20, 60 + i * 15);
- }
- public void generateSentence() {
- {
- sentence = new String[20];
- for (int i = 0; i < 20; i++) {
- String Sentence;
- Sentence = article[(int) (Math.random() * 5)] + " "
- + noun[(int) (Math.random() * 5)] + " "
- + verb[(int) (Math.random() * 5)] + " "
- + preposition[(int) (Math.random() * 5)] + " "
- + article[(int) (Math.random() * 5)] + " "
- + noun[(int) (Math.random() * 5)] + ".";
- Sentence = Sentence.substring(0, 1).toUpperCase()
- + Sentence.substring(1);
- sentence[i] = Sentence;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment