Lab 5: A Decision Tree Applet

Overview

In lab today you will create an applet for the Decision Tree you designed in your Prelab.

Example: Who are you?

Here is a sample Decision Tree applet to give you an idea of what you need to do:



Preliminaries

Create a subdirectory of your public_html/cps001 directory to store your applet files for this lab. Access the P: drive from My Computer on your desktop. Open the folder for your public_html directory. Open the folder for the cps001 directory you just made. Create a new folder titled Lab5.

For reference, the basic structure of any applet we write in lab and their associated HTML page is at the following location:

What to do

Create an HTML document for your applet. Start GNU Emacs. Go to File->Open and open a new file in your Lab5 directory named DecisionTree.html. Type in the HTML page for your applet, using the applet tag below.

<applet code = "DecisionTree.class" width = 450 height = 125> </applet>

Save your file. Click on your name below to bring up your webpage:

Now you need to turn your Decision Tree design into a functional Java applet. Open a new file in your Lab5 directory named DecisionTree.java.

Be careful not to open multiple copies of the same file!!! It is very easy to lose your work if you do this. You can see a list of all the files you have open by clicking on Buffers. You can switch between files by selecting the file you want from the Buffers drop-down menu. If the file name has a * after it, like this

DecisionTree.java *

it means you haven't saved the file since the last time you made changes.

If you see something like this

DecisionTree.java <2>
DecisionTree.java

it means you have multiple copies of the same file open -- ask for help!

Make sure to do the following in your Java program:

  1. Name your class DecisionTree.

  2. Declare variables you will need. At the very least you will need a TextField, two Buttons, an integer to remember what question you're on.

  3. Write the init() method, which should create new objects, add them to the screen, and set the question number variable. It should also call the addActionListener(this) method on each of your Buttons.

  4. Write the actionPerformed(ActionEvent event) method. This part of your program will be run whenever someone presses one of the buttons in your applet. In this method, you need to use several if and if-else structures so your Decision Tree functions properly. You can get a good idea of how to do this from the NobelAdvice.java program in Great Ideas on pages 57-59. Set the contents of the TextFields the Button labels as appropriate.

Some good advice: Start slow!! Do Steps 1-3 first. Then compile and see what the applet looks like from your webpage. You might want to change the size of your applet in your DecisionTree.html. Once you are satisfied with how your applet looks, move on to Step 4. It will be easier to add code in stages, stopping to compile and test in-between.

You can also look at the Java source program for the example "Who are you?" Decision Tree.

Test your applet!!!

Once your code is complete and your compile is error-free, test out all the possible response combinations to make sure it works exactly as intended. Applets that don't work correctly will lose credit.

Checklist

For full credit, you need to do the following: