This lab upgrades the calculator GUI from lab 11 by adding a recursive summation function.

Do This ...

Create a lab14 folder and copy your solution to lab 11 into that folder. Review and rerun the code to make sure that you understand it and that it still runs properly. Be sure to copy and rerun the unit tests as well.

Recursion

This exercise is very similar to the factorial example covered in the text, the video and the class.

Exercise 14.1

Add a summation method to your calculator that receives a non-negative integer n and returns the sum of the numbers from 0 to n. For example, the summation of 4 is 10 (i.e., 0+1+2+3+4). Implement this summation method recursively.

With that method in place, upgrade your calculate() method to compute a summation when it receives the operator code S (i.e., a capital “S”). For the summation, calculate() should use operand1 only and ignore operand2 (note that the user will be required to enter a value for the second operand, but it will simply be ignored in the calculate method). Because the summation method is not designed to handle negative numbers, be sure to raise a ValueError for a negative operand.

Add unit test cases to exercise this new function, including the raised exception and upgrade your GUI controller to offer the new operator. Save this version of your code to turn in.

Checking In

Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria: