This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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 ]
Ah yes. I was faked out by the scrambled variable names in getGrandTotal, when, in fact, the order is irrelevant.
This Josh is a tricky fellow.
Joined: Dec 10, 2007
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.
author and iconoclast
Oh, my god I am so dense. Told ya i was green. That totaly makes sense now. Now I just need to see if I can fix it.
Joined: Nov 30, 2004
Re. my comment:
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 ]
Joined: Dec 10, 2007
Thanks everybody my stupid cookie calculator works fabulous now!
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!
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com