Starting from:

$30

Assignment A5: Recursion

Assignment A5 (50 marks)
Focus: Recursion
For each question, write the base case(s) and the recursive call in English first, then write your
Java code. Make sure to use appropriate code formatting and structure (e.g., indentation,
brackets, etc.).
Q1. [10 marks] Write a recursive method to compute the following series:
𝑓(𝑖) =
1
2
+
1
4
+
1
6
+ ⋯
1
2𝑖
Write a test program that displays f(i) for i = 1, 2, 3, 4, and 5.
Sample run
Q2. [10 marks] Write a recursive method, reverse, that displays a string in a reverse order.
For example, reverse("UBC-O") displays O-CBU. Use a helper method to improve the
performance of your program.
Write a test program that prompts the user to enter a string and displays its reversal.
Sample run
Q3. [10 marks] Write a recursive method that finds the number of occurrences of a given letter
in a string. Use a helper method to improve the performance of your code.
Write a test program that reads from the user a string and a character, and displays the
number of occurrences for the character in the string.
Sample run
Q4. [20 marks] Write a method void listAllFiles(File dir) that recursively displays
a list of a directory's files and subdirectories. An example is illustrated below. Note the
following in the output:
 Directory names are all capitalized and written between brackets [ ].
 Files and subdirectories are indented in order to illustrate to which parent directory they
belong. Use a helper method void listAllFiles(File dir, String spaces)
where dir represents the root directory (i.e., Folder 1 in the example below) and
spaces stores a number of spaces " " that is incremented every time the method is
recursively called. The helper method should print out spaces followed by the file or
subdirectory names.
Folder structure on the hard drive OUTPUT from your Java Program for
“Folder 1”

Grading
 15 % for logic explanation
 70 % for proper code structure and logic
 15 % for correct syntax and formatting

Submission Instructions
For this assignment, you need to do the following:
1- Create a Java project of which name consists of your student number followed by the
assignment number, e.g., “1234567_A2”.
2- Create one class for each question and write your answer inside that class. Your classes
should have the same name as the question number (e.g., Q1)
3- After solving all questions, open Windows Explorer (or any other file explorer).
4- Navigate to your Java project folder (can be found inside your Eclipse workspace folder).
5- Locate the “src” folder for this project (the folder that includes the source code for all
questions).
6- Zip the “src” folder and rename the zipped file to match your project name (e.g.,
1234567_A2.zip).
7- Submit the zipped file to Canvas
Note that you can resubmit an assignment, but the new submission overwrites the old
submission and receives a new timestamp.

More products