Summer 2014, The College of Saint Rose

Lab 7: Recursion
Due: 4:30 PM, Tuesday, July 15, 2014

In this week's lab, you will get a chance to practice your skills with recursion.

You may work alone or with a partner on this lab. Only one submission per group is needed.

Getting Set Up

To get your BlueJ environment set up for this week's lab assignment, start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this course and choose the name "Lab7" (no spaces) for the project.

Create a document where you will record your answers to the lecture assignment and lab questions. If you use plain text, call it "lab7.txt". If it's a Word document, you can call it whatever you'd like, but when you submit, be sure you convert it to a PDF document "lab7.pdf" before you submit it.

Lecture Assignment Questions

We will usually discuss these questions at the start of class on the lab due date, so no credit can be earned for late submissions of lecture assignment questions.

LA Question 1: In the notes, there are mathematical formulas for the iterative and recursive versions of the sums of the numbers from 1 to n. Write similar formulas for computing the factorial of a number, both iteratively and recursively. (4 points)

LA Question 2: Gaddis Algorithm Workbench 3, p. 959. (2 points)

LA Question 3: Gaddis Algorithm Workbench 4, p. 959. (2 points)

LA Question 4: Gaddis Algorithm Workbench 5, p. 959-960. (2 points)

LA Question 5: Gaddis Algorithm Workbench 6, p. 960. (2 points)

LA Question 6: About how many multiplication operations would it take for the recPower method in the Powers example to raise a value to the 1,000,000th power? What about using fastRecPower? (2 points)

LA Question 7: Draw a memory diagram for the RatioListApplet example showing its state after the following ratios have been added: 1/2, 3/4, 8/3, and a call to getMin is about to return from its base case invocation. Include all variables and parameters in existence at that time from all instances of Ratio and RatioList objects, but only the ratios instance variable of RatioListApplet. (10 points)

Practice Programs

Practice Program: Write a Java application called Factorial.java. It should work much like the Powers example, in this case reading in a positive integer, then computes the factorial of that number. Provide two methods: one with a while or for loop, and a recursive method. (6 points)

Practice Program: Augment the RatioListApplet example to include the following:

• A button and necessary code to multiply all of the Ratio values in the current RatioList by the ratio currently represented in the display text fields. (15 points)
• A button that clears the RatioList. (3 points)
• A button that removes the most recently-inserted Ratio from the RatioList. Note: this is a challenging problem if you have not seen this kind of thing before. Just make your best effort and feel free to ask a lot of questions! (4 points)
• A button that searches for and removes the first instance of a ratio that is the same as one in the display and removes it from the RatioList. Note: this is an even more challenging problem if you have not seen this kind of thing before. Just make your best effort and feel free to ask a lot of questions! (4 points)

Submitting

Before 4:30 PM, Tuesday, July 15, 2014, submit your lab for grading. There are two things you need to do to complete the submission: (i) Copy your file with the answers to the lecture assignment and lab questions into your project directory. Be sure to use the correct file name. If you prepared your answers in Word, export to a PDF file and submit that. (ii) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab7".