Lab 1510 - Paper Folds

Description

Take a piece of paper and fold it in half. Unfold it and there is one fold, which I'll signify with a "v". If you fold the paper twice (always keeping the folds in the same direction and the creases parallel), it will have three folds when you unfold it, and they will be in a "^ v v" pattern. Fold a piece of paper three times, and you will see "^ ^ v v ^ v v" as the sequence of folds.

Folding a piece of already folded paper sounds a lot like recurison, and indeed it is. In case you can't see the algorithm, here it is:

This actually makes sense, if you think about it, or you can just take it on faith as you commonly do when writing recursive functions.

Assignment

Write a class, PaperFold, that has a recursive routine that returns a string representing the fold pattern for a paper folded n times. Here is code to test your class:

        class TestPaperFolds
        {
            public static void main(String[] args)
            {
                PaperFold paper = new PaperFold();
                System.out.println( paper.fold(4) );
            }
        }

You may want to write a private helper function to generate a string that takes a fold sequence and returns it reversed and flipped over such that a "v ^ ^ ^" becomes a "v v v ^". As in many recursive solutions, expect the fold method to be extremely simple (and of course recursive).


Copyright © 2010 by Asylum Computer Services LLC Return to CS Labs