T-Th 9:05 |
CS 1110: Introduction to Computing Using Java Spring 2012 |
||||||||||
Main
About: Announcements Staff Consultants Times & Places Calendar Materials: Texts DrJava VideoNote Terminology Handouts: Lectures Assignments Labs Assessment: Grading Exams Quizzes Resources: CMS Piazza (link) Piazza (about) AEWs FAQ Java API Style Guide Academic Integrity |
RecursionThere is a PDF version of these instructions, if you would prefer to have that instead. This lab gives you experience with writing recursive functions. All of the functions in this lab will either be recursive functions on Strings, or recursive functions on integers, just as we saw in class. Requirements For This LabThe very first thing that you should do in this lab is to download the file Recursive.java. You should create a new directory on your hard drive for this file. You will note that all of the functions in this file are stubs, and are not fully implemented. Your entire lab will consist of modifying this file. To successfully complete this lab, you should implememnt the first four methods in the file Recursive.java. When you have done this, show your file to a TA. As always, you should try to finish the lab during your section. However, if you do not finish during section, you have until the beginning of lab next week to finish it. You should always do your best to finish during lab hours; remember that labs are graded on effort, not correctness. If you get stuck, do not waste time. Ask the TA or consultant for help. Recursive MethodsRemember that creating and understanding a recursive function involves four points:
In this lab, you are to implement the first four methods from the file Recursive.java. These are the ones listed int the table below:
Even though we only ask you to work on the first 4 functions in lab, you will get greater fluency in recursion if you do them all. So, during the week, every one in a while write one of the functions and test it. You should particularly try some of the integer recursive functions that appear later in Recursive.java (Optional) Recursion and MemoryThis is a completely optional exercise for your own education and enjoyment. At the very end of Recursion.java are several functions called test(). In class, we talked about what happens when you forget your base case: the recursive method keeps creating frames until it runs out of memory. How many frames can it create before it runs out of memory? To find out, hit the Reset button and then type these lines in the Interactions pane, one at a time:
Look at the body of test. From it, you can see that the number in static
variable number is the number of calls made, and thus frames created, until
there was "stack overflow". Write down that number somewhere.
Next, hit the reset button and perform this test:
Here, method test(int), which has one parameter but many local variables, is called many times. Compare the number of frames created here with the number created when calling method test(). Why is one bigger than another?
Finally hit the reset button and perform this test:
Here, a method with 10 parameters is called many times. Compare the number of frames created this time to the number of frames created the other times. Do the numbers make any sense to you? |