CMPU 102 - Lab 9

In this lab you will implement part of Assignment 9.  (If you have already started assignment 9, great; you will have less work to do.)

You should complete at least steps 1-5 during the lab session.

Tasks

  1. Add a class to represent nodes in the decision tree.  You may write your own node class, or you may copy the DecisionTreeNode class.

  2. Modify the createOrLoadInitialDecisionTree method (in the ExpertSystem class) to construct the initial three-node decision tree, as described in the handout for Assignment 9.  It should consist of a single root node (the top-level question), with two leaf nodes underneath (both of them answers).

  3. Add a method to ExpertSystem that takes a PrintStream as a parameter and outputs the current decision tree to that stream in whatever format you have devised.  Next, add a call to this method in your program so that the initial decision tree is output to System.out.

  4. Implement the playOnce method so that it plays the guessing game once, and then returns false.  You do not have to implement adding new questions and answers to the decision tree.  Run the program and verify that it works for the single question in the initial decision tree.  Hint: you can create a BufferedReader to read lines of input from System.in as follows:

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    

    Use the readLine method to read a single line of input (the user's response to a yes/no question).  You will probably want to modify the playOnce, run, and main methods so that they throw IOException.

  5. Add code to the saveCurrentDecisionTree method to save the decision tree to a file called "decisiontree.dat".  Hint: you can create a PrintStream object that writes to a file as follows:

    PrintStream out = new PrintStream(new FileOutputStream("decisiontree.dat"));
    

    Then you can pass this object to the method you created in step 3 to save your decision tree to a file.  Don't forget to call close on the PrintStream after you have saved the decision tree.  Run the program and verify that it saves your initial three-node decision tree.  The file will be in the directory "eclipse-workspace/expertsystem".  From a terminal window, run the commands:

    cd
    cd eclipse-workspace
    cd expertsystem
    ls
    cat decisiontree.dat
    

    After running the ls command you should see the file "decisiontree.dat" in the directory listing, and the cat command should print its contents.

  6. Modify the createOrLoadInitialDecisionTree method so that it attempts to read the saved decision tree from the file.  If the file cannot be found, then it should create the default initial decision tree instead.  Hint: First create a method that takes a BufferedReader as an argument, reads a saved decision tree from the BufferedReader, and returns the root of the decision tree read from the file.  Then, you can create a BufferedReader to read from a file as follows:

    BufferedReader reader = new BufferedReader(new FileReader("decisiontree.dat"));
    

    You will probably want the method that reads the decision tree to throw IOException.  However, createOrLoadInitialDecisionTree should NOT throw IOException.  Instead, it should use a try/catch block, where the catch block catches IOException.  The try block should attempt to load the saved decision tree, and the catch block should create the default initial decision tree.

You can test step 6 by modifying "decisiontree.dat" in a text editor, and adding extra nodes to the tree.  To edit the file, run the following commands from a terminal window:

cd
emacs eclipse-workspace/expertsystem/decisiontree.dat &

Don't forget the save the file before re-testing the expert system program.

Getting Started

Download expertsystem.zip from the class web page and import it into your Eclipse workspace.

When you are done

Demonstrate your expert system to me by deleting "decisiontree.dat", running the expert system program, and then showing me the contents of the newly-created "decisiontree.dat".  Extra credit will be given if you can modify this file and load a larger decision tree into the program.

Submit by running the following commands in a terminal window:

cd
cd eclipse-workspace
submit102 expertsystem