Lecture 43, CS 302-7 and 8, May 7

  1. Various things
    1. Program 4

                                                               i.     Extra credit posted

  1. Review:
    1. Interfaces
  2. Triangle.java
  3. Comparable
    1. Comparable is an interface included in the Java standard package
    2. All classes that implement comparable must implement a.compareTo(<Class> b) method
    3. Once you define an interface, there are certain things that you should not change
    4. compareTo – returns a negative number if a comes before b, zero if a and b are the same, positive number otherwise

                                                               i.     Note: not –1,0,1.  Can be any negative or positive values

                                                             ii.     Make sure to check vs zero, not just check ==-1 or 1

    1. Motivation for type parameter – we want to be able to compare to an object of this class

public interface Comparable<t>{

      int compareTo(T other) ;

}

    1. TypeParameter when declaring ‘implements’ Comparable – usually just the class itself

                                                               i.     Just like ArrayList

implements Comparable<ClassType>

public int compareTo(<ClassType> other){

      if (a) {return –1}…}

    1. Implementing compareTo allows you to use Arrays.sort() on an array of objects of the class