15-200 staff-200@cs Highlights
(Lab #2)


Saturday, September 21, 2002

Question:

I don't really understand what the compareTo method does. I know that it compares the two objects'names and should return -1, 0, or 1 but I don't get how it decideds if one name is greater than the other. Does it have a built in checker for alphabetical order or something? And when you say that the class uses this method for order, does that mean that the number returned by the method is used to place object in alphabetical order?

Answer:

compareTo really depends on what two things are being compared, on numbers this is the standard <, =, > relationships, on Strings it's alphabetical order... you'll need to decide how to compare two of your objects. Also remember that you'll need to create your own objects, the food class is just an example. Yes, you will use compareTo to created a sorted list.


Question:

We are confused by the comments for the insertInOrder method in the LinkedList class. It says it should insert the item after before the first item it finds with a greater value as reported by compareTo(). What does "after before" mean? And what is meant by "greater value"? Should our compareTo() method test if the list is in alphabetical order or something along these lines? Right now, that method returns 0 or 1.

Answer:

I don't have the code in front of me, but it sounds like that comment didn't communicate very well!

Insert in order should assume that the list is already in sorted order and insert the new item in a way that keeps it sorted. In other words, f the list is in sorted order before the method is called, it should remain that way after the method is called. Instead of resorting the whole list, the method should simply insert the item into the "right" place.

"Greater value" is determined by your data class, itself. It should implement the Comparable interface, which includes compareTo(). This method determines the relative order of any two items in the list, and since it is (intended to be) transitive, it can be used iteratively to put the list in order.


Question:

In the instructions for lab for it says that the database should be "interactive", does this mean that we use I/O or can we just use static tests?

Answer:

This means that it should have a menu or other tool for user interaction -- it should be a "product".


Question:

Is the Database class a test driver class?

Answer:

It could be -- but we really want you to test the components individually with test drivers, as well.


Question:

Can our new class similar to "Food.java" have any accessor methods (i.e. getName() )?

Answer:

Sure!


Friday, September 20, 2002

Question:

What does the CompareTo do in the Food.java file? Does it return a 0 or a 1 depending on whether or not they are equal? if so, how does the insertInOrder method in LinkedList.java 'order' the nodes? Also, what does the following mean?

Answer:

Comparable is an interface, any class that implements Comparable must have this compareTo method which is supposed to give you a way to order things. You do NOT instantiate a Comparable, you instantiate a class that implements the interface.

For Integers, compareTo does the standard less than, equal, greater comparison (encoded as -1, 0, and 1 respectively). For Strings it uses alphabetical order. It depends on the class really. Though it's pretty safe to say that if the two objects being compared are equal (eg .equals() returns true) then compareTo should return 0.

As for having the list in order at the end, if I compare any node with it's successor I should always get the same result. Thus after performing this insert, the list should still be sorted. Note that if you just stick an item at the head, then it probably won't remain sorted :)