CS230 Data Structures, Wellesley College
The Wonders of Bird Watching
The pastoral setting of Wellesley College offers beautiful flora and fauna to explore during those much needed breaks from school work. You recently discovered an assortment of unusual birds that sometimes share the environs with you, and decided to record your observations in a diary. In CS230, you just learned about Arrays and Vectors, which can be used for storing information about a collection of objects. This creates a great opportunity to write a Java program to maintain a database of your bird watching observations!First we will discuss about approaching the problem at hand, how we want to organize the information we need to deal with. In particular, we are interested in identifying what Java classes we should define, what tasks we want to include in each class, and how these classes will communicate with each other.
As always when you are starting a working cs230 session, update your local copy of the cs230 file system,
by running the following commands in a Linux shell:
> cd ~/cs230
> cvs update -d
Check your cs230 directory, to make sure you can see the following files, in the newly created
Bird.javaallows you to create instances of the
Birdclass for storing information about a single bird (
nameof the species,
commentsyou may want to add about a particular instance of a bird).
Observation.javaallows you to create instances of the
Observationclass for storing information about a single bird observation (
birdthat you observed,
location,you saw the bird, the
daythe observation was made, as well as any
commentsyou may want to add about a particular observation.) Study these class definitions! Testing code has also been added to the main method of each of the classes. However, you should add a few more lines of testing code to each.
In the lab today we will work on the definition of a new class named
An instance of a
BirdDiaryVector will store a collection of
instances. Use a Vector to store the collection of Observations.
Can you think of any other type of container that could be used in this case, instead of a Vector?
Think about the instance variables you need to have in the
Then start thinking about the methods you need to define, to make it a useful data base of Bird Observations.
Remember to define an instance method named
toString(), that returns a String representation of all the
contents of a
BirdDiaryVector, as early as possible. This method will be helpful with the
incremental testing of your code.
The implementation of
toString() will be very simple, if you take advantage of the
toString() method in the
class (which, in turn, uses the
toString() method of the
to get the string representation
of each individual
As always, you should test your code incremenatlly, as you add it.
It would be useful and a good practice to include a method to write the diary out to a file, and an other one to load it from a file. Think about the format of the file you write to, as this format will affect the way you read from the file.
What it would take to read the data from the web, instead of a file?
You can find bird data to read in, from the foloowing URL:
Each line in that page, contains just one piece of information, in the order:
bird name, bird sex, bird comments, location, month, day, observation comments.
Next, work on implementing a search method to look for a record in the diary, using the name of a bird as the searching key. Use binary search for this task. You may need to re-think the way you have implemented the add() method, that adds a record to the diary.
As time permits, add a Read-Eval-Print loop to your main method. You will use this loop to test your binary search method. The program is getting into a loop, waiting for the user to type in the keyboard the name of the bird to look for. Then, it reads the user's input, takes the appropriate actions, - in this case, it calls the binary search method -, and presents the user with the results. This continues, until the user types "quit".