aspose file tools*
The moose likes Beginning Java and the fly likes Sombody please help this Noob! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Sombody please help this Noob!" Watch "Sombody please help this Noob!" New topic
Author

Sombody please help this Noob!

Josh Pont
Greenhorn

Joined: Dec 10, 2007
Posts: 4
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 ]
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
Hello, Josh.

Couple of things - first of all the tags are useful for maintaining indentation and making your code more readable.

Second, I see a problem with the use of your getGrandTotal method. Take a look at the order of method arguments.
[ December 10, 2007: Message edited by: Bridget Kennedy ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Actually it's where you call Output() that you should look at. Here's the call:

Output(GrandTotal,TotalCookieCost,SalesTax,ShippingTotal);

and here's the method declaration:

void Output(double TotalCookieCost,double SalesTax,double ShippingTotal,double GrandTotal)

You're passing the arguments in the wrong order: in Output, the value of GrandTotal is the value copied from the ShippingTotal variable in main(). The other arguments are similarly mixed up.


[Jess in Action][AskingGoodQuestions]
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
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.
Josh Pont
Greenhorn

Joined: Dec 10, 2007
Posts: 4
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Your output method takes four parameters. When you call it, you supply four arguments. The arguments you supply when you call it have to be in the same order as the parameters. An example:

double divide(double a, double b) { return a/b; }

divide(1, 2) is 0.5 (one half, 1/2), but divide(2, 1) is 2.0 (i.e., 2/1).

When you're calling your Output method, you have to supply the arguments in the same order that they are listed in the declaration of Output. So instead of what you have, you want:

Output(TotalCookieCost, SalesTax, ShippingTotal, GrandTotal);

so that it matches the declaration. Understand?
Josh Pont
Greenhorn

Joined: Dec 10, 2007
Posts: 4
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.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 84
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 ]
Josh Pont
Greenhorn

Joined: Dec 10, 2007
Posts: 4
Thanks everybody my stupid cookie calculator works fabulous now!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42946
    
  70
Originally posted by Josh Pont:
Thanks everybody my stupid cookie calculator works fabulous now!

It's no longer stupid after input from all these knowledgeable people has been incorporated
Adam Schaible
Ranch Hand

Joined: Oct 04, 2007
Posts: 101
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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Sombody please help this Noob!