You made a new Hat each time with only one rectangle in the ArrayList of the Hat for each trip through the
for loop. So you ended up with 5 Hats. Instead, make an
add method so you can add rectangles to any Hat. Then you can have a Hat with multiple rectangles. In your
h.countHats() statement you get zero because you never did anything with the Hat
h you created before the for loop. Try this:
import java.util.ArrayList;
import java.awt.geom.Rectangle2D;
public class HatRx {
ArrayList<Rectangle2D.Double> rectangles = new ArrayList<Rectangle2D.Double>(5);
public HatRx(){}
void add(double x, double y, double w, double h){
rectangles.add((new Rectangle2D.Double(x,y,w,h) ) );
}
public void countHats(){
System.out.println("There are "+ rectangles.size() +" in list");
}
public static void main(String[] args){
HatRx h = new HatRx();
int limit =5;
int randomLim = 100;
for(int i = 0; i < limit; i++) {
h.add( Math.random()*randomLim, Math.random()*randomLim,
Math.random()*randomLim, Math.random()*randomLim);
}
h.countHats();
}
}