CIS 17: Java Programming

Project F

Objective:

To implement the Iterable interface and verify the result with a foreach construct.
import java.util.*;

abstract class Shape {
  abstract public void draw();
  abstract public void erase();
  protected void print(String s) {
    System.out.println(s);
  }
}

class Circle extends Shape {
  public void draw()  { print("Circle.draw()" ); }
  public void erase() { print("Circle.erase()"); }
}

class Square extends Shape {
  public void draw()  { print("Square.draw()" ); }
  public void erase() { print("Square.erase()"); }
}

class Triangle extends Shape {
  public void draw()  { print("Triangle.draw()" ); }
  public void erase() { print("Triangle.erase()"); }
}

// To be modified to make the class Iterable:
public class RandomShapeGenerator {
  private Random rand = new Random(47);
  public Shape next() {
    switch(rand.nextInt(3)) {
      default:
      case 0: return new Circle();
      case 1: return new Square();
      case 2: return new Triangle(); }
  }
  public static void main(String[] args) {
    // Verify that the modifications work.
  }
}

Submit your work via the CATE form for Project F.

Legend: method/function keyword literal

2007/04/18