I will post midterm scores tonight. 

Let this not be a factor in attending the makeup.

The make up is good practice. Wed in class. 

import java.util.*;

class Student extends ArrayList<Integer> {
  String name; 
  Student(String name) {
    this.name = name;
  }
  public String toString() {
    return this.name ;
  }
}

The final code:

-bash-4.1$ javac Student.java
-bash-4.1$ java Student
Laura[]
Laura[90]
Laura[90, 94]
Laura[90, 94, 94]
-bash-4.1$ pico -w Student.java
-bash-4.1$ cat Student.java
import java.util.*;

class Student extends ArrayList<Integer> {
  String name;
  Student(String name) {
    this.name = name;
  }
  public String toString() {
    return this.name + super.toString();
  }

  public static void main(String[] args) {
    Student a;
    a = new Student("Laura");
    System.out.println( a );
    a.add( 90 );
    System.out.println( a );
    a.add( 94 );
    System.out.println( a );
    a.add( 94 );
    System.out.println( a );
  }
}

-bash-4.1$ javac Student.java
-bash-4.1$ java Student
Laura[]
Laura[90]
Laura[90, 94]
Laura[90, 94, 94]
-bash-4.1$

No more homework. 

Two more lab assignments. 

1. Implement the Cannonball with GUIs/Graphics etc. 

2. Add Rectangles to the program we start today. 

3. You will run some examples from a book and think about them. 

Program: draw circles and allows you to move them with the mouse.

When you move the circle it becomes yellow and all the circles
(later on Shapes) that it comes into contact with also become yellow
but only while it is in contact with. Here's the finished code:



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

class Two extends JFrame {
  Two() {
    this.setVisible(true); 
    this.setSize(400, 500); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     
  }
  public static void main(String[] args) {
    JFrame t = new Two(); 
    Three three = new Three(Integer.parseInt(args[0]));
    t.getContentPane().add( three ); 
  }
}

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x; 
    this.y = y; 
  }
}

class Circle{
  Point center;
  int radius;
  Color color; 
  Circle(Point a, int r, Color c) {
    this.center = a; 
    this.radius = r; 
    this.color = c; 
  } 
  void make(Color c) { this.color = c; } // [6] 
  void draw(Graphics g) {

    g.setColor(this.color); 
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
    g.setColor(Color.BLACK); 
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
  }
  void drawCircleOverlap(Graphics g) {

    g.setColor(Color.YELLOW); 
    g.fillOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
    g.setColor(Color.BLUE); 
    g.drawOval(this.center.x - this.radius, 
               this.center.y - this.radius, 
               2 * this.radius, 2 * this.radius); 
  }
  boolean contains(Point where) {
    int dx = center.x; 
    int dy = center.y; 
    return this.distanceTo(where) < radius;
  }
  int distanceTo(Point where)
  {
    int dy = Math.abs(where.y - center.y);
    int dx = Math.abs(where.x - center.x);
    int distanceTo = (int) (Math.sqrt((dx * dx) + (dy * dy)));
   return distanceTo;    
  }
  void moveTo(Point where) {
    this.center = where; 
  } 
  public String toString() {
    return "Circle at " + this.center + " with radius " + this.radius; 
  }
}

class Three extends JComponent implements MouseListener, MouseMotionListener {
  public void mouseMoved(MouseEvent e) { } 
  public void mouseDragged(MouseEvent e) { 
    System.out.println( e.getX() + ", " + e.getY() );

    if (current != null) { 
      this.current.moveTo( new Point(e.getX(), e.getY()) );
      System.out.println( current ); 
      
      for(Circle r : this.shapes)
      {
        if(current.distanceTo(r.center) < current.radius + r.radius)
        {
          r.make(Color.YELLOW);       // [1]
          current.make(Color.YELLOW); // [2]
        } else {                      // [3]
          r.make(Color.WHITE);        // [4]
        }                             // [5]
      }
      repaint();
    }
  }
  public void mouseClicked(MouseEvent e) { } 

  Circle current;

  public void mousePressed(MouseEvent e) { 
    System.out.println( "Mouse pressed..." ); 
    Point mouse = new Point(e.getX(), e.getY()); 
    for (Circle r : this.shapes) {
      if (r.contains(mouse)) {
        this.current = r;
        break; 
      } 
    }
  } 
  public void mouseReleased(MouseEvent e) { 
    this.current = null; 
  } 
  public void mouseEntered(MouseEvent e) { } 
  public void mouseExited(MouseEvent e) { } 
  ArrayList<Circle> shapes; 
  Three(int size) {

    this.addMouseMotionListener( this ); 
    this.addMouseListener( this ); 

    this.shapes = new ArrayList<Circle>(); 
    for (int i = 0; i < size; i++) {
      Point where = new Point((int)(Math.random() * 300 + 50), 
                              (int)(Math.random() * 300 + 50)); 
      int radius = (int)(Math.random() * 50 + 20);
      Color color = new Color(255, 255, 255);
      this.shapes.add(new Circle(where, radius, color));
    } 
  }  
  public void paintComponent(Graphics g) {
    for (Circle r : this.shapes)
      r.draw( g ); 
  }
}

Let's start developing this program:

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

class Circle {
  int radius, x, y; 
  Circle(int x, int y, int r) {
    this.x = x;
    this.y = y; 
    this.radius = r;
  }
  void draw(Graphics g) {
    g.drawOval(this.x, this.y, 2 * this.radius, 2 * this.radius); 
  }
}

class Program extends JFrame {
  Program(int number) {
    ArrayList<Circle> circles = new ArrayList<Circle>
    Screen a = new Screen(...); 
    this.add( a );
    this.setSize(300, 400); 
    this.setVisible(true); 
    this.setDefaultCloseOperation( EXIT_ON_CLOSE ); 
  } 
  public static void main(String[] args) {
    int number = Integer.parseInt( args[0] ); 
    Program a = new Program( number ); 
  }
}

class Screen extends JComponent {
  int number;
  int count = 0; 
  Screen(...) {
    ... 
  }  
  public void paintComponent(Graphics g) {
    this.count += 1; 
    System.out.println( "Ouch: " + this.count ); 
  } 
}