// Program to desmonstrate simple decision tree import java.awt.*; import java.awt.event.*; public class CarD extends java.applet.Applet implements ActionListener { TextField mQuery, mAnswer; Button bYes, bNo; int location; public void init() { mQuery = new TextField(70); mQuery.setText("Was your car just now running before it quit?"); bYes = new Button("Yes"); bNo = new Button("No"); location = 0; mAnswer = new TextField(70); bYes.addActionListener(this); bNo.addActionListener(this); add(mQuery) ; add(bYes) ; add(bNo) ; add(mAnswer) ; } public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bYes) { if (location == 0) { location = 1; mQuery.setText("Is the gas gauge on empty?"); } else if (location == 1) { location = 3; mAnswer.setText("Better find some gas."); } else if (location == 2) { location = 5; mAnswer.setText("Maybe you should check the gas."); } } if (cause == bNo) { if (location == 0) { location = 2; mQuery.setText("Does the engine crank?"); } else if (location == 1) { location = 4; mAnswer.setText("Messy. Call a tow truck."); } else if (location == 2) { location = 6; mAnswer.setText("Looks like a dead battery."); } } } }