Looking over these files, you should thoroughly understand the
ComparableMLApp class, and see that the
BinSearcher classes are very
similar to the classes we worked with in class. 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 turning
Recipeinto classes that implement
Comparable, and then running tests on them from the
SearchDriver. You will only modify the concrete list item classes (
Student, etc.) and
DO NOT NEEDto modify
Before you can run the program, you will have to complete the
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).
Studentclass and specify that it implements
Studentclass 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
thisas if it's a method. (This is a new use for the
thiskeyword.) You do not need to modify the constructors unless you decide to add new instance variables to the
Studentclass, but you should look them over and understand them.
toStringmethod 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
toStringmethod does not need to include all the information about an object; just enough to identify it.
compareTomethod. 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
Understanding the test driver code:
Why is the id of Rey Diaz different in the two tests involving her? Why
is the id of Harry Potter the same? Read the code for the private
runBinSearchTests helper method to understand what is going
What happens if you create students without majors? Will they be found? For example what would happen if we had
studentSearch1 = new Student("Hermione Granger");Uncomment the three lines that search for "Rey" in different ways. What do you expect the results to be? Were you right?
createListOfStudentsmethod (remember that students should be sorted by name in the list because we're doing a binary search!), and then, back in the public
runBinSearchTestsa 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:
- search for the first item in the list
- search for the last item in the list
- search for an item not in the list
Imagine writing a Bird class for tracking banded birds by ID numbers. Bird comparisons will be based on band numbers.
toStringmethod (include the band number & species).
equalsmethod. Notice how the
equalsmethod for Bird is different from the
equalsmethod for Student. Why is that? Is the difference relevant to
compareTomethod. You will need to test equality separately from less-than/greater-than.
runBinarySearchTestsmethod. Add some additional tests. (You may also add more birds to the
createListOfBirdsmethod if you like.)
Decide what what store items should be ordered based on. (For example, name? Price?)
toStringmethod (include at least the item name and id).
equalsmethod. If you do your comparison on an attribute other than name, change the
equalsmethod to use the same attribute. In other words, if
compareTois based on price, then
equalsshould be based on price also.
Recipeor whatever class you created).
toStringmethod (include at least the recipe name and id).