Howdy. 

There's an exam next Wed. 

The exam will test how well you can write simple programs in Java.

The first exam tested how well you could read Java, for example: 

http://www.cs.indiana.edu/classes/c212/spr2015/02232015/abcdef.pdf

But now you need to write code, simple programs. 

Here are some examples of such programs:

http://www.cs.indiana.edu/classes/c211/lab5.html

http://www.cs.indiana.edu/classes/c211/lab6.html

http://www.cs.indiana.edu/classes/c211/lab4.html

That's the kind of exercises you need to prepare. 

This Wed 2/25 we will test the seating for the exam next week: 

(a) you will receive envelopes

(b) they will be on your seat

(c) they will contain EEE plus your Lab Assignment for this week

That's the administrative part. 

The class extension mechanism: 

  -- polymorphism

  -- inheritance

  -- dynamic method lookup (overridding) 

  -- shadowing of variables

  -- constructor chaining 

I want to show these things happen in all of Java. 

Today I want to give you the motivation to understand them.

At the end the minute paper (name, signed, date for attendance) will
have two questions (we'll collect them at the end as we always do):

(a) what's the clearest point of the lecture

(b) what's the muddiest point of the lecture

Let's get started:

How do we create our own Big Bang? 

I would like to show you the main raw ingredients. 

Here's an example of JFrame:

import javax.swing.JFrame; 

class One {
  public static void main(String[] args) {
    JFrame a = new JFrame(); 
    // System.out.println( a ); 
    a.setSize(400, 400); 
    a.setVisible(true); 
    // a.setDefaultCloseOperation( 3 ); // JFrame.EXIT_ON_CLOSE
  }
}

Now let's model a simple game: 

import javax.swing.JFrame;

class TV extends JFrame {
  TV() {
    this.setSize(400, 400); 
    this.setVisible(true); 
    // this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  }
  public static void main(String[] args) {
    JFrame a = new TV();   
  }
}

That's a simple model, let's make it more complicated. 

Let's draw something:

import java.awt.Color; 
import java.awt.Graphics; 

class Circle {
  int x, y; // center 
  int radius;
  Color color;
  Circle(int x, int y, int radius, Color color) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    this.color = color; 
  }
  void draw(Graphics g) {
    g.setColor(this.color); 
    g.fillOval( this.x - this.radius, 
                this.y - this.radius, 
                2 * this.radius, 
                2 * this.radius );
    g.setColor(Color.BLACK); 
    g.drawOval( this.x - this.radius, 
                this.y - this.radius, 
                2 * this.radius, 
                2 * this.radius ); 
  }
}
/*
 * Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 4.53 * 100
453.0
> 4.35 * 100
434.99999999999994
> 0.1
0.1
> 0.1 + 0.1
0.2
> 0.1 + 0.1 + 0.1
0.30000000000000004
> 0.1 + 0.1 + 0.1 + 0.1
0.4

*/

import javax.swing.JComponent;
import java.util.ArrayList;
import java.awt.Graphics; 
import java.awt.Color; 
import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent;

class Screen extends JComponent implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) { 
      System.out.println("Mouse moved..."); 
  } 
  public void mouseDragged(MouseEvent e) { 
    int x = e.getX(), y = e.getY();
    System.out.println("Mouse dragged at (" + x + ", " + y + ")"); 
  } 
  ArrayList<Circle> a = new ArrayList<Circle>(); 
  Screen() {
    this.addMouseMotionListener(this); 
    int numberOfCircles = 10;
    for (int i = 0; i < numberOfCircles; i++) {
      this.a.add( new Circle ( (int) (400 * Math.random()), 
                               (int) (400 * Math.random()), 
                               (int) (Math.random() * (80 - 20) + 20), 
                               new Color( (float) Math.random() , 
                                          (float) Math.random() , 
                                          (float) Math.random() ) ) );  
    }
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.a) {
      c.draw(g);  
    }
  }
}

import javax.swing.JFrame;

class TV extends JFrame {
  TV() {
    this.add(new Screen()); 
    this.setSize(400, 400); 
    this.setVisible(true); 
    // this.setDefaultCloseOperation(EXIT_ON_CLOSE); 
  }
  public static void main(String[] args) {
    JFrame a = new TV();   
  }
}

Compile and run this.

We still need to add timer, key event listener. 

But we're already clear of the basic structure, I hope. 

See you on Wed. 

--