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