Howdy. 

We are now preparing for Exam Three and implicitly for the Project. 

Next week: sample project development. 

Project has three stages, so next week:

  (a) Stage One on Tue 

  (b) Stage Two on Thu

  (c) Stage Three in lab.

What is on Exam Three? 

  (a) event-handling (mouse, keyboard, timers, buttons/guis)

  (b) sorting 

  (c) exceptions

  (d) graphics and graphical user interfaces

Lane, Troy, Trevor, Shea, Jing, Chia-Hsuan, Evan, Noor, Rob, Ryan, 
Tommy, Yi, Alex, Andrew, Kongchen, Chenyang, Shengyu, Qingyue,

Goal: sort a list of objects both ascending and descending. 

Previously on C212: Java programs are made of classes, abstract classes
and interfaces. Classes are containers, they contain: static members and
blueprints. Blueprints are good for creating objects, so they contain the
instance members. Constructors are custom initialization procedures. There
is a class extension mechanism that allows us to model in stages. It comes
with the following phenomena: inheritance, polymorphism, dynamic method lookup
(also called overriding), constructor chaining and shadowing of variables. 
Abstract classes are unfinished classes (they contain abstract methods or
simply are not suitable for instantiation). Interfaces are elements of pure
design (i.e., they don't make any kind of committment with respect to the
actual implementation). 

-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$

Let's say that we have some Players and we need to sort them.

My request is to sort them twice: ascending and descending. 

https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html

import java.util.*;

public class Player implements Comparable<Player> {
  
  public int compareTo(Player other) {
    if (this.points < other.points) return -1; // ascending 
    else if (this.points > other.points) return 1; 
    else // return 0; 
      return this.name.compareTo(other.name); 
  }
  
  private int points;
  private String name;
  Player(String name, int points) {
    this.name = name;
    this.points = points; 
  }
  public String toString() {
    return // this.getClass().getName() + "(" + 
      this.name + ":" + this.points; // + ")";  
  }
  public static void main(String[] args) {
    Player a = new Player("Tommy" , 19);  
    Player b = new Player("Troy"  , 19);  
    Player c = new Player("Trevor", 19);  
    Player d = new Player("Shea"  , 21);  
    Player e = new Player("Lane"  , 24);  
    System.out.println( c ); 
    ArrayList<Player> players = new ArrayList<Player>(); 
    players.add(c); 
    players.add(b); 
    players.add(a); 
    players.add(d); 
    players.add(e); 
    players.add(new Player("Jack", 14)); 
    System.out.println( players ); 
    Collections.sort( players ); 
    System.out.println( players ); 
  }
}

Now I have question: is there a drawback to this method? 

Second: how do I notice and report mouse motion? 

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

import javax.swing.JFrame; 

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

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

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

So it's the same thing... 

--