Objectives

Revisiting The Calculator Application

Exercise 5b.1:

Create a new package for this lab exercise and make a copy of the calculator application you built in the last lab, including the Calculator class as well as the GUI controller and the JUnit test driver.

Make sure that they all still operate properly.

The switch Statement

Here, you’ll refactor your calculator operation, that is, you’ll modify the program so that it produces the same output using an improved implementation. Refactored code should pass all the same tests that it passed before the refactoring.

Exercise 5b.2:

Replace the multiple-branch if statement in calculate() with an equivalent switch statement. Don’t forget to throw the appropriate exceptions. Rerun your GUI controller and your unit tests to make sure that the new code works as required.

Add extra cases for alternate function codes:

Add test cases to exercise these new function codes.

Save this code for the next exercise.

The Debugger

Testing can reveal errors, commonly called bugs, in our programs. To help fix these errors, most development environments provide debuggers. A debugger is a program that allows you to control your program�s execution and to inspect your program�s data. Go here to get started with your IDE’s debugger:

Exercise 5b.3:

Try out the Eclipse Java Debugger on both your new switch statement and your old if-else-if statement from the last lab. Set a breakpoint at the very beginning of the switch or the if statement and step through its execution a few times, seeing how the debugger allows you to visualize the control flow of your calculator. Note the differences between the flow of execution of the switch and the if statements.

The while and do-while Statements

You can approximate the square root using a method that implements the following algorithm:

Receive x.
Set approximation = x / 2.
Repeat while Math.abs(approximation2 - x) > 1e-8
      approximation = (approximation + (x / approximation)) / 2.
Return approximation.

This version of the algorithm computes the square root to an accuracy of eight decimal places as specified by the 1e-8 literal in the while condition.

Exercise 5b.4:

Add a square root option to your calculator (code: ‘r’) that returns the square root of the first operand (and ignores the second operand). Write a method, computeRoot(), that implements the algorithm given above and modify calculate() to call this method and return the result. Throw an exception for a negative operand.

Add test cases to exercise this new function, including the thrown exception. Rerun your GUI controller and your unit tests to make sure that the new code works as required. Save your code for the next exercise.

Recursion

This exercise is very similar to the factorial example covered in the lecture.

Exercise 5b.5:

Add a summation method to your calculator that receives an 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). Use the code S (i.e., a capital S) for this function, implement it using recursion, and throw an exception for a negative operand.

Add test cases to exercise this new function, including the thrown exception. Rerun your GUI controller and your unit tests. Save this version of your code to turn in.

Extra Credit: For extra credit:

Checking In

Submit all the code and supporting files for the exercises in this lab.