Define a class of objects called Circle, another one Triangle. 

Create a few of these objects and put them in an array (or ArrayList). 

Then sort them by area, descending. 

public class Point {
  private int x, y; 
  public Point(int x, int y) {
    this.x = x;
    this.y = y; 
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x, 
        dy = this.y - other.y;
    return Math.sqrt( dx * dx + dy * dy ); 
  }
}

public class Line {
  private Point a, b; 
  public Line(Point a, Point b) {
    this.a = a;
    this.b = b; 
  }
  public double length() {
    return this.a.distanceTo(this.b);  
  }
}

public class Circle {
  private Point center;
  private int radius;
  public Circle(Point center, int radius) {
    this.center = center;
    this.radius = radius;
  }
  public double area() {
    return Math.PI * Math.pow(this.radius, 2); 
  } 
}

public class Triangle {
  private Point a, b, c;
  public Triangle(Point a, Point b, Point c) {
    this.a = a;
    this.b = b;
    this.c = c; 
  }
  // https://en.wikipedia.org/wiki/Heron%27s_formula
  public double area() {
    Line a = new Line(this.c, this.b); 
    Line b = new Line(this.a, this.c); 
    Line c = new Line(this.a, this.b); 
    double s = (a.length() + b.length() + c.length())/2; 
    return Math.sqrt( s * (s - a.length()) * (s - b.length()) * (s - c.length()) ); 
  } 
}

So here's what I do:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> ArrayList<Circle> a = new ArrayList<Circle>()
Static Error: Undefined class 'ArrayList'
> import java.util.ArrayList; // auto-import
ArrayList<Circle> a = new ArrayList<Circle>()
> a
[]
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Circle@32c09ce0]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
Static Error: No method in ArrayList<Circle> with name 'add' matches this invocation
    Arguments: (Triangle)
    Candidate signatures: 
        void add(int, Circle)
        boolean add(Circle)


How should I define a so it can hold both types of objects. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<java.lang.Object> a = new ArrayList<java.lang.Object>()
> a
[]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@3ba4c419, Circle@6ac4a7df]


Storage problem solved. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<java.lang.Object> a = new ArrayList<java.lang.Object>()
> a
[]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@3ba4c419, Circle@6ac4a7df]
> Object[] b = new Object[6]
> b
{ null, null, null, null, null, null }
> b[2] = new Circle(new Point(100, 200), 30);
> b
{ null, null, Circle@5ae70659, null, null, null }
> b[0] = new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) )
Triangle@3b7c81b9
> b
{ Triangle@3b7c81b9, null, Circle@5ae70659, null, null, null }
> Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<java.lang.Object> a = new ArrayList<java.lang.Object>()
> a
[]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@3ba4c419, Circle@6ac4a7df]
> Object[] b = new Object[6]
> b
{ null, null, null, null, null, null }
> b[2] = new Circle(new Point(100, 200), 30);
> b
{ null, null, Circle@5ae70659, null, null, null }
> b[0] = new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) )
Triangle@3b7c81b9
> b
{ Triangle@3b7c81b9, null, Circle@5ae70659, null, null, null }


https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html

https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#sort-java.lang.Object:A-

https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

Boom! We now need to understand what an interface is...

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html

https://docs.oracle.com/javase/tutorial/collections/

https://docs.oracle.com/javase/tutorial/reallybigindex.html

https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<Object> a = new ArrayList<Object>()
> a
[]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@3bd17a00, Circle@526a47af]


I can still access the area though... 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<Object> a = new ArrayList<Object>()
> a
[]
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@3bd17a00, Circle@526a47af]
> a.get(0)
Triangle@3bd17a00
> a.get(0).area()
Static Error: No method in Object has name 'area'
> a.get(1).area()
Static Error: No method in Object has name 'area'
> a.get(1)
Circle@526a47af
> ((Circle)a.get(1)).area()
2827.4333882308138
> ((Circle)a.get(0)).area()
java.lang.ClassCastException: From Triangle to Circle
> ((Triangle)a.get(0)).area()
5450.000000000003


The problem is Object does not have area() in it. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<Shape> a = new ArrayList<Shape>()
> a
[]
> a.add(new Circle(new Point(100, 200), 30))
true
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a
[Circle@5b268f76, Triangle@781e82c]
> a.get(0).area()
2827.4333882308138
> a.get(1).area()
5450.000000000003


And we have seen interfaces in action.

Here's another way:

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

public class Circle extends Shape {
  private Point center;
  private int radius;
  public Circle(Point center, int radius) {
    this.center = center;
    this.radius = radius;
  }
  public double area() {
    return Math.PI * Math.pow(this.radius, 2); 
  } 
}

public class Triangle extends Shape {
  private Point a, b, c;
  public Triangle(Point a, Point b, Point c) {
    this.a = a;
    this.b = b;
    this.c = c; 
  }
  // https://en.wikipedia.org/wiki/Heron%27s_formula
  public double area() {
    Line a = new Line(this.c, this.b); 
    Line b = new Line(this.a, this.c); 
    Line c = new Line(this.a, this.b); 
    double s = (a.length() + b.length() + c.length())/2; 
    return Math.sqrt( s * (s - a.length()) * (s - b.length()) * (s - c.length()) ); 
  } 
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.ArrayList;
> ArrayList<Shape> a = new ArrayList<Shape>()
> a.add(new Triangle( new Point(30, 80), new Point(-30, -50), new Point(100, 50) ))
true
> a.add(new Circle(new Point(100, 200), 30))
true
> a
[Triangle@1f3720cf, Circle@6fd45f31]
> a.get(0)
Triangle@1f3720cf
> a.get(0).area()
5450.000000000003
> a.get(1).area()
2827.4333882308138


So now we have three questions:

(a) how do we sort? 

(b) what is constructor chaining?

(c) what is event handling and how do we use it? 

--

  mahazuga: Mary Ann
* serepate: Serena
* hgarciah: Henri
* balbakr: Bakr
* ruifan: Rui
* mzelenin: Matthew
* jnp2: Jay
* zeyang: Zejun
  zhang486: Jingyun
* yiwecao: Yiwei
* rthammon: Ryan
* wang686: Jiaxing
* dweissma: Andrew
* kevcao: Kevin
* ssalmero: Salmeron, Santiago (TA)
* luo23: Yawen
* runxzhao: Runxia
* dgerman: German, Dan-Adrian (Primary Instructor)
  creba: Chris