public class Circle {
  public String area() {
    return "pi times r squared";
  }
}

public class Triangle {
  public String area() {
    return "heron's formula";
  }
}

import java.util.ArrayList;

public class Monday {
  public static void main(String[] args) {
    ArrayList<...> a = new ArrayList<...>(); 
    a.add(new Circle()); 
    a.add(new Triangle()); 
  }
}

With help from Ben and Payton:

import java.util.ArrayList;

public class Monday {
  public static void main(String[] args) {
    ArrayList<Object> a = new ArrayList<Object>(); 
    a.add(new Circle()); 
    a.add(new Triangle()); 
    System.out.println( a ); 
  }
}

So we work out this program:

import java.util.ArrayList;

public class Monday {
  public static void main(String[] args) {
    ArrayList<Object> a = new ArrayList<Object>(); 
    a.add(new Circle()); // because of polymorphism (a Unicorn is a Horse)
    a.add(new Triangle()); 
    System.out.println( a );
    Circle c = new Circle(); 
    System.out.println( c.area() ); 
    a.add( c ); 
    System.out.println( a );
    System.out.println( a.get(2) ); // this is a Circle object 
    // System.out.println( a.get(2).area() ); // bad, area does exist in Object
    System.out.println( ((Circle)a.get(2)).area() ); // casting 
    Object e = a.get(1); 
    System.out.println( e ); // a triangle 
    if ( e instanceof Triangle) 
      System.out.println( ((Triangle)a.get(1)).area() ); // casting 
  }
}

--

abstract public class Shape {
  abstract public String area();
}

public class Circle extends Shape {
  public String area() {
    return this + "'s area is pi times r squared";
  }
}

public class Triangle extends Shape {
  public String area() {
    return this + " is a triangle with area calculated by heron's formula";
  }
}

import java.util.ArrayList;

public class Monday {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add(new Circle()); // because of polymorphism (a Unicorn is a Horse)
    a.add(new Triangle()); 
    System.out.println( a );
    Circle c = new Circle(); 
    System.out.println( c.area() ); 
    a.add( c ); 
    System.out.println( a );
    System.out.println( a.get(2) ); // this is a Circle object 
    System.out.println( a.get(2).area() ); // no longer bad, dynamic method lookup    
  }
}

--

abstract public class Shape {
  protected int x, y; 
  public Shape(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  abstract public String area();
}

public class Circle extends Shape {
  public Circle(int x, int y) {
    super(x, y); // constructor chaining 
  }
  public String area() {
    return this + "'s area is pi times r squared";
  }
  public String toString() {
    return "Circle at (" + this.x + ", " + this.y + ")"; // this?
  }
}

public class Triangle extends Shape {
  public Triangle(int x, int y) {
    super(x, y); // constructor chaining 
  }
  public String area() {
    return this + " is a triangle with area calculated by heron's formula";
  }
}

public class Triangle extends Shape {
  public Triangle(int x, int y) {
    super(x, y); // constructor chaining 
  }
  public String area() {
    return this + " is a triangle with area calculated by heron's formula";
  }
}

--


interface Shape {
  String area();
}

public class Circle implements Shape {
  public String area() {
    return this + "'s area is pi times r squared";
  }
  public String toString() {
    return "Circle at (" + this.x + ", " + this.y + ")"; // this?
  }
}


public class Triangle implements Shape {
  public String area() {
    return this + " is a triangle with area calculated by heron's formula";
  }
}

import java.util.ArrayList;

public class Monday {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add(new Circle()); // because of polymorphism (a Unicorn is a Horse)
    a.add(new Triangle()); 
    System.out.println( a );
    Circle c = new Circle(); 
    System.out.println( c.area() ); 
    a.add( c ); 
    System.out.println( a ); // watch what gets printed 
    System.out.println( a.get(2) ); // this is a Circle object 
    System.out.println( a.get(2).area() ); // no longer bad, dynamic method lookup    
  }
}


--
































Start from this today:

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

How do we do this in Java?