Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java: switch Example - Random Insults

JavaFAQ Home » Java Notes by Fred Swartz Go to all tips in Java Notes by Fred Swartz


Bookmark and Share

The following class could be useful for generating random insults in response to erroneous user input.

switch Example - Random Insults

Formatting. Some of the cases are formatted on a single line. This is a common style when they do similar short actions. It makes the switch statements much easier to read.

Arrays? This version is written without using arrays, which would provide a better way to do this.

Overloading rand(). The rand() method is overloaded only to demonstrate this feature.

Separate logic from user interface. This program is separated into two files: one file contains the logic for generating the insult in a string. It knows nothing about the user interface, and could equally well be used in a dialog program (as in the test program below), console I/O, a GUI program, or a web-based application.

Look at code example:

Logic

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
// File  : 1java/textgen/InsultGenerator1.java
// Pupose: Generates random insults.
// Author: Fred Swartz
// License:public domain
// Date  : 2005 May 16


public class InsultGenerator1 {

    //============================================ badInputInsult
    public static String badInputInsult() {
        String insult = "bad value from badInputInsult???";
        switch (rand(0, 1)) {
            case 0: insult = "What kind of "
                      + infoAdjective() + " "
                      + infoNoun()
                      + " is this, you "
                      + personAdjective() + " "
                      + person()
                      + "?";
                      break;

            case 1: insult = "Never enter this kind of "
                      + infoAdjective() + " "
                      + infoNoun()
                      + " again!!!!";
                      break;
        }
        return insult;
    }

    //==================================================== person
    public static String person() {
        String name = "bad value from person???";
        switch (rand(3)) {
            case 0: name = "idiot"   ; break;
            case 1: name = "imbecile"; break;
            case 2: name = "moron"   ; break;
        }
        return name;
    }

    //=========================================== personAdjective
    public static String personAdjective() {
        String adj = "bad value from infoAdjective???";
        switch (rand(4)) {
            case 0: adj = "clueless"; break;
            case 1: adj = "witless" ; break;
            case 2: adj = "stupid"  ; break;
            case 3: adj = "hopeless"; break;
        }
        return adj;
    }

    //============================================= infoAdjective
    public static String infoAdjective() {
        String adj = "bad value from infoAdjective???";
        switch (rand(5)) {
            case 0: adj = "revolting"  ; break;
            case 1: adj = "insulting"  ; break;
            case 2: adj = "meaningless"; break;
            case 3: adj = "useless"    ; break;
            case 4: adj = "idiotic"    ; break;
        }
        return adj;
    }

    //================================================== infoNoun
    public static String infoNoun() {
        String noun = "bad value from infoNoun???";
        switch (rand(4)) {
            case 0: noun = "nonsense"; break;
            case 1: noun = "crap"    ; break;
            case 2: noun = "swill"   ; break;
            case 3: noun = "garbage" ; break;
        }
        return noun;
    }

    //===================================================== rand
    // Returns random int in range 0...bound-1
    public static int rand(int bound) {
        return rand(0, bound-1);
    }

    //===================================================== rand
    // Returns random int in range low...high
    public static int rand(int low, int high) {
        return low + (int)(Math.random() * (high - low + 1));
    }
}

Main program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
// File  : 1java/textgen/TestTextGenerator1.java
// Pupose: Show some random insults.
// Author: Fred Swartz
// License:public domain
// Date  : 2005 May 16

import javax.swing.*;

public class TestTextGenerator1 {
    public static void main(String[] args) {
        String display;
        do {
            display = InsultGenerator1.badInputInsult()
                    + "\n\nWould you like another opinion?";
        } while (JOptionPane.showConfirmDialog(null, display) == JOptionPane.YES_OPTION);
    }
}

 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy