CS115 lecture notes, Spring 1999 Week 4, Friday Reading: Chapter 4 Hand back exams. Flow of execution ----------------- public class Buzz { public static void baffle (String blimp) { System.out.println (blimp); zippo ("ping", -5); } public static void zippo (String quince, int flag) { if (flag < 0) { System.out.println (quince + " zoop"); } else { System.out.println ("ik"); baffle (quince); System.out.println ("boo-wa-ha-ha"); } } public static void main (String[] args) { zippo ("rattle", 13); } } Interesting thing here is that main calls zippo zippo calls baffle baffle calls zippo again That would seem to cause a circle, which would continue forever IF zippo called baffle ALL THE TIME. But it doesn't. The second time zippo runs, it just prints something and returns. That allows baffle to return, which allows the first version of zippo to return. Using state diagrams to keep track of method invocations. Homework 3 ---------- A note on the homeworks: the lab time is intended to get you started; you have the rest of the week to finish. If you get off to a good start and then hit a wall, stop. ----- What is a bounding box? It's four parameters that specify the location and size of a rectangle. The rectangle is invisible; it is only used as a guideline. drawOval and fillOval take bounding boxes as arguments and use them to determine the boundaries of the oval. drawRectangle and fillRectangle take bounding boxes as arguments and use them to determine the boundaries of the Rectangle. In the case of rectangles, the shape that is drawn is the same as the bounding box, but in general that is not true. Where do the bounding boxes come from? ------------------------------------- When paint invokes paintBox, it passes a BB that is the size of the whole window. In effect, it gives paintBox permission to draw anywhere on the screen. When paintBox invokes paintSnowperson, it passes a BB that is the same height as the window, one third the width, and centered. Strictly, paintSnowperson should not draw outside the box, although there is actually nothing to prevent it. paintSnowperson then calculates bounding boxes for the head, thorax and abdomen. It uses each BB twice, as an argument to drawOval, and as arguments to paintFace, paintShirt, and, recursively, paintSnowperson. Tricky things ------------- Algebra in coordinate space is a little tricky, so don't worry if it takes a while to sink in. You have to choose some numbers to determine the proportions of the snowman, but you should minimize the number of constants that appear in the code. Instead of making the programmer do computations at programming time, make the computer do it at run-time. That way, if you change any of the proportions, everything gets updated. Resizing the screen is the best way to check your program. If the proportions of the snowpeople change or the ovals stop touching, or they start overlapping, something is wrong. Next week -- recursion. Read 4.15! We'll probably have a quiz.