Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML
 
 
Hot Web Programming Job

 

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML

[an error occurred while processing this directive]

Java Notes

switch Example - Random Insults

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

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.

Random Insult Model

This is the logic of the program, and does no I/O so it would be equally suitable as the logic behind a console program, GUI program, or web server program.

  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 
 92 
 93 
 94 
 95 
 96 
 97 
// File   : 1java/textgen/InsultGenerator1.java
// Purpose: Generates random insults.
// Author : Fred Swartz
// License:public domain
// Date   : 2005 May 16
// Comments: The structure of the random generation is 
//           entirely in executable code, which creates a
//           large maintenance problem.
//           A much better way to write this is as a 
//           Data-Driven program, where this information is
//           represented by data structures, and there is a
//           simple program that processes the data.

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));
    }
}

Console Main program

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
// File   : 1java/textgen/TestTextGenerator1.java
// Purpose: 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);
    }
}

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (
post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.