created 01/01/03; revised 06/23/2016


Chapter 74 Programming Exercises


Exercise 1

Modify the snowflake method so that different levels of stars are drawn with different colors. Use colors that are all close to pastel blue. Do this by changing the pen color based on the size of the star.

Click here to go back to the main menu.


Exercise 2

Implement the blizzard program: draw a few dozen randomly positioned snowflakes of a few random sizes and a few random colors.

Click here to go back to the main menu.


Exercise 3

Write a program that creates the following figure:

Nested Boxes

Do this by starting with the snowflake program and replacing the method that draws a snowflake with the method

drawBoxes( Graphics gr, int x, int y, int width, int height )

that draws a box centered at (x,y) of given width and height. If width or height are below a minimum, just draw that box. Otherwise, draw the box and then call the function recursively with a smaller width and height.

You will find the drawRect( ulX, ulY, width, height) method of Graphics to be useful.

Click here to go back to the main menu.


Exercise 4

Write a program that creates the following figure:

Sierpinski Triangle

The only drawing method this figure uses is drawLine(). The basic figure is a triangle. But rather than draw the full sized triangle, draw three small triangles:

Write a method

drawTriangle( Grahics graph, int lowerLeftX, int lowerLeftY, int side )

that draws a triangle with a lower left corner at (lowerLeftX, lowerLeftY) if side is small. Otherwise, it draws three triangles with side side/2 nested inside the big triangle.

You will find it useful to remember that the height of an equilateral triangle is side*Math.sqrt(3)/2

Click here to go back to the main menu.


End of Exercises