Computer Science 60
Principles of Computer Science
Spring 2006


Assignment 3: Twenty Questions! [100 Points]
Due Monday, February 6 by 11:59 PM

In this assignment you will implement the 20 Questions Game, described in class, using Java. This is a lot of fun but also requires some careful design. There are also two completely optional bonus problems this week. Most students find this assignment to be more challenging than Assignment 2, so please try to start early!

You will find a compiled version of our solution to this assignment in a file called twenty_questions.class in the directory /cs/cs60/assignments/assignment3. Note that Node.class is the compiled version of Node.java, which in our solution is the class describing a single node in the binary tree. Play this game by running

  java twenty_questions
so that you get a good feel for how the program should work!

Here are the requirements for your program:

The only slightly "grungy" thing is that of getting input from the user. To demonstrate how input works in Java, we have provided a small demonstration program called IO.java in the directory /cs/cs60/assignments/assignment3. You may use any and all of this code in your own code.

Think about the design of your code before starting to program. We will be grading this program for both functionality and good style. Regarding style, keep the following in mind:

Please submit all of the files that comprise your implementation of this game. Undoubtedly, you will have a file describing a node in the binary tree as well as the executable class. If you wish, you may have other files as well. You can use cs60submit multiple times, once for each file that you need to submit. Alternatively, if you type cs60submitall and hit the RETURN key, all of the java files in the directory in which you are currently located will be submitted. cs60submitall will show you the name of each file that it is sumbitting for you.

Optional Bonus Problem 1 (10 Points)

Write another version of the 20 questions game called Bonus20.java which allows the user to read in data from a file when starting a new game and also to save the current state of the game to a file when ending the game. This requires some careful thinking and only a very little bit of code! Specifically, when the game starts, the program asks if the user would like to load in a file that was saved earlier and, if so, prompts the user for the name of the file. When the game ends, the program asks the user if the game should be saved to a file and, if so, prompts the user for the name of the file. Note that we haven't talked about file input and output in class. However, in the directory /cs/cs60/assignments/assignment3/bonus/ you will find two files, one named Fileread.java and one named Filewrite.java which demonstrate the use of files. You may copy and modify these files in your program as you wish. Note that you cannot simply save the tree as a file "as is" because the tree is made of interconnected nodes which are not items that Java knows how to save in a file. However, strings can be easily saved and read from files (as the provided sample code illustrates). Therefore, the file will need to be some sort of string representation of the game tree. How you do this is entirely up to you.

Submit your Bonus20.java file and any other files which your program requires.

Optional Bonus Problem 2 (10 Points)

In class we looked at B-trees (specifically, "2-3-4 trees") - a beautiful data structure for the Dictionary abstract data type. The wonderful thing about this data structure is that all operations can be performed in O(log n) time. Implement a B-tree class in a file called Btree.java. This class should implement the abstract data type provided in the interface DictionaryADT.java in /cs/cs60/assignments/assignment3/bonus. Specifically, the interface supports the following operations:

In addition, you should have a method int height() which returns the height of the tree. Recall from class that the height of a tree is the length of the longest path from the root to a leaf node, where the leaf nodes are assumed to be "null" nodes. Thus, a tree comprising just a single root node with two null children has height 1. An empty tree is a "null" tree and thus has height 0. Do you see why this height method wasn't specified in the DictionaryADT interface, even though we want to have it implemented in the Btree class?

Make sure that your code is well-structured, clean, and readable. Submit your file Btree.java and any other files which it requires. Notice that Btree.java defines a B-tree and is not executable. However, you should write a thorough tester program to make sure that your class functions correctly. We will test your Btree class using our own tester programs.

Last modified January 2006 by Christine Alvarado or Ran Libeskind-Hadas