Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sombody please help this Noob!

 
Josh Pont
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bridget Kennedy
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody my stupid cookie calculator works fabulous now!
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic