Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 4: Recursion in CSCI 204 - Introduction to Computer Science II at Bucknell University, Lab Reports of Computer Science

A lab guide for students in the csci 204 course at bucknell university. It covers the topic of recursion, with emacs editing tips and practice problems. Students are expected to write recursive programs, understand the logic of existing recursive functions, and modify a java program to list all files in a directory and its sub-directories recursively.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-ety
koofers-user-ety 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI 204
Introduction to Computer Science II
Bucknell University
Computer Science Department
http://www.eg.bucknell.edu/~csci204/
Lab 4: Recursion
c
February 7, 2007
pf3
pf4
pf5

Partial preview of the text

Download Lab 4: Recursion in CSCI 204 - Introduction to Computer Science II at Bucknell University and more Lab Reports Computer Science in PDF only on Docsity!

CSCI 204

Introduction to Computer Science II Bucknell University Computer Science Department http://www.eg.bucknell.edu/~csci204/

Lab 4: Recursion

© c February 7, 2007

Table of Contents

**1. Objectives

  1. Emacs Editing Tips** 2.1. Speedbar in Emacs **2.2. Towers of Hanoi
  2. Practice With Recursion** 3.1. Factorial **3.2. Listing the Files in a Directory and all its Sub-directories
  3. Hand In**

Section 3: Practice With Recursion 4

2.2. Towers of Hanoi

Since the subject of today’s lab is recursion, here is an emacs tip related to recursion. emacs has a command that will solve the Towers of Hanoi problem. Issue the command M-x hanoi

and emacs will solve the problem with 3 disks. If you want to change the number of disks, use a numeric argument. You can specify a numeric argument by typing C-u followed by the digits of the argument. If you type

C-u 5 M-x hanoi emacs will solve the problem with 5 disks. Most emacs commands will accept a numeric argument. For example, you can move down 25 lines in a file by typing C-u 25 C-n. You can put 70 stars across a page by typing C-u 70 *.

3. Practice With Recursion

3.1. Factorial

Copy the programs Factorial.java and TestFactorial.java from the ~csci204/2007-spring/student/labs/lab04 directory into your lab directory. Compile and run TestFactorial.java. Observe the behavior of the program. Read the program Factorial.java and understand the logic of the program. Then answer the following questions in your handin file.

  1. If we rewrite the first branch of the if-else in the function factorial as

Section 3: Practice With Recursion 5

if (n == 1) return 1; will the final result change? Why?

  1. If we rewrite the branch as if (n == 0) return 0; will the final result change? Why?
  2. If we revise the second branch of the if-else in the function as else return 2 * n * factorial (n - 1); What will happen to the final result? Can you come up a mathematical equivalent of it? Based on the factorial function given above, write a similar recursive function that adds up all the odd numbers between 1 and the given limit. Assume this limit is an odd num- ber. Add a method to the Factorial class to do the work, call it long addOddInts(int n). Also create a testing method for it in the class TestFactorial.java.

3.2. Listing the Files in a Directory and all its Sub-directories

Recursive algorithms are natural for solutions to problems with hierarchical structure. Such a problem is listing all the files in a directory and all of its sub directories. Since the Unix file system is hierarchical, we should immediately think of using a recursive approach.

Section 4: Hand In 7

ls -R ~csci204/2007-spring/student/labs/lab where the -R option recursively lists subdirectories encountered.

4. Hand In

Note: All code you write must be appropriately commented. UML, Javadoc, and other software documents are not necessary for this lab. Please save all answers and code to one handin.java file and print it using a2ps -s 2 or some other double-sided print command. The file redirect command from lab03 may be helpful when creating the handin.java file.

  1. Answers to the questions about the factorial program.
  2. The source code of the revised Factorial.java and TestFactorial.java that contains a recursive method that adds up all the odd numbers between 1 and the given limit. Demonstrate that your program works by entering 7 as the limit.
  3. Source code of your working directory listing program.
  4. Output of your working directory listing program.