For this part, you'll be writing five
methods that illustrate the power of recursion. The methods will
perform the following operations:
Write a method that takes as parameter
and integer n and returns the factorial of n without using
any loops. The recursive definition of factorial is as follows:
| fact(n) = | { |
1 |
if n == 0 |
|
| n * fact(n-1) |
otherwise |
|||
Write a method that takes as parameter
an integer n
and returns an ArrayList containing all powers of 2 up to the nth
power. Again, use recursion without any loops. The
recursive definition of this function is as
follows:
| pow2(n) = | { |
the empty list |
if n < 0 |
|
| add 2n to the end
of pow2(n-1) |
otherwise |
|||
examples:
Write a recursive method to reverse the
order of the characters in a string. The
reverse of a string is just the last character followed by the reverse
of the rest of the string (or the first character preceded by the
reverse of the rest of the string). As a base case, the reverse
of a
length-0 or length-1 string is the string itself. That is,
| reverse(x) = | { |
x |
if length(x) <= 1 |
|
| remove the first character of
x; reverse the remaining string; add the removed character to the end of x; |
otherwise |
|||
You may want to look up some of java's
String methods, such as charAt
and substring.
examples
% java RecFun reverse alucard
dracula
% java RecFun reverse alabama
amabala
| pal(x) = | { |
true |
if length(x) <= 1 |
|
| x's first char == x's last
char and pal(x with its first
and last characters removed) |
otherwise |
|||
Given two strings s1 and s2, determine
whether or not s2 is a subsequence
of s1. That is, do the characters of s2 appear in s1 in the
correct order, but not necessarily consecutively? I'll let you
work out the details on this one, but here are a couple of hints:
For this part, write a series of
programs that will draw fractal images using recursion. Each
program will have three command line arguments indicating the width and
height of the picture, and the number of levels of recursion to use in
the picture. There are four types of drawing:
You'll need to use the Picture class
from earlier labs for this part. I've added a few methods, so you
need to download a new version, found in the lab11.jar
file.
You can draw a
picture with the bubble pattern shown below recursively. The
basic strategy is this:
1. Draw a circle in the center of the picture. Its radius should be 1/4 the width of the picture. Use the drawCircleFill method.
2. Then divide the picture into
four equal sized areas (A, B, C, and D) as shown in the figure to the
right. Recursively draw bubbles in each of the four areas.
You'll need to have a base case to stop
the recursion at some point, so that you don't get into an endless
series of recursive calls. You can achieve this by setting a
limit on the number of levels of recursive calls that your program
makes. I suggest writing a method with the signature
public static void drawBubbles(Picture
p, int x, int y, int width, int height, int nlevels);
to draw a bubble pattern in the
rectangle with (x,y) in the upper left corner, of the given width and
height. nlevels
represents the number of levels of recursive calls. When you make
a recursive call, decrement the value of nlevels that you pass to
the method. When the level is 0, just draw a single circle and
return.
Call drawBubbles from a main program in
a file called Bubbles.java. Bubbles.java has three command-line
arguments: the width and height of the picture, and the desired
number of recursion levels. The main program should instantiate a
Picture of the specified dimensions, draw a background color, and then
call drawBubbles to draw the bubble pattern. Don't forget to call
picture.display() when
you're finished drawing.
The following shows the result with 6 levels of recursion. With zero levels, there will just be a single circle in the middle. Of course, you can use whatever colors you like.

Next, write a program called
Carpet.java which draws an image
known as Sierpinski's carpet, as shown below:

To create this drawing, divide the work
area into a 3x3 grid. Fill in the center section and then draw a
Sierpinski carpet (with one fewer recursion levels) in each of the
remaining 8
surrounding squares.
You can use the method
drawRectFill(int x, int y, int width,
int
height);
to draw a rectangle filled with the
current color
The picture above shows a Sierpinski
carpet
of order 5.
As with Bubbles, Carpet.java should
accept command-line arguments to indicate the width and height of the
picture, and the number of recursion levels.

You can draw a
Sierpinski gasket as
follows:
The base case (i.e., levels==0) is
simply a triangle drawn inside a rectangle.
To draw a Sierpinski gasket with n
recursion levels (n>0),
locate 3 equal sized rectangles (A, B, C) within the main rectangle, as
shown in the figure to the right. Draw a Sierpinski gasket with
(n-1) levels in each of these three rectangles. For this figure,
all of the triangles that are drawn are of the same size, so don't draw
anything until you get to level 0.
To draw a triangle, you can use the
drawPolyFill method of Picture. You pass in an array of x-values,
y-values, and
a count of the number of points to use. These values represent
the x- and y- coordinates of the vertices of the polygon.
For example, to draw a filled triangle with vertices (3,4), (5,7), and
(6,9), write
int[] xvals = {3, 5, 6};
int[] yvals = {4, 7, 9};
picture.drawPolyFill(xvals, yvals, 3);
Call your program
Gasket.java. Use the same command-line arguments as in Bubbles
and Carpet. The final recursive image you'll be
creating is known as the Koch
Snowflake. In this one, you'll be
drawing
a modified equilateral triangle. The
recursive method is
actually only concerned with drawing one "side" of the triangle, which
is called a Koch curve.
Once you have the Koch curve working, you
can draw a Koch Snowflake with just 3 Koch curves.
A Koch curve from point P to point Q is
described recursively as follows:
The Koch curves of order 0, 1, 2, and 3
(with direction = 0) look like this:

| void setPosition(double x,
double y); |
sets the current position of the
drawing pen to (x,y). |
| void setDirection(double angle); |
sets the current direction of
the drawing pen to the given angle (in degrees). (0 indicates
horizontal to the right; positive directions are measured
counterclockwise; negative directions are measured clockwise) |
| void rotate(double angle); |
rotates the pen direction by the
given number of degrees. |
| void drawLineForward(double
distance); |
draws a line of the given
distance (in pixels), starting at the current pen position and heading
in the current direction. |
Here is an example of the resulting final picture (a Koch snowflake
of order
5):

The directory you submit should contain
the files RecFun.java, Bubbles.java, Carpet.java, Gasket.java, and
Snowflake.java. Be sure to write a comment with your name and a
brief description of tthe program in each file. Also, if you
followed the Honor Code in this
assignment,
insert a paragraph attesting to this fact in the top comment block of
this file
as well.