Java programs are made out of classes, interfaces, abstract classes. 

In Java classes are containers and interfaces are elements of pure design.

Abstract classes can't be instantiated directly. 

We also use classes to model entities (perhaps in stages). 

The class extension mechanism produces:
  -- inheritance
  -- polymorphism 
  -- dynamic method lookup (overriding)
  -- constructor chaining 
  -- shadowing

How do we sort objects in Java? 

Example: sort a number of players by score (ascending, descending). 

Caulin, Sara, Chris, Aaron, Brian, Tyler, Domino, Timmy, Kevin, Victor,
Jack, Abby, Shuanghao, Ingrid, Ben, Rob, Tori, Sam, Ivy, Carson, Ke, Xinya,
Tristan, Brian, John, Yifan, Daozhen, Yiming, Xinhui, Aiyun, Andrew, Anthony,
Brandon, Sergio, John, Joseph, Drake, Duncan

Longhua why are not here? He was there yesterday with Shengyu.  

public class Player implements Comparable<Player>{
  private String name;
  private int points; 
  public Player(String name, int points) {
    this.name = name;  
    this.points = points; 
  }
  public String toString() {
    return // this.getClass().getName() + 
      this.name + ":" + this.points; 
  }
  public int compareTo(Player other) { // ascending 
    if (this.points > other.points) return 1;
    else if (this.points < other.points) return -1;
    else return this.name.compareTo(other.name); 
  }
}

import java.util.*; 

public class One {
  public static void main(String[] args) {
    ArrayList<Player> players = new ArrayList<Player>(); 
    players.add(new Player("Aiyun"  , 14));
    players.add(new Player("Xinya"  , 23));
    players.add(new Player("Xinhui" , 19));
    players.add(new Player("Xiaohui", 19));    
    System.out.println( players ); 
    Collections.sort( players ); 
    System.out.println( players ); 
  }
}

Exercise:

Assume players in a list, names and points just like above. 

Here's the order I want: 

(a) numbers <= 21 first
(b) numbers >  21 last
(c) numbers <= 21 in descending order
(d) numbers >  21 in descending order 

Think about it for next time. 

Here's a glimpse of generic programming:

-bash-4.1$ cat Comparable.java
interface Comparable<T> {
  public int compareTo(T other);
}
-bash-4.1$ 
-bash-4.1$ cat Collections.java
import java.util.ArrayList;

class Collections {
  public static <T extends Comparable<T>> void sort(ArrayList<T> students) {
    boolean sorted = false;
    while(! sorted) {
      sorted = true;
      for (int i = 0; i < students.size() - 1; i = i + 1) {
        T a = students.get(i);
        T b = students.get(i+1);
        if (a.compareTo(b) > 0) {
           students.set(i, b);
           students.set(i+1, a);
           sorted = false;
           System.out.println("Swapping " + a + " with " + b);
        }
      }
    }
  }
}
-bash-4.1$

This just as an example.

Problem: only allows for a (default/natural) total order. 

The other (more powerful) way of sorting involves Comparator<T>'s. 

Here's another topic that is crucial and similar. 

http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

import javax.swing.JFrame; 

public class One extends JFrame {
  public One() {
    this.setSize(400, 400); 
    this.setVisible(true);  
    Broker b = new Broker(); 
    this.addMouseMotionListener( b ); 
  }
  public static void main(String[] args) {
    One a = new One(); 
    
  }
}

import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

class Broker implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) {
    int x = e.getX(), y = e.getY(); 
    System.out.println("Mouse moved at (" + x + ", " + y + ")"); 
  }
  public void mouseDragged(MouseEvent e) { }
  
}

--