CSc 103 Project 3

Binary Trees


The goal of this project is to write a Java implementation of a binary tree and write a client application that uses it.

Overview - Animals Guessing Game

You are required to write a Java application that plays an interactive guessing game called Animals.  The game is similar to Twenty Questions.  The player thinks of an animal and the computer tries to guess which animal the player is imagining by asking questions which can be answered yes or no.  Each time it plays, the computer learns a new animal, and then uses that information in successive games.

When it starts, the computer doesn't know any animals.  So it will simply give up and ask you to tell it the answer (user input shown in boldface for clarity):

        Please think of an animal.
        I give up.
        What animal is it?
        Dog
        What question should I have asked?
        Does it have fur?

After several games have been played, the following dialog might occur:

        Please think of an animal.
        Q: Does it have fur?
        N
        Q: Does it have tusks?
        Y
        Q: Does it have big ears?
        Y
        Is it an elephant?
        N
        Oops, I guessed wrong.


Notice that the computer's strategy is to ask all the relevant questions possible before guessing an animal.  (It does NOT guess an animal then ask another question ...)  If the computer guesses incorrectly, or runs out of things to guess, the user is asked to provide the correct animal and a question which would identify that animal:

        I give up.
        What animal is it?
        Wild Boar
        What question should I have asked?
        Does it have bad breath?



You should limit the user's response to questions to a single character 'y' or 'n' (upper or lower case) or 'q' to quit.


Software Design

Click on a class in the diagram to view the javadocs.

Class Diagram


You are to implement two classes in the above design:

BinaryTree, which implements the I_BinaryTree Interface.   You must use a linked implementation.  Your implementations must match the javadoc class specifications exactly. (If you say "implements I_BinaryTree", then the compiler will ensure that your specification matches.)

AnimalsGame, the client application that interacts with the user.  There are no javadocs for the client application.  You must design your own solution following the principles for maintainable design presented in class.

Here are the .class files for the instructor components, in a JAR formatted file:   Project3.jar

Properties File

You must design your client application so that no messages displayed to the user are hard-coded in your application.  Instead, your application will read the messages from a text data file called a properties file, specifically, animals.properties.  The Java tutorial has a good explanation and examples of how to load a properties file.  (Your application will not need to write a properties file).  Here is a sample code segment:

        try
        {
            messages.load(new FileInputStream("animals.properties"));
        }
        catch (java.io.IOException e)
        {
            System.err.println("Unable to load properties file.");
            System.exit(1);
        }


Compiling your module on Unix (Vogon)

Download the Project3.jar file into your working directory.
wget  http://www.csc.calpoly.edu/%7Ejdalbey/103/Projects/Animals/Project3.jar

Compile your modules
javac -classpath Project3.jar  BinaryTree.java
javac -classpath .:Project3.jar  AnimalsGame.java

Execute the application
java -classpath .:Project3.jar  AnimalsGame


Testing

You will create a unit test for the BinaryTree class as a scheduled lab activity.
You must do your own system testing for the Animals Game.
The instructor will supply his own unit tests and system tests when he grades your submission.
When you application is tested, it will be run with a different properties file than the sample above.

Sample Execution

Here is a sample execution.

Grading

This project has two due dates.  For the first due date, submit your BinaryTree class.
For the second due date, submit your AnimalsGame class.

60%  Correct functionality (Your program will be graded with instructor's test data).
         BinaryTree unit tests (30%)
         AnimalsGame system tests (30%)
25%  Algorithm design meets quality standards for flexibility, maintainability, etc. (View common flaws).
15%  Coding standards are followed.

Remember, your program will be tested on Vogon.  Code that does not compile will receive a grade of zero.



Handing in Your Source Electronically
       handin  graderjd  Project3  BinaryTree.java
       handin  graderjd  Project3  AnimalsGame.java

Submitting a Printout of your program

Printout the source code for your submission.

To avoid being late, your printout must be on the table at the front of the classroom before the official class starting time at the first class meeting after the due date.
 

Extra Credit

Enhance your application so that a game can be saved to secondary storage and later reloaded.

When the user enters 'q' to quit a game, the current state of the game should be saved to
a data file.   The application should display "Game saved to (filename goes here)."

When the application starts, it should check to see if a saved game file exists.
If there is no saved game file, then the game should begin with an empty tree as in
the original game. 
If a saved game file is found, display "Loading game from (filename goes here)."
and then begin by displaying the first question from the tree.



Testing

Upload your program and the Project3.jar to Vogon.
Use your secure shell client program, the unix script command, or some other
mechanism to capture the output of executing your program.
Run your program using redirection to provide the instructors data as input to your program:

java -classpath .:Project3.jar  AnimalsGame   < ~graderjd/Public/ec3_data.txt


It should save the game. Then run the program again and from the keyboard enter
y
n
y
y

and it should guess "Badger."

Create a printout of the above execution.



Handing in Your Source Electronically
       handin  graderjd  Project3EC  BinaryTree.java
       handin  graderjd  Project3EC  AnimalsGame.java

Submitting a Printout of your program

Printout the source code for your submission.  Print the execution as described above.

To avoid being late, your printout must be on the table at the front of the classroom before the official class starting time at the first class meeting after the due date.