Instructions
- Answer Exercise#1 (see below) from Chapter 4 as a group on your blog with code links to GitHub. Remember to link to the files (not the main GitHub repo).
Question text from the book (slightly edited for our course).
Chapter 3 contained the program Calc.java. It is available on program listings page on the book website.
Calc currently implements one function: it adds two integers. test-driven design to add additional functionality to subtract integers, multiply two integers, and divide two integers. First create a failing test for one of the new functionalities, modify the class until the test passes, then perform any refactoring needed. Repeat until all of the required functionality has been added to your new version of Calc, and all tests pass.
Remember that in TDD, the tests determine the requirements. This means you must encode decisions such as whether the division method returns an integer or a floating point number in automated tests before modifying the software.
Submit all code including your final version of Calc, a screenshot showing that all tests pass. Most importantly, include narrative describing each TDD test created, the changes needed to make it pass, and any refactoring that was necessary.
Answer
First we create the test for this software. We decided to leave the operations as integers since the inputs are such type. For the test it is known that a division by 0 is not possible so we catch an Arithmetic Exception in case this happens. However, Java automatically throws this when it happens, so as long as the tests catches it, the code will pass the test.
The book asks us to create a failing test, so the first version of Calc.java will be called Calc1.java. In here we create an underflow by dividing a small number (1) by a big number (100000) so that the real answer is an even smaller number (0.0001) but the computer can’t hold the bits to that so it will give 0 instead, which is the incorrect answer.

In our case we decided to threat this underflow by throwing an Arithmetic Exception since the test can catch such issue. This Calc.java will pass the test CalcTest.java.
