Exam next week. 

The first exam was a test of reading Java:

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

This second exam checking that you can write simple 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

Wednesday we will practice seating for next Wed, envelopes etc. 

The class extension mechanism

  -- polymorphism

  -- inheritance

  -- dynamic method lookup

  -- constructor chaining

  -- shadowing of variables

Interfaces

Abstract classes

I start demonstrating a JFrame:

import javax.swing.JFrame; 

public 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
  }
}

Here's a second stage:

import javax.swing.JFrame; 

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

I will model Circles:

import java.awt.Color; 

class Circle {
  int x, y; 
  int radius;
  Color color;
  Circle(int x, int y, int radius) {
    this.x = x; 
    this.y = y;
    this.radius = radius; 
  }
}

Then we keep developing and we end up with this:

import javax.swing.JFrame; 

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

Here's the abstraction used within: 

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 being 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 = 6; 
    for (int i = 0; i < numberOfCircles; i++) {
      this.a.add(new Circle((int)(Math.random() * 400), 
                            (int)(Math.random() * 400), 
                            (int) (Math.random() * 60) + 20, 
                            new Color( (float) Math.random(), // red
                                       (float) Math.random(), // green 
                                       (float) Math.random())));  
    }
  }
  public void paintComponent(Graphics g) {
    for (Circle c : this.a) 
      c.draw(g); 
  }
}

And the scene is made of instances of this class: 

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);

  }
}

Compile and run it. 

--