switch
and while
statements in Java.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.
switch
StatementHere, 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.
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.
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:
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.
while
and do-while
StatementsYou can approximate the square root using a method that implements the following algorithm:
Receivex
.
Setapproximation
=x
/ 2.
Repeat while Math.abs(approximation
2 -x
) >1e-8
approximation
= (approximation
+ (x
/approximation
)) / 2.
Returnapproximation
.
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.
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.
This exercise is very similar to the factorial example covered in the lecture.
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:
Submit all the code and supporting files for the exercises in this lab.