return to lecture index

Lecture #10 (September 23, 2005)

TEST REVIEW! TEST ON MONDAY!

Will we have to use intefaces on this exam?

Interfaces are fair game.

Can you go over the math for Big-O?

If you end up with a model of something that looks like:

5.5n3 + 2n + 7
you drop every thing but the number with the highest power. Because over time the number with the highest power will increase the most. This is why we can drop everything but the one that has the highest power. For this example we would only care about the n3.

In reality we are more concerned about whether something is linear or exponential. For example, whether a remove method for the vector has a constant (linear) cost or is it exponential. For a review of Big-O go look at lecture 4, the Big-O

Can you go over iterators, and why they are useful.

Iterators are useful because if you build an interator then you can change the back end so that it can be used on a linkedlist or an array list. An interator is an interface, which means it is a specification, a list of requirements, methods that lets two objects interact. Anything that implements this interface has to have these methods. You leave it to the class to implement those methods based on what that class needs. For more review of iterators look at lecture 8, LinkedListIterator

We had a question about interfaces. What was the role of the interface is this week's lab.

Yes this lab did have an interface, we had to implement the Comparable interface. Often in java we end up wanting to compare two things. So there is a common need in programming to want to compare two things. The equals method is in the object class and tells us if two objects are the same, but they did not include a compareTo method. When the people wrote java realized that they should have written an compareTo method they had to decide how to make one. Rather than changing the Object class which is very fundamental, they decided to write a interface Comparable that has one method.

interface Comparable{
  public int compareTo(Object o);
}

But this does not tells us what to compare or how to. So when we write a class if it makes sense to compare it, for instance if we wrote a car class, then we write a method public int compareTo(Object o). We then also tell the class that it implements Comparable. For linked list, since we dont always know what the class it is going to contain can just take a generic Comparable. This Comparable says that the only thing that you can hand the LinkedList is something that can implement this method. Now we can make a list in order because we can ask it if it is smaller than the one in front of it.

The object that has the compareTo method can have all kinds of other methods in it and properties. When we write the compareTo method we decide what we want the compareTo method to compare. When we are writing the class we want to type cast it inside our method. That way we cant pass in a monkey and compare it to a car. If we type cast it then the program will die right there and that is good because having the program die is better than having erroneous results.

Is it common to create helper methods for processes we do more than a few times?

It is common to create a method if we are doing something multiple times for code that is several lines long. On the other hand it would not make sense to create a method for System.out.println() because it is just one line of code.

Can you explain the equals method?

By default the equals method asks if the two objects have the same reference.
class Object{
 
  public boolean equals (Object o){
   return(this==o);
  }

}

If you really care about identity, is this my girlfriend, is this my car, that is when the equals operator is really good. But the writers of java don't know how your class should best be compared. So 90% of the time we have to write it over again and over-ride it in the class that we wrote.

Can you go over the doublylinkedlist or circularly linked list?

The doubly linked list is good because the extra reference lets us remove things from the end of the list in linear time instead of having to walk through the whole list. For more info look at lecture 9 or lecture 10

Circularly linkedlists are different in that they dont have a tail reference and it only is useful in times when our problem is circular. We would use it when we wanted to go around and around and around like walking around a study session asking people questions. We dont want to use a circular list instead of a singly linked list because we want to have the tail reference. Also we will usually use singly or doubly linked list because that is what is available in pre written code. For more infor on circularly linked lists look at lecture 9