next up previous
Next: What to turn in Up: Assignment 4: Snowpeople! Previous: Make Mrs. Snowperson pregnant

Make all snowpeople pregnant

Ok, now let's imagine that instead of making a particular snowperson pregnant, we want to make all snowpeople pregnant. Instead of adding a line to main, we would add a line to paintSnowperson. What would that line do? It would invoke paintSnowperson! Can you do that? Can you invoke a method from within itself? Well, yes, you can, but you have to be careful.

SO DON'T DO THIS YET!!!

Think for a minute. What if we draw a big snowperson, and then put a small snowperson in the abdomen oval. Then we have to put an even smaller snowperson in the small abdomen oval, and so on and so on. The problem is, it would never stop. We would be drawing smaller and smaller snowpeople until doomsday!

One solution is to pick a minimum snowperson size, and say that below that size, we refuse to draw any more snowpeople.

1.
Add a line at the beginning of paintSnowperson that checks the height of the bounding box and returns immediately if it is less than 10.
        if (height < 10) return;

2.
Now that you have that line, it is safe to add code at the end of the method so that after drawing the ovals, and the face, and the Olympic rings on the chest, it invokes paintSnowperson to put a small snowperson in the abdomen.


next up previous
Next: What to turn in Up: Assignment 4: Snowpeople! Previous: Make Mrs. Snowperson pregnant
Allen B. Downey
2/25/1998