CPSC 170 Lab 6: Recursion and Induction

As usual, create a lab6 subdirectory for today's lab, open this document in Mozilla, and start emacs.
  1. Computing a positive integer power of a number is easily seen as a recursive process:
    x0 = 1
    xy = x*xy-1  for all y > 1
    
    File Power.java contains a main program that reads in integers base and exp and calls method power to compute baseexp. Fill in the code for power to make it a recursive method to do the power computation.

    Print Power.java.

  2. The Fibonacci sequence is a well-known mathematical sequence in which each term is the sum of the two previous terms. More specifically, if fib(n) is the nth term of the sequence, then the sequence can be defined as follows:
        fib(0) = 0
        fib(1) = 1
        fib(n) = fib(n-1) + fib(n-2)  n>1
    
    For example, the first eight elements of the sequence are 0,1,1,2,3,5,8,13.

    Because the Fibonacci sequence is defined recursively, it is natural to write a recursive method to determine the nth number in the sequence. File Fib.java contains the skeleton for a program that reads an integer n from and computes and prints the nth Fibonacci number. Save this file to your directory. Following the specification above, fill in the code for method fib so that it recursively computes and returns the nth number in the sequence.

    Print Fib.java.

  3. Printing a string backwards can be done iteratively or recursively. To do it recursively, think of the following specification:

    If s contains any characters (i.e., is not the empty string)

    File Backwards.java contains a program that prompts the user for a string, then calls method printBackwards to print the string backwards. Save this file to your directory and fill in the code for printBackwards using the recursive strategy outlined above. Recall that for a String s in Java,

    So if s="happy", s.length=5, s.charAt(1)=a, and s.substring(2,4) = "pp".

    Print Backwards.java.

  4. A palindrome is a string that is the same forward and backward. In Chapter 3 you saw a program that uses a loop to determine whether a string is a palindrome. However, it is also easy to define a palindrome recursively as follows:

    Write a program that prompts for and reads in a string, then prints a message saying whether it is a palindrome. Your main method should read the string and call a recursive (static) method palindrome that takes a string and returns true if the string is a palindrome, false otherwise.

  5. A fractal is a geometric figure that is self-similar in that any piece of the figure contains a miniature of the entire figure. There are lots of fractals that have interesting mathematical properties (and make pretty pictures); a Sierpinski triangle is a fractal that may be constructed as follows:
    1. Draw a triangle.
    2. Draw a new triangle by connecting the midpoints of the three sides of your original triangle. This should split your triangle into four smaller triangles, one in the center and three around the outside.
    3. Repeat (2) for each of the outside triangles (not the center one). Each of them will split into four yet smaller triangles. Repeat for each of their outside triangles.. and for each of the new ones.. and so on, forever. Draw a few rounds of this on paper to see how it works, then check out the demo to see it on screen. Reload to get another triangle.

    Your job is to write a Java program that draws a Sierpinski triangle. Think about the following:

    When this works, modify it so that when the user clicks, a new, random Sierpinski triangle is drawn. (The demo does this too.) You'll need the usual private inner class to implement the MouseListener interface, and in the mouseClicked method you will need to choose three random points and a random color and repaint. If you followed the guidelines above, nothing will change in your paintComponent or sierpinski methods. (But be sure that paintComponent calls super.paintComponent() as its first statement or it won't erase the previous triangle.)

    Print your Sierpinski program.