Howdy.

 1. Homework 08

 2. Homework 09

 3. Lab 12

 Reading assignment: Chapters 15, 16 

--

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

public class BigBang extends JComponent implements ActionListener, MouseListener, KeyListener {
  Timer timer; 
  World world; 
  public BigBang(World world) {
    this.world = world; 
    this.addMouseListener(this); 
    this.addKeyListener(this); 
    this.setFocusable(true); 
    this.requestFocus();
  }
  public void start(int delay, int size) {
    JFrame a = new JFrame(); 
    a.add( this ); 
    a.setVisible(true); 
    a.setSize(size, size); 
    this.timer = new Timer(delay, this);  
    this.timer.start(); 
  }
  public void mouseEntered(MouseEvent e) { }
  public void mouseExited(MouseEvent e) { }
  public void mousePressed(MouseEvent e) { 
    this.world.meh(e); 
    this.repaint(); 
  }
  public void mouseReleased(MouseEvent u) { }
  public void mouseClicked(MouseEvent e) { }
  public void keyPressed(KeyEvent e) {
    this.world.keh(e);
    this.repaint();
  }
  public void keyReleased(KeyEvent e) { }  
  public void keyTyped(KeyEvent e) { } 
  // int count;
  public void actionPerformed(ActionEvent e) {
    // this.count += 1; 
    // System.out.println("Ouch" + this.count);     
    this.world.teh();
    this.repaint();
  }
  public void paintComponent(Graphics g) {
    this.world.draw(g); 
  }
}

--

import java.awt.Graphics; 
import java.awt.event.KeyEvent; 
import java.awt.event.MouseEvent; 

interface World {
  void draw(Graphics g);  
  void teh(); 
  void meh(MouseEvent e); 
  void keh(KeyEvent e); 
}

--

import java.awt.*;
import java.awt.event.*;

public class Game implements World {
  Cloud c = new Cloud(); 
  Bullet b = new Bullet(200, 400, 10, new Color( 1.0f, 1.0f, 0.0f) ); 
  //                                         red   green blue 
  public Game() {
    for (int i = 0; i < 50; i++) {
      this.c.add( new Circle( (int) (Math.random() * 300 + 50) , 
                              (int) (Math.random() * 300 + 50), 
                              (int) (Math.random() * 20 + 10),
                              new Color((float) Math.random(),
                                        (float) Math.random(), 
                                        (float) Math.random()))); 
    }
  }
  public void teh() {
    this.b.move(); 
    this.c.collide(this.b); 
  }
  public void meh(MouseEvent e) {
    
  }
  public void keh(KeyEvent e) {
    
  }
  public void draw(Graphics g) {
    g.drawRect(0, 0, 400, 400); 
    this.c.draw(g); 
    this.b.draw(g); 
  }
  public static void main(String[] args) {
    BigBang b = new BigBang(new Game()); 
    b.start(100, 400); 
  }
}

--

import java.util.*;
import java.awt.*;

public class Cloud extends ArrayList<Circle> {
  
  public void draw(Graphics g) {
    for (Circle c : this)
      c.draw(g); 
  }
  
  public void collide(Bullet b) {
    for (int i = 0; i < this.size(); i++) { // better check from the other side
      if (this.get(i).overlaps(b)) {
        this.remove(i); 
        i--; 
      }
    }
  }
  
}

--

import java.awt.*; 

public class Circle {
  int x, y, radius;
  Color c;
  public Circle(int x, int y, int r, Color c) {
    this.x = x; 
    this.y = y;
    this.radius = r;
    this.c = c;
  }
  public void draw(Graphics g) {
    g.setColor(this.c); 
    g.fillOval(this.x - this.radius, this.y - this.radius, 2 * this.radius, 2 * this.radius);  
  }
  public boolean overlaps(Circle other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y; 
    double distance = Math.sqrt( dx * dx + dy * dy );  
    return this.radius + other.radius >= distance;
  }
}

--

import java.awt.*; 

public class Bullet extends Circle {
  int vx = 3, vy = -3; 
  public Bullet(int x, int y, int r, Color c) {
    super(x, y, r, c); 
  }
  public void move() {
    this.x += this.vx; 
    this.y += this.vy;   
    if (this.x > 400) { this.x = 400; this.vx = - this.vx; } 
    if (this.x <   0) { this.x =   0; this.vx = - this.vx; } 
    if (this.y > 400) { this.y = 400; this.vy = - this.vy; } 
    if (this.y <   0) { this.y =   0; this.vy = - this.vy; } 
  }
}

--

 A ManyString is one of:

   -- empty

   -- (cons String ManyString) 

 Please implement this type in Java.

 In your code don't use import at all. 

 http://silo.cs.indiana.edu:8346/c212/fall2016/1114.phps

 http://www.java2novice.com/images/linked_list.png

Three classes: 
 
public class Program {
  public static void main(String[] args) {
    ManyString m = new ManyString(); 
    System.out.println( m ); 
    m.cons( "Pacers" ); 
    System.out.println( m ); 
    System.out.println( m.first() );     
    m.cons( "Cavaliers" );
    System.out.println( m ); 
    // System.out.println( m.rest() ); 
  }
}

public class ManyString {
  Cell head;
  // empty by default
  public void cons(String s) {
    this.head = new Cell(s, this.head);  
  }
  public String first() {
    return this.head.first; 
  }
  public String toString() {
    return "[" + this.head + "]"; 
  }
}

public class Cell {
  String first; 
  Cell rest; 
  public Cell(String s, Cell rest) {
    this.first = s; 
    this.rest = rest;
  }
  public String toString() {
    return this.first + ", " + this.rest; 
  }
}

--