Using the Collection Class

Collection coll = new Collection(10);
Point point1, point2, point3;
Integer intObj1, intObj2;
Double doubleObj1, doubleObj2;

// Create objects to put in collection
   
point1 = new Point(50, 0);
point2 = new Point(0, 100);
point3 = new Point(0, 0);
intObj1 = new Integer(45);
intObj2 = new Integer(8000);
doubleObj1 = new Double(3.14159);
doubleObj2 = new Double(1.414);
      
// Add objects to the collection
 
coll.addItem(intObj1);
coll.addItem(point1);
coll.addItem(point2);
coll.addItem(doubleObj1);
coll.addItem(intObj2);
coll.addItem(doubleObj2);
    
// Print the entire collection (using its toString method)

System.out.println(coll);
 
// Iterate through the collection, handling each kind of object
// in a different way

coll.start();   
while (coll.hasMoreItems()) {
	Object obj = coll.nextItem();
	System.out.println(obj);
	if (obj instanceof Integer)
		System.out.println("\tInteger: " + ((Integer)obj).intValue());
	else if (obj instanceof Double)
		System.out.println("\tDouble: " + ((Double)obj).doubleValue());
	else if (obj instanceof Point)
		System.out.println("\tPoint: distance to origin = " +
		                   ((Point)obj).distance(point3));
	else
		System.out.println("\tUnknown object");
}

The output produced by the code above:

Collection has 6 items:
        45
        (50.0, 0.0)
        (0.0, 100.0)
        3.14159
        8000
        1.414

45
        Integer: 45
(50.0, 0.0)
        Point: distance to origin = 50.0
(0.0, 100.0)
        Point: distance to origin = 100.0
3.14159
        Double: 3.14159
8000
        Integer: 8000
1.414
        Double: 1.414