## CSE 131 Module 6: Recursion

### Exercises

These exercises are part of the asynchronous component of this course. Material is taught and demonstrated, but at times you are asked to complete one of the exercises to help you master the new concepts.

In the repository you are using for this course, look inside the exercises source folder, and you will see assignments organized by name. Before continuing on to the next portion of the asynchronous material, spend some time on the exercise. When you are finished, commit your code so that credit for the exercise can be given.

It is not so important that you get the exercise exactly right. It is important to work through it as best you can before you look at the solution or continue with other work in this module.

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Recursive sum

#### Instructions

• In the exercises6 package of the exercises source folder, create
• Sum.java as a Java class
• SumTest.java as a JUnit test file
• Complete both so that sum is computed as defined by the explicitly recursive formula:
• sum(n) = sum(n-1) + n, if n > 0
• sum(n) = 0, otherwise
• Write some tests for sum and run the unit test file to make sure your function works.
Remember that you have to have a @Test directive above any unit test method, as shown in the video.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

#### Instructions

• In the exercises6 package of the exercises source folder, complete the
• Complete both so that addition is computed as defined by the explicitly recursive formula:
• add(x,y) = x, if y = 0
• Write some more tests for add and run the unit test file to make sure your function works.
Remember that you have to have a @Test directive above any unit test method, as shown in the video.
• Under what conditions on x and y does your method operate correctly?
• How could you complete the addAll(x,y) method so it works for any x and y?
See the code, as a hint is given there.
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Base Cases

Part 1
In the video, one possible explanation for human reproduction was given, namely that a woman is born with all of the babies already inside her that she would ever deliver. This idea is attributed to Anton von Leeuwenhoek who, as a lensmaker, greatly advanced the use of microscopes in studying biology. In terms of recursion and base cases, what is wrong with von Leeuwenhoek's speculation?
Part 2

#### Instructions

• In the exercises6 package of the exercises source folder, find and open the FactorialNoBaseCase class.
• Run the class as a Java Application.
• What happens, and why?
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!

Before starting this exercise:
Be sure to update your repository before beginning this assignment, so that you receive any code you need.

### Graph Paper

#### Instructions

• In the exercises6 package of the exercises source folder, find and open the GraphPaper class.
• Complete the class so that it produces a grid on the screen
• Think about the recursive substructure of drawing a grid
• Think about the base case(s)
After completing this exercise:
Commit and push any files you have changed so that you receive credit for your work!
```

```
```

```
```

```
```

```