We had a help session yesterday. 

We'll have one each Sunday @2-4pm. 

Haleigh, Erin, Ben, Michael, Brian, Danielle, 
Minh, Grant, Vinayak, Daniel, Chen, Mahamat, 
Shane, Kevin, Griffin, Nick Palumbo, Joe, Brandon, Nick Palmer, Phoebe, Shikun D

Project: Tetris.

http://www.greenfoot.org/scenarios/335

http://www.greenfoot.org/support/applets-wont-run.html

There is an exam on Wed, very simple exam. 

There are 9 tasks listed in Homework Ten. 

Solutions have not been posted.

When I post them expect two things:

(a) some interactive templates

(b) some more explanations and code 

For the assignment you need to complete the templates and submit THAT code. 

For the exam study all code posted because I can ask any of that. 

Example: 

6. Creating and Using Timers.

https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=java%20timer

https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html

https://docs.oracle.com/javase/8/docs/api/javax/swing/Timer.html

import javax.swing.*; 

class Six {
  public static void main(String[] args) {
    Timer t = new Timer(500, new Helper() );  
    t.start(); 
  }
}

import java.awt.event.*; 

class Helper implements ActionListener {
  int count = 0; 
  public void actionPerformed(ActionEvent e) {
    this.count += 1;
    System.out.println("Howdy: " + this.count); 
  }
}

Another example: 

import javax.swing.*; 

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

import java.awt.event.*;

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

Example variation: 

import javax.swing.*; 

public class One {
  public static void main(String[] args) {
    JFrame a = new JFrame(); 
    a.setVisible(true);
    a.setSize(400, 400); 
    a.getContentPane().add(new Two()); 
  }
}

import javax.swing.*; 
import java.awt.*;

public class Two extends JComponent {
  int x, y; 
  public Two() {
    this.addMouseMotionListener( new Helper(this) );  
  }
  public void paintComponent(Graphics g) {
    g.drawString("(" + this.x + ", " + this.y + ")", this.x, this.y); 
  }
  
}

import java.awt.event.*; 

public class Helper implements MouseMotionListener {
  Two two; 
  Helper(Two two) {
    this.two = two;  
  }
  public void mouseDragged(MouseEvent e) {
    
  }
  public void mouseMoved(MouseEvent e) {
    this.two.x = e.getX(); 
    this.two.y = e.getY(); 
    this.two.repaint(); 
  }  
}

Now a longer example:

(a) a Student has name, age, gpa. 

(b) sort the students by name, age, gpa. 

https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html

http://www.tutorialspoint.com/java/java_using_comparator.htm

http://zetcode.com/tutorials/javagamestutorial/tetris/

http://zetcode.com/tutorials/javagamestutorial/

http://zetcode.com/tutorials/javaswingtutorial/

class Student {
  String name;
  double gpa;
  int age; 
  Student(String name, double gpa, int age) {
    this.name = name;
    this.gpa = gpa;
    this.age = age; 
  }
  public String toString() {
    return this.name + "(" + this.gpa + "/"+ this.age + ")";  
  }
}

import java.util.*; 

class Seven {
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>(); 
    a.add(new Student("Chris"   , 2.9 , 6)); 
    a.add(new Student("Chris"   , 2.9 , 7)); 
    a.add(new Student("Nick"    , 3.4 , 8)); 
    a.add(new Student("Nicholas", 3.4 , 2)); 
    a.add(new Student("Brian"   , 3.14, 9)); 
    a.add(new Student("Brian"   , 3.14, 1)); 
    a.add(new Student("Brandon" , 3.14, 5)); 
    a.add(new Student("Mike"    , 3.45, 3)); 
    a.add(new Student("Michelle", 3.45, 7)); 
    System.out.println( a ); 
    Collections.sort( a , new Judge() );
    System.out.println( a ); 
      
  }
}

import java.util.*;

class Judge implements Comparator<Student> {
  public int compare(Student a, Student b) {
    if (a.gpa > b.gpa) return -1; // a first 
    else if (a.gpa < b.gpa) return 1; // b first  
    else if (a.age > b.age) return 1; // b first 
    else if (a.age < b.age) return -1; // a first 
    else return 0; // order does not matter 
  }
}

Exercise:

Assume the following types: Orange, Apple, Mango. 

Create and store a few of these in some order in an ArrayList<...>. 

Sort so Mangos come first, followed by Oranges, then by Apples. 

import java.util.*; 

class Six {
  public static void main(String[] args) {
    ArrayList<Fruit> a = new ArrayList<Fruit>(); 
    a.add(new Apple()); 
    a.add(new Mango()); 
    a.add(new Orange()); 
    a.add(new Apple()); 
    a.add(new Orange()); 
    a.add(new Apple()); 
    a.add(new Mango()); 
    System.out.println( a );
    Collections.sort( a ); 
    System.out.println( a );    
  }
}

class Fruit implements Comparable<Fruit> {
  public int compareTo(Fruit other) {
    if      (this instanceof Mango  && other instanceof Mango ) return  0; 
    else if (this instanceof Mango  && other instanceof Apple ) return -1; 
    else if (this instanceof Mango  && other instanceof Orange) return -1; 
    else if (this instanceof Orange && other instanceof Mango ) return  1; 
    else if (this instanceof Orange && other instanceof Apple ) return -1; 
    else if (this instanceof Orange && other instanceof Orange) return  0; 
    else if (this instanceof Apple  && other instanceof Mango ) return  1; 
    else if (this instanceof Apple  && other instanceof Apple ) return  0; 
    else return 1; // if (this instanceof Apple  && other instanceof Orange) return  1; 
  }
}


class Apple extends Fruit {
  
}

class Mango extends Fruit {
   
}

class Orange extends Fruit {
  
}

Run Six to see the sorting happen. 

--