Homework Assignment 3

Toward a Simple Key/Value Database

Computer Science II
Object-Oriented Programming

Due: Thursday, February 10, at 8:00 AM


For this assignment, you implement two classes that will serve as the foundation of a simple key/value database. A programmer could use your database to implement a note-taking program, where each note has a label and consists of some text. It could also be used to implement a phone directory or a calendar or a writer's planning program.

We will extend this program a couple of times throughout the semester as we learn more about Java and object-oriented programming.

Your programming task is defined below as a sequence of small requirements. Write your program by implementing one requirement at a time in order. That is how we will grade your program.

Programming Advice

As always, write your program in the style we first learned in Session 1:

Take small steps, and your tests will give you feedback as soon as possible. Use the GenerateTest program to create your test class.

Evaluating the Program

To evaluate the functionality of your submission, we will do two things:

As soon as we encounter a requirement that is not satisfied, we will stop grading. So solving an item lower in the list won't be of any use to you if the you haven't completed the tasks that precede it in the list.

New Java for the Assignment

Let's expand our knowledge of Java with this program.

Comparing Two Objects

In Java, we compare two objects using the equals(Object obj) method. Notice that the argument to this method is of type Object. As a result, we write a method of this sort:

    public boolean equals(Object obj)
        if (! obj instanceof CLASSNAME)
            return false;
        CLASSNAME name = (CLASSNAME) obj;
        // ... code to compare receiver to the argument

... where CLASSNAME is the name of the class that contains the method. If obj is an instance of CLASSNAME, we cast it as such so that we can treat it as such. (This method overrides the equals() defined in class Object.)

Working with Strings

Strings respond to many useful messages that we can use to work with them. In particular, a String will find substrings of itself and compare itself to another String.

Vector, A Dynamically-Sized Collection

One of the shortcomings of using arrays is that we have to know exactly how many objects we need. But in many programs we don't know the number of objects we store until we execute the program.

In such situations, we can use a Vector to hold our objects. Unlike an array, a Vector can hold any number of objects and will grow as necessary. Here are some standard ways to work with Vectors:

If you have any questions about these new Java ideas, please ask questions soon! You do not need to scour the web for more information about these classes and methods, beyond the on-line Java documentation linked.


Write tests and code for each of the following requirements, in order. The words in bold indicate message names. Whenever a requirement says the user can "ask whether...", the expected answer is boolean. Whenever a requirement speaks of a "particular" item, then that item will be an argument to the method.

  1. The user can create an Association between a key and a value. The Association responds to a toString() message by returning a string of the form:


  2. The user can ask an Association whether its keyContains a particular substring.

  3. The user can ask an Association whether its valueContains a particular substring.

  4. The user can ask an Association whether it contains a particular substring in either its key or value.

  5. The user can ask an Association whether it hasKey that is a particular string.

  6. The user can create an AssociationList that contains no Associations.

  7. The user can add an existing Association to an AssociationList.

  8. The user can add a new Association to an AssociationList by sending a key and a value.

  9. The user can ask an AssociationList whether it contains a particular Association.

  10. The user can ask an AssociationList whether it containsKey for a particular key.

  11. The user can ask an AssociationList to lookup a particular key, returning a Vector of the Associations it contains that have that key.

  12. The user can ask an AssociationList to findAll matches for a particular string, returning a Vector containing all Associations that have that string anywhere in their key or value.

  13. The user can ask an AssociationList to remove a particular Association.

  14. The user can ask an AssociationList to update every Association having a particular key by appending a particular string to its value.

You may not add so-called access methods to the Association class. That is, you may not add "get" or "set" methods, whose only behavior is to return a field's value or assign a field's value, respectively.


By the due date and time, submit the files

Be sure that your submission follows all homework submission requirements.

Eugene Wallingford ..... wallingf@cs.uni.edu .... February 3, 2005