Howdy.

Minute paper:

  (a) what's your team name? 

  (b) who's on your team?

Mohammed
Natch
Adam
Tianyun 
Mary

--

Sam B
Matt E
Fisher
Priya
David 
Zhibo 

--

Circle 

Triangle

Rectangle

Objects of these kinds fit into an ArrayList<Object>

Let's assume that they all have a method called area()

import java.util.*; 

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

public class Circle {
  public double area() {
    return 3.14;  
  }
}

public class Triangle {
  public double area() {
    return 2.18728; 
  }
}

What do I do? 

import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Object> a = new ArrayList<Object>(); 
    a.add( new Circle() );
    a.add( new Triangle() ); 
    System.out.println( a ); 
    for (Object object : a)
      if (object instanceof Circle)
        System.out.println( ((Circle)object).area() ); 
      else 
        System.out.println( object ); 
  }
}

Adam says:

import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add( new Circle() );
    a.add( new Triangle() ); 
    System.out.println( a ); 
    for (Shape object : a)
      if (object instanceof Circle)
        System.out.println( ((Circle)object).area() ); 
      else 
        System.out.println( object ); 
  }
}

public class Shape {
   
}

public class Circle extends Shape {
  public double area() {
    return 3.14;  
  }
}

public class Triangle extends Shape {
  public double area() {
    return 2.18728; 
  }
}

And now: 

import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add( new Circle() );
    a.add( new Triangle() ); 
    System.out.println( a ); 
    for (Shape object : a)
      System.out.println( object.area() ); 
  }
}

public class Shape {
  public double area() {
    return -2.3; 
  }
}

public class Triangle extends Shape {
  public double area() {
    return 2.18728; 
  }
}

public class Circle extends Shape {
  public double area() {
    return 3.14;  
  }
}

What about negative areas? 

import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add( new Circle() );
    a.add( new Triangle() ); 
    System.out.println( a ); 
    for (Shape object : a) {
      System.out.println( object.area() ); 
      System.out.println( object.talk() );  
    }
  }
}

public class Circle extends Shape {
  public double area() {
    System.out.println("Circle (" + this.x + ", " + this.y + ")"); 
    return 3.14;  
  }
}

public abstract class Shape {
  int x, y; 
  public abstract double area();
  public String talk() {
    return "I am a Shape.";  
  }
}

Abstract classes cannot be instantiated directly. 

If that's the case does it make sense for an abstract class to have a constructor? 

import java.util.*; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Shape> a = new ArrayList<Shape>(); 
    a.add( new Circle(2, 3, 5) );
    a.add( new Triangle( -2, -5) ); 
    System.out.println( a ); 
    for (Shape object : a) {
      System.out.println( object.area() ); 
      System.out.println( object.talk() );  
    }
  }
}

public abstract class Shape {
  int x, y; 
  public Shape(int x, int y) {
    super(); 
    this.x = x;
    this.y = y; 
  }
  public abstract double area();
  public String talk() {
    return "I am a Shape.";  
  }
}

public class Circle extends Shape {
  int radius; 
  public Circle(int x, int y, int radius) {
    super(x, y);  
    this.radius = radius;
  }
  public double area() {
    System.out.println("Circle (" + this.x + ", " + this.y + ")"); 
    return Math.PI * Math.pow(this.radius, 2);  
  }
}


public class Triangle extends Shape {
  public Triangle(int x, int y) {
    super(x, y);  
  }
  public double area() {
    return 2.18728; 
  }
}

Remember that if I define Rectangle without an area() I inherit an abstract method. 

import java.util.ArrayList; 

public class Example {
  public static void main(String[] args) {
    ArrayList<Shape> shapes = new ArrayList<Shape>(); 
    Circle a = new Circle(); 
    shapes.add( a ); 
    for (Shape shape : shapes)
      System.out.println(shape.area());
  }
}

interface Shape {
  public double area(); 
}

public class Circle implements Shape {
  public double area() {
    return 3.14;  
  }
}

Now a sorting problem.

public class Player implements Comparable<Player> {
  private String name; 
  private int points; 
  public Player(String name, int points) {
    this.name = name; 
    this.points = points;
  }
  public String toString() {
    return this.name + ":" + this.points; 
  }
  public int compareTo(Player other) {
    return this.name.compareTo(other.name);  // I delegate to names
    // but you should rewrite this to look at the number of points
  }
}


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

public class Example {
  public static void main(String[] args) {
    Player a = new Player("Leslie", 20);  
    ArrayList<Player> players = new ArrayList<Player>(); 
    players.add( a ); 
    players.add( new Player("Laura", 22) ); 
    players.add( new Player("Larry", 21) ); 
    players.add( new Player("Louis", 19) ); 
    System.out.println( players );
    Collections.sort( players ); 
    System.out.println( players ); // sorted 
  }
}

So this runs and produces: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Example
[Leslie:20, Laura:22, Larry:21, Louis:19]
[Larry:21, Laura:22, Leslie:20, Louis:19]


--