Looking over these files, you should thoroughly understand the
ComparableMLApp class, and see that the
SearchDriver and BinSearcher classes are very
similar to classes we have already seen. The
BinSearcher class, though, will do a binary search through
any list of Comparable objects, not just Strings. In fact, the
SearchDriver is currently set up to use the same
BinSearcher object (an instance variable) for both String
and Student objects.
NOTE: In lab, you will be turningStudent,Bird,StoreItem, andRecipeinto classes that implementComparable, and then running tests on them from theSearchDriver. You will only modify the concrete list item classes (Student, etc.) andSearchDriver. YouDO NOT NEEDto modifyComparableMLApporBinSearcher.
Before you can run the program, you will have to complete the
Student class. (Or comment out the code in the
runBinarySearchTests method in the
SearchDriver class that creates the Student
list and uses Student objects.)
Comparisons of students will be based on names; in other words, if student A's name comes before student B's name lexicographically, then student A will come before student B. We will also assume that student names are unique (no two students have exactly the same name).
Student class and specify that it implements
Comparable<Student>.
Student class has two
constructors, but rather than include almost the exact same code twice,
we are calling one constructor from another. The way to call a
constructor from within another constructor in the same class is to call
this as if it's a method.
(This is a new use for the this keyword.)
You do not need to modify the constructors unless you decide to add new
instance variables to the Student class, but you should
look them over and understand them.
toString method and make sure you feel
comfortable with it. (You do not need to change it, although you may
change the formatting if you want to.)
Note that a class's
toString method does not need to include all the information
about an object; just enough to identify it.
compareTo method. Since the comparison will
be based on names, return what a comparison of this object's name
and the other object's name would return. Remove the "CODE MISSING"
comment since there shouldn't be any code missing anymore.
Test your code with the tests that are already in the
SearchDriver class.
Understanding the test driver code:
searchAndReport helper
method to understand what is going on.
When is it printing information from the parameter, and when is it
printing information from the object found in the list?
compareTo method in the
Student class to find the explanation.
createListOfStudents method (remember that students should
be sorted by name in the list because we're
doing a binary search!), and then, back in the public
runBinarySearchTests
method, call runBinSearchTests a few more times to test the
code more thoroughly.
What are good test cases? A common test is to search for some item in the middle of the list. Good tests would also include:These are called boundary conditions or boundary tests.
- search for the first item in the list
- search for the last item in the list
- search for an item not in the list
Next, write a Bird class for tracking banded birds by ID numbers. Bird comparisons will be based on band numbers.
Comparable<Bird>.toString method (include the band number
& species).equals method.
Notice how the equals method for Bird is different
from the equals method for Student. Why is that? Is
the difference relevant to compareTo?
compareTo method. You
will need to
test equality separately from less-than/greater-than. (Or you can
implement the entire method with one subtraction statement.)
SearchDriver's
runBinarySearchTests method. Add some additional
tests. (You may also add more birds to the
createListOfBirds method if you like.)
Write a StoreItem class to keep track of items in a store. Decide what store items should be ordered based on. (For example, name? Price?)
Comparable<StoreItem>.toString method (include at least the
item name and id).compareTo method. equals method.
If you do your comparison on an attribute other than name,
change the equals method to use the same attribute.
In other words, if compareTo is based on price, then
equals should be based on price also.SearchDriver's
runBinarySearchTests method.
Comparable<T>
(where T is Recipe or whatever class
you created).toString method (include at least the
recipe name and id).compareTo method. SearchDriver's
runBinarySearchTests method.