I have created this simple program for my programming logic class, my methods all seem to do the right math but when I get to my output method the values are switched around. That is to say I have changed my GrandTotal to ShippingTotal as well as other because shipping toltal displays the correct answer for grand total. Help me please if you can. I'll paste my code below.
[fbr - added code tags]
[ inserted line breaks in one really long line - Jim ] [ December 10, 2007: Message edited by: Jim Yingst ]
I am still confused as to how I can fix my issue? My professor is teaching this class for the first time and is unfamiliar with Java, so its the blind leading the blind. I should change my Output() method from void to what double because all its called args are doubles? And yes, I am that green P.S. Thanks all for the help so far and to come.
I was faked out by the scrambled variable names in getGrandTotal, when, in fact, the order is irrelevant.
The reason the order doesn't matter in this method:
is that all the arguments are of the same type and the operation performed in the method (adding them all together) just happens to be order independent.
Of course, even though it will work regardless of order, you should always follow your specified ordering when calling this method for the purposes of making your code more readable. Doing so will also prevent future errors - say in the case where you modify the method and introduce operations for which the ordering IS important. [ December 11, 2007: Message edited by: Bridget Kennedy ]
So now that it is solved, I think there is something valuable to take away here ---
When designing a method, it's parameter list is just as important as the method name. There's no "rule" - but a good metric is three or less parameters, with only two of the same type - and try not to make the same-type-paramters sequential.
You could also think about splitting the method into two methods if they could operate on two parameters each (forgive me, I really haven't looked MUCH at your code).
It's not like your designing methods for a million programmers to use, but who's to say you won't some day? Whenever you design an "api" - those are some initial things to consider.
Hope that helps!
Do you want ants? Because that's how you get ants. And a tiny ads:
Free, earth friendly heat - from the CodeRanch trailboss