2006/11/05

Calculator.java revisted

As discussed yesterday, while implementing the CalulatorService, doubles are not good enough for precision —see the following lecture recommended by Ken Anderson: Floating point.

To fix this problem, the first thing to do is to re-implement the Calculator.java object to accept and return strings, instead of doubles, and operate with BigDecimals:
import java.math.BigDecimal;

public class Calculator extends Object {

public static String add(String addend1, String addend2) {

BigDecimal x = new BigDecimal(addend1);
BigDecimal y = new BigDecimal(addend2);
BigDecimal z = x.add(y);

return z.toString();
}

Re-make the stubs and then, re-implement our CPO.m to use, also, strings:
@implementation CPO
- (IBAction)add:(id)sender
{
id x = [[form cellAtIndex:0] stringValue];
id y = [[form cellAtIndex:1] stringValue];
id z = [CalculatorService add:x in_addend2:y];

[[form cellAtIndex:2] setStringValue:z];
}
@end

I wonder... how does types travel through the Web Service protocols?
Let us find out...