Your newBalance variable is a BigDecimal and not a double. I notice the comment in the first method that says "change double newBalance to BigDecimal when working with finances".
You should use BigDecimal when precision is important, such as finances, but at that point it's to late as the value being passed in is a double. If there is any precision loss because of the nature of floating point variables it has already happened.
You should probably do something more like this.
P.S. If you should not put the amount into a double anywhere in the program. You should always deal with it as a String or BigDecimal. BigDecimal has a constructor that takes a String so you can store the value as a BigDecimal as soon as you get the value into your program. [ March 05, 2005: Message edited by: Steven Bell ]
Joined: Oct 04, 2004
Steven, many thanks for your reply.
Unfortunately the getBalance and setBalance signatures and parameters have been defined for me. i.e.
I have to use the following for getBalance and setBalance:
getBalance return type double argument name and data type is ( ) setBalance return type void argument name and data type is (double newBalance)
That's what I'm getting confused about.
As I am dealing with finances I need to use BigDecimal but how on earth do I implement that with these method constraints? They have also stated no additional methods unless absolutely necessary!!
Joined: Dec 29, 2004
well, you're about halfway screwed as far as I can tell. Using BigDecimal will keep you from having some precision loss during math operations, hopefully that won't be a problem.
You'll have to get a double from the BigDecimal so you will probably have to use Double.parseDouble and get the String from the BigDecimal.