Java Reference
In-Depth Information
In the
Command
class it is sufficient to introduce three new elements in
the command selection chain in order to handle the three different bases:
public class
Command {
boolean
nextCommand() {
// ...
try
{
// reads the command from the keyboard
if
(command.equals("
!
")
||
command.equals("-") ...
...
else if
(command.equals("dec"))
calc.setBase(
new
DecimalBase());
else if
(command.equals("bin"))
calc.setBase(
new
BinaryBase());
else if
(command.equals("hex"))
calc.setBase(
new
HexBase());
}
catch
(IOException ioe) { ioe.printStackTrace(); }
return true
;
}
}
6.5.4
Test
The test of the multiple number bases must be conducted together with the
test of the representation format. The goal of the test is to check whether
numbers are represented correctly using all possible combinations of base
and format.
Class
TestFormat
is a simple test that verifies the conformance of the
program with the example presented in Table 6.2.
public class
TestFormat
extends
TestCase {
//...
public
void testFormatBase(){
Calculator calc
#
new
Calculator();
try
{
calc.addOperand("0.75");
assertEquals("0.75",calc.secondOperand());
calc.setBase(
new
BinaryBase());
assertEquals("0.11",calc.secondOperand());
calc.setBase(
new
HexBase());
assertEquals("0.C",calc.secondOperand());
calc.setFormat(
new
FloatingPointFormat());
assertEquals("C.0*10^-1.0",calc.secondOperand());
calc.setBase(
new
BinaryBase());
assertEquals("1.1*10^-1.0",calc.secondOperand());
calc.setBase(
new
DecimalBase());
assertEquals("7.5*10^-1.0",calc.secondOperand());