next up previous
Next: What to turn in Up: Homework 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 draw, we would add a line to drawSnowperson. What would that line do? It would invoke drawSnowperson! 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 drawSnowperson 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 t-shirt, it invokes drawSnowperson to put a small snowperson in the abdomen.


next up previous
Next: What to turn in Up: Homework 4: Snowpeople! Previous: Make Mrs. Snowperson pregnant
Allen B. Downey
1999-09-29