Lecture 42, CS 302-6, December 12

  1. Review:
    1. The Object Class

                                                               i.     Every Class extends Object

                                                             ii.     Override methods

1.    equals(Object o), toString()

                                                            iii.     instanceof

    1. Interfaces

                                                               i.     Definition – a contract between interface and implementing classes

                                                             ii.     Defining

1.    Syntax

2.    Interface type

3.    Abstract methods

4.    static final constants

                                                            iii.     Using

1.    class implements interface

2.    Include all methods

3.    Code or stub

  1. Comparable
    1. Comparable is an interface included in the Java standard package
    2. All classes that implement comparable must implement a.compareTo(Object b) method

                                                               i.     http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Comparable.html

    1. Once you define an interface, there are certain things that you should not change

                                                               i.     For instance, if you add another method, that will break existing code, since it won’t implement that method.

    1. compareTo – returns a negative number if a comes before b, zero if a and b are the same, positive number otherwise

                                                               i.     Note: not just –1 and 1.  Can be any negative or positive values

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

                                                               i.     Just like ArrayList

public Class <className> 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
  1. Exam Review
    1. Sample exam – written #1: E3.java
    2. Sample exam – multichoice #5: Data.java
  2. Homework – For Wednesday – work on Program 4