• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling methods  RSS feed

 
Ken Terp
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I feel like this might be a stupid question as I am very new to JAVA and I am having quite a bit of difficulty writing a progam, but I am going to ask it anyway. Can you call a different method based upon what a user inputs from within a method using if ,else if statements... here's an example if it helps. I guess one my questions is why can't I just call the method without parameters for example, computeNoDiscount() Any help would be very much appreciated. Thanks.

public static double calculatePrice(double retailPrice, double numOrdered, char discountType) {
if (discountType == 'n')
computeNoDiscount(retailPrice, numOrdered);
else if (discountType == 'c')
computeCouponDiscount(retailPrice, numOrdered);
else if (discountType == 'f')
computeFreeShipDiscount(retailPrice, numOrdered);
else if (discountType == 's')
computeSpecialDiscount(retailPrice, numOrdered);

double fpr;
fpr = retailPrice * numOrdered;
return fpr;
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Terp:
Hello
I feel like this might be a stupid question as I am very new to JAVA and I am having quite a bit of difficulty writing a progam, but I am going to ask it anyway. Can you call a different method based upon what a user inputs from within a method using if ,else if statements... here's an example if it helps. I guess one my questions is why can't I just call the method without parameters for example, computeNoDiscount() Any help would be very much appreciated. Thanks.

public static double calculatePrice(double retailPrice, double numOrdered, char discountType) {
if (discountType == 'n')
computeNoDiscount(retailPrice, numOrdered);
else if (discountType == 'c')
computeCouponDiscount(retailPrice, numOrdered);
else if (discountType == 'f')
computeFreeShipDiscount(retailPrice, numOrdered);
else if (discountType == 's')
computeSpecialDiscount(retailPrice, numOrdered);

double fpr;
fpr = retailPrice * numOrdered;
return fpr;


One question I have is what do the methods computeNoDiscount, etc. do? You aren't using anything returned from the methods.

If the purpose of the methods is to determine how much of a discount you get based on the discount type, you could have a single method that accepts the discount type and returns the discount.
[ October 06, 2006: Message edited by: Keith Lynn ]
 
Ken Terp
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depending on the type of discount the calculatePrice method has to call the correct method to compute the price for the discount.

computeNoDiscount
computeFreeShipDiscount
computeCouponDiscount
computeSpecialDiscount
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you call a different method based upon what a user inputs from within a method using if ,else if statements.

Yes.

Originally posted by Ken Terp:
Depending on the type of discount the calculatePrice method has to call the correct method to compute the price for the discount.

computeNoDiscount
computeFreeShipDiscount
computeCouponDiscount
computeSpecialDiscount

The point Keith was trying to make is that your method names indicate that your methods make some type of calculation. Usually, when a method computes a calculation, it returns the results. If your methods are defined to return a result, then you need to "catch" the result in a variable, e.g.


[ October 07, 2006: Message edited by: sven studde ]
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cross posted on another forum.

Ken Terp,

Thanks for wasting our time. Bye.
 
Ken Terp
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry you feel that way as wasting your time was not my intention.
As I stated in my original post. I am new to Java and I am sorry you feel I am wasting your time. I am also new to posting and don't like asking for help unless I really need it. I don't appreciate your attitude. I have spent over 40 hours trying to figure this out and I hit a wall. I do not want the answer... I want help. You have obviously forgotten what it is like to need help with something. Thanks for being so understanding.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You definitely need to return something from these methods. Like the following changes I made to your code. I added the double "discount". Assuming the methods return the discount back as a decimal (example: 0.15). Then you want to to get that discount back from each method and apply it to the price.


I'm assuming you were thinking that the discount computed in each method was visible to the rest of the code and that's not normally true. You should read up on the scope of variables.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sven studde:
Cross posted on another forum...

sven, I understand your frustration, but let's try to be a bit more understanding and patient with greenhorns. A friendly reminder should do just fine. I didn't find anything on another forum, although this thread does appear to be a continuation of a previous topic, which I've closed.

Ken, if you still have questions on this particular topic, please continue the discussion here. If you have questions on another part of the project, please be specific in this thread. Thanks!
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sven, I understand your frustration, but let's try to be a bit more understanding and patient with greenhorns. A friendly reminder should do just fine. I didn't find anything on another forum, although this thread does appear to be a continuation of a previous topic, which I've closed.

It's cross posted on a forum on another website, and there are three threads going here.
[ October 08, 2006: Message edited by: sven studde ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sven studde:

It's cross posted on a forum on another website, and there are three threads going here.


That's absolutely OK with us. Please don't complain about it. Posting to multiple forums within JavaRanch -- that we don't like. But what self-respecting Rancher ever visits any other Java discussion site? I know I don't.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!