To make the game more interesting to play, your program should be able to save a game to a file and also to read in an old game from a file. This will allow the user to continue a game that was played earlier. Unfortunately, Java applets cannot either read or write files. Thus the demo below on this web page does not support the file operations.
You will need to create a class to hold the information which will be stored in the animal tree. These objects need to be able to tell whether they represent an animal or question and should be able to return its contents.
This program uses a CardLayout manager. It can be imagined as holding a list of panels, only one of which shows at a time. They are added with the command add(name,myPanel), where name is a string which can be used to bring up the panel. If the name of the layout manager is cardManager, then the panel with name myName can be brought to the top (and shown) with the command cardManager.show(this,myName). The manager remembers the order the panels were inserted, so commands like cardManager.first(this), cardManager.last(this), and cardManager.next(this) also can be used to select among the panels.
One minor complication is that you will likely be calling these methods from inside "listener" inner classes of the frame. If you write just cardManager.first(this) it will refer to the this of the inner class. To get it to call the correct "this", write cardManager.first(AnimalApp.this).
I've provided the code to set up the user interface, add listeners, and have provided the skeletons of the listener classes, but the rest is up to you. You should not have much problem in writing the code at this point, but the logic will need very careful thought! To make life easier, you may work in pairs on this project. If you decide to work in a pair with someone in class, you must let me know at the beginning of lab on Wednesday who your partner is. Once you complete the overall design I suggest that you have one person work on the main game, with the other person specializing in the interaction with the files. Both members of the team will receive the same grade and will be responsible for understanding all of the code in the project. Here is the code you will need for creating, writing to, and reading from files:
// create a new input file with name fileName. DataInputStream inFile = new DataInputStream(new FileInputStream(fileName)); char letter = inFile.readChar(); // read character from inFile and store in letter String contents = inFile.readUTF(); // read string from inFile and store in contents inFile.close(); // close file // create a new output file with name fileName. DataOutputStream outFile = new DataOutputStream(new FileOutputStream(fileName)); outFile.writeChar(letter); // write character from letter onto outFile outFile.writeUTF(contents); // write string from contents onto outFile outFile.close(); // close fileWhen you write to the file you will need to include a one character tag for each string to indicate whether it is a question or answer. I suggest writing 'Q' before each question and 'A' before each answer. It is easiest to read the file back if you write the file either with a pre-order or post-order traversal of the tree. While you could write it using the iterators of the BinaryTree class, it is just as easy to write your own recursive version. To read the file in and recreate the binary tree you will need to write a recursive routine. The trick is to "believe" in recursion, as if you do it will be pretty easy. One helpful hint: at the end of writing a subtree, be sure to bring the cursor back to the root of the subtree (which is generally not the root of the entire tree).
If you would like to earn some extra credit, you can do your own layout for the game rather than using my (boring) layout (though I suggest that you continue to use the card layout manager). Your layout should be easy for users to figure out as well as attractive and creative. The only buttons visible to a user should be those that they are able to productively use (i.e., don't show them buttons they are not allowed to push!).
Be sure to get an early start on this and to do an extremely careful design, as otherwise you will not have much luck on this program!