Howdy.

(require 2htdp/image)
(require 2htdp/universe)

; A Point is a (make-point Number Number)
(define-struct point (x y))

; A Circle is a (make-circl Point Number)
(define-struct circl (center radius))

; A ManyCircl is one of:
;   -- empty

;   -- (cons Circle ManyCircl)

; A World is a ManyCircl 

; (big-bang initial
;           (to-draw ...)
;           (on-tick ...)
;           (on-mouse ...))

(define initial empty)

; render : World -> Image
; (define (render world)
;   (cond ((empty? world) ...)
;         (else (... (first world) ... (render (rest world)) ...))))
(define (render world)
  (cond ((empty? world) (empty-scene 400 400))
        (else (place-image (circle (circl-radius (first world)) "outline" "red")
                           (point-x (circl-center (first world)))
                           (point-y (circl-center (first world)))
                           (render (rest world))))))

(define sample-world (cons (make-circl (make-point 300 300) 30) 
                      (cons (make-circl (make-point 200 100) 80)
                       (cons (make-circl (make-point  50  50) 10) empty))))

; enlarge : circl -> circl
; increases the radius by 1 pixel 
(define (enlarge circl)
  (make-circl (circl-center circl) (add1 (circl-radius circl))))

; update : world -> world

(define (update world)
  (cond ((empty? world) world)
        (else (cons (enlarge (first world))
                    (update (rest world))))))

; meh : world x y event -> world

(define (meh world x y event)
  (cond ((mouse=? "button-down" event) 
         (cons (make-circl (make-point x y) 1)
               world))
        (else world)))

(big-bang sample-world
          (to-draw render)
          (on-tick update)
          (on-mouse meh))

Run this: how do you code it in Java?

How do you define a Big Bang in Java? 

How does the world in the program above manage an arbitrary number of circles?

Two answers:

  (a) arrays

  (b) array lists

Motivation:

http://www.cs.indiana.edu/classes/c212-dgerman/fall2015/hwFour.html

E6.6 page 295

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int[] a; // declaration 
> int b[];
> b 
null
> b = new int[6]; // allocation 
> b
{ 0, 0, 0, 0, 0, 0 }
> b[0] = 3 // initialization (one element at a time)
3
> b
{ 3, 0, 0, 0, 0, 0 }
> b[1] = -2
-2
> b
{ 3, -2, 0, 0, 0, 0 }
> b[3] = 12
12
> b
{ 3, -2, 0, 12, 0, 0 }
> b.length
6


So let's write this code:

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
     int[] a; 
     a = new int[0];
     System.out.println( Arrays.toString( a ) ); 
     int number = 2; 
     int[] b = Arrays.copyOf( a , a.length + 1); 
     b[b.length-1] = number; 
     System.out.println( Arrays.toString( b ) ); 
     b = Arrays.copyOf( b , b.length + 1); 
     number = -3; 
     b[b.length - 1] = number; 
     System.out.println( Arrays.toString( b ) ); 
  }
}

I can now write the following: 

import java.util.Arrays; 
import java.util.Scanner; 

public class Two {
  public static void main(String[] args) {
    Scanner a = new Scanner(System.in);  
    int[] b = new int[0]; 
    System.out.print("Number: "); 
    // int number = Integer.parseInt(a.nextLine()); 
    String line = a.nextLine(); 
    while (! line.equals("bye")) {
      int number = Integer.parseInt( line ); 
      b = Arrays.copyOf(b, b.length + 1); 
      b[b.length-1] = number; 
      System.out.println(Arrays.toString( b )); 
      System.out.print("Number: "); 
      line = a.nextLine();       
    }
    Arrays.sort( b ); 
    System.out.println(Arrays.toString( b ));     
  }
}

Can we do better? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> import java.util.ArrayList
> ArrayList<String> a;
> String[] b;
> String c[];
> a = new ArrayList<String>();
> a
[]
> a.add("Laura")
true
> a
[Laura]
> a.add("Leslie");
> a
[Laura, Leslie]
> a.add("Alex");
> a.add("Chris");
> a.add("Dylan");
> a
[Laura, Leslie, Alex, Chris, Dylan]


Let's write this code: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Integer a = new Integer(2);
> a
2


Wrapper classes. 

import java.util.ArrayList; 
import java.util.Scanner; 
import java.util.Collections; 

public class Three {
  public static void main(String[] args) {
     ArrayList<Integer> a = new ArrayList<Integer>(); 
     System.out.println( a ); 
     Scanner in = new Scanner(System.in); 
     System.out.println("Number: "); 
     String line = in.nextLine(); 
     while (! line.equals("bye")) {
       Integer number = Integer.parseInt(line); 
       a.add(number); 
       System.out.println( a ); 
       System.out.print("Number: "); 
       line = in.nextLine(); 
     }
     Collections.sort( a ); 
     System.out.println( a );      
  }
}

See you on Wednesday. 

--