CSC 121A Lab 6 - Spring 2005

Thursday, February 10, 2005
Due: Wednesday, February 16, 2005, 5:00PM

[Course home page] [Java API Documentation]

Answer all numbered and bulleted questions. Upload your answers and program files to the VikingWeb coursework section. Besides uploading your written answers to the VikingWeb, please turn in a printed copy either in class on the due date or leave a copy in the box outside my door later on during the day. (You may turn in the printed copies later than 5PM on the due date, but the files must be uploaded to VikingWeb at or before 5PM.)

You may work in pairs for all parts of this assignment, including the homework exercises.

 

Lists

Here are a pair of files implementing the list data structure we talked about in class: ListNode.java, ListTest.java. Download and compile them, and answer the following questions about the ListTest class:

 

Snowflakes

Here is a drawing of a 6-point star. It was drawn as six lines radiating from the center point. Perhaps it's not very interesting by itself.

Let's make things a bit more interesting by drawing a shrunken copy of the star at the end of each point of the original.

Now maybe you are beginning to see something resembling a snowflake. This is similar to what happens in nature when water vapor forms into ice crystals where there is a particle of dust or a sharp point of a previous ice crystal. Of course, we can get an even more interesting picture by repeating the process of drawing a star at the end of each of the points of this snowflake:

Our goal in this assignment, as you can probably imagine, is to apply recursion to the problem of drawing such a snowflake in a Java applet. For solving this problem recursively, you need to think about the following:

Now, let's get started. Here is skeleton code for the applet and a web page for it: SnowflakeApplet.java, Snowflake.html. Take a look at the source code. The panel class is where the actual snowflake is drawn (see the paintComponent and drawStar methods). In the paintComponenet method, you need to fill in the blanks to have the snowflake centered in the middle of the applet. The width and height of the applet have been obtained for you already.

Let's first draw just the simple six-sided star (the first figure shown above). The drawStar method takes x and y coordinates and the size of each arm of the star to draw. Each of the six lines of the star starts at the center (x,y) and ends at a point on the circle of radius size. The X distance from the center to a point on the circle is size * cos( theta ). The Y distance from the center to a point on the circle is size * sin( theta ). See the figure:

What you need to do is figure out the six values for theta and you can figure out the six endpoint coordinates for the lines. The Java trig. functions are static methods of the Math class and they take radians as arguments.

Here is some skeleton code for the drawStar method once you've figured out the value to use for theta.

double angle = ???????;  // value of a single theta

	for ( int i = 0; i < points; i ++ ) 
        {
	    int xend = (int) ( x + size * Math.cos ( angle * i ) );
	    int yend = (int) ( y + size * Math.sin ( angle * i ) );

	    g.setColor( col );
	    g.drawLine( x, y, xend, yend );
	} // end for

Fill in this method and then compile and run the applet. It should draw the basic six-sided star on the web page. There's nothing recursive here yet-- we'll get to that next...

To draw the next level of the snowflake, smaller stars are drawn at the end of each of the six lines. (See the second figure at the top of this page.) Do you know what the center coordinates of these stars are? Let's say the smaller stars should be about one-third the size of the initial star (that's the sizemult field in the StarflakePanel class).

How can we draw a star at the end of each line? Well, we can do so by using the drawStar() method again. Within the for loop above, you can add a recursive method call to the drawStar method. Then, you also have to remember to handle the base case. The base case in this context can be "when a star is too small, don't draw it." You can use the minsize field of the StarflakePanel class as the minimal size of a star. Add in the line of code below in the appropriate place of the drawStar method:

if ( size <= minsize ) return; // base case 

Now, if your applet compiles, it should run and produce a nice picture of a snow flake. You can adjust the values of the fields of the StarflakePanel class to get snow flakes of different patterns.

 

Homework Exercises

 

Based on material from Introduction to CS using Java by Bradley Kjell, CCSU.



Last modified: Thu Feb 10 09:12:27 EST 2005