Today: 

(a) what can we do with interfaces?

(b) why do we need abstract classes? 

(c) how do we sort in the most general way? 

(d) can we do better than what we came up with at (c)? 

(e) can we do even better than what we came up with at (d)? 

We also described Homework Five:

http://www.cs.indiana.edu/classes/c212-dgerman/sum2013/hwFive.html

It is an exercise in object-oriented design with some (restricted) scaffolded help. 

Now let's remind ourselves the code we wrote yesterday: 


class Rectangle {
  
}


class Circle {
  public String toString() {
    return "Circle!";  
  }
}

import java.util.*; 

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

Let's add features that allow us to calculate and report areas: 

class Circle {
  int radius;
  Circle(int r) {
    this.radius = r;  
  }
  double area() {
    return Math.PI * Math.pow(this.radius, 2);  
  }
  public String toString() {
    return "Circle: " + this.area();  
  }
}

class Rectangle {
  int width, height; 
  Rectangle(int w, int h) {
    this.width = w;
    this.height = h; 
  }
  double area() {
    return this.width * this.height;  
  }
  public String toString() {
    return "Rectangle: " + this.area();  
  }
}

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Object> shapes = new ArrayList<Object>(); 
    shapes.add(new Circle(1)); 
    shapes.add(new Rectangle(3, 2)); 
    shapes.add(new Circle(2)); 
    shapes.add("something"); 
    shapes.add(new Scanner("whatever you may want to add")); 
    System.out.println( shapes ); 
    for (Object a : shapes) 
      System.out.println( a ); 
    for (Object a : shapes) 
      if (a instanceof Circle) 
        System.out.println( ((Circle)a).area() ); 
      else if (a instanceof Rectangle) 
        System.out.println( ((Rectangle)a).area() ); 
      else
        System.out.println( a + " is not a shape.");
  }
}

Now the answer to the first question:

public interface Shape {
  public double area();  
}

class Circle implements Shape {
  int radius;
  Circle(int r) {
    this.radius = r;  
  }
  public double area() {
    return Math.PI * Math.pow(this.radius, 2);  
  }
  public String toString() {
    return "Circle: " + this.area();  
  }
}

class Rectangle implements Shape {
  int width, height; 
  Rectangle(int w, int h) {
    this.width = w;
    this.height = h; 
  }
  public double area() {
    return this.width * this.height;  
  }
  public String toString() {
    return "Rectangle: " + this.area();  
  }
}

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Shape> shapes = new ArrayList<Shape>(); 
    shapes.add(new Circle(1)); 
    shapes.add(new Rectangle(3, 2)); 
    shapes.add(new Circle(2)); 
    System.out.println( shapes ); 
    for (Shape a : shapes) 
      System.out.println( a.area() ); 
  }
}

What good are abstract classes? 

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  Point() {
    this(0, 0);  
  }
  public String toString() {
    return "Point at (" + this.x + ", " + this.y + ")";  
  }
}

abstract class Shape {
  Point location;
  abstract double area();  
}

class Circle extends Shape {
  // Point location; 
  int radius;
  Circle(int r, Point center) {
    this.radius = r;  
    this.location = center; 
  }
  public double area() {
    return Math.PI * Math.pow(this.radius, 2);  
  }
  public String toString() {
    return "Circle: " + this.area();  
  }
}

class Rectangle extends Shape {
  // Point location;
  int width, height; 
  Rectangle(int w, int h, Point topLeft) {
    this.width = w;
    this.height = h; 
    this.location = topLeft; 
  }
  public double area() {
    return this.width * this.height;  
  }
  public String toString() {
    return "Rectangle: " + this.area();  
  }
}

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Shape> shapes = new ArrayList<Shape>(); 
    shapes.add(new Circle(1, new Point(3, 4))); 
    shapes.add(new Rectangle(3, 2, new Point (-1, -1))); 
    shapes.add(new Circle(2, new Point())); 
    System.out.println( shapes ); 
    for (Shape a : shapes) 
      System.out.println( a.area() + " " + a.location ); 
  }
}

Minute paper: 

  How could you sort a bunch of objects that you don't know anything SPECIFIC about? 

Please send a quick answer by e-mail to dgerman@indiana.edu as attendance for the day. 

Tomorrow we will provide Java's answer(s) to this question.