Predicates: snowing and weekend
Consequences: go to work, read a book, shovel the driveway, ski:
| snowing | weekend | activity |
| false | false | go to work |
| false | true | read a book |
| true | false | shovel driveway |
| true | true | go skiing |
*/
import javax.swing.JOptionPane;
import psJava.KeyIn;
class ActivityExpert {
public boolean snowing;
public boolean weekend;
// flat rules
public String flatRules(){
if (!snowing &&
!weekend) return "go to work";
if (!snowing &&
weekend) return "read a book";
if ( snowing &&
!weekend) return "shovel driveway";
if ( snowing &&
weekend) return "go skiing";
// Note the need for a "drop
through" return, or the compiler complains.
return "Error! no rule invoked";
}
// cascadingElse
public String cascadingElse() {
if (!snowing &&
!weekend)
return "go to work";
else if (!snowing &&
weekend)
return "read a book";
else if ( snowing &&
!weekend)
return "shovel driveway";
else if ( snowing &&
weekend)
return "go skiing";
else return "Error! no rule
invoked";
}
// decision tree
public String decisionTree() {
if (snowing) {
if (weekend)
return "go skiing";
else
return "shovel the driveway";
} else {
if (weekend)
return "read a book";
else
return "go to work";
}
}
public void testRules() {
JOptionPane.showMessageDialog(null,
"Testing flat list");
snowing = false;
weekend = false;
JOptionPane.showMessageDialog(null,
"Not snowing, not weekend " + flatRules());
snowing = false;
weekend = true;
JOptionPane.showMessageDialog(null,
"Not snowing, weekend " + flatRules());
snowing = true;
weekend = false;
JOptionPane.showMessageDialog(null,
"Snowing, not weekend " + flatRules());
snowing = true;
weekend = true;
JOptionPane.showMessageDialog(null,
"Snowing, weekend " + flatRules());
JOptionPane.showMessageDialog(null,
"");
JOptionPane.showMessageDialog(null,
"Testing cascade");
snowing = false;
weekend = false;
JOptionPane.showMessageDialog(null,
"Not snowing, not weekend " + cascadingElse());
snowing = false;
weekend = true;
JOptionPane.showMessageDialog(null,
"Not snowing, weekend " + cascadingElse());
snowing = true;
weekend = false;
JOptionPane.showMessageDialog(null,
"Snowing, not weekend " + cascadingElse());
snowing = true;
weekend = true;
JOptionPane.showMessageDialog(null,
"Snowing, weekend " + cascadingElse());
JOptionPane.showMessageDialog(null,
"");
JOptionPane.showMessageDialog(null,
"Testing Decision Tree");
snowing = false;
weekend = false;
JOptionPane.showMessageDialog(null,
"Not snowing, not weekend " + decisionTree());
snowing = false;
weekend = true;
JOptionPane.showMessageDialog(null,
"Not snowing, weekend " + decisionTree());
snowing = true;
weekend = false;
JOptionPane.showMessageDialog(null,
"Snowing, not weekend " + decisionTree());
snowing = true;
weekend = true;
JOptionPane.showMessageDialog(null,
"Snowing, weekend " + decisionTree());
JOptionPane.showMessageDialog(null,
"");
}
public void userInput() {
snowing = KeyIn.readBoolean("Snowing?");
weekend = KeyIn.readBoolean("Weekend?","yes",
"no");
JOptionPane.showMessageDialog(null, decisionTree());
}
} // ActivityExpert