• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Having trouble passing method properties

 
Greenhorn
Posts: 10
Android IntelliJ IDE Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

I'm not 100% sure what is wrong with my code. I have to have the output below:



but am getting:

Commission on 50.0 at a rate of 5 is 50.0
Commission on 10000.0 at a rate of 5 is 10000.0
Commission on 75.0 at a rate of 7.5% is 75.0

Here is my code:



Could someone please help point me in the right direction? Thank you.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't do this:

a line of code should do one thing, and one thing only. if you want to return a value, return it. If you want to compute a value, compute it. If you want to assign a value, assign it.

Doing all three at once is confusing, at best.

note that you don't have to save the value you are returning in your method. You can just do this:

(I think i have that syntax right)

Let me also suggest you concentrate on fixing one of thest at a time. I'd even comment out the other two. Use a lot of System.out.println() statements to see what your code is REALLY doing at each step.
 
Kevin Anderson
Greenhorn
Posts: 10
Android IntelliJ IDE Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I figured out all but one of my issues. Now the below calculation keeps returning a zero:




AMOUNT is = 1000

I have to have cPercent as a int and sAmount as a double. Could the problem be with the conversion to a double?

This was so much easier in C#!

Thank you
 
Sheriff
Posts: 67735
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cPercent is an int. so with integer math, what happens when you divide the value of cPercent by 100?
 
Kevin Anderson
Greenhorn
Posts: 10
Android IntelliJ IDE Netbeans IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh ok. Thank you.
 
Sheriff
Posts: 17627
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BTW, the little warts like "s" and "c" in front of "Amount" and "Percent" are generally discouraged. Hungarian notation has no place in Java code. Strive to make your code convey the intent instead. I like to say "write your code so that it reads the way it's supposed to work."

I don't quite get your calculations. On the method computeCommission(double, int), you'd think that the return value would give back the commission calculated from the two parameters passed in. However, you don't use the percent value at all in the calculation. The formula you have programmed there looks totally wrong. The second version of computeCommission doesn't look right either. Ok, saw that you modified your formula.

Still, you should not need more than one method to compute the commission. What you have here are various ways data for the calculations can be given to you. One way is to have a percentage value, which you need to convert to a decimal value. Another way is to have the decimal value given directly. And the amount on which you base the commission calculation, while it's 1000 in the examples you have been given, should not be hard-coded as 1000. Your program should work with any value on which a commission can be paid. So rather than having multiple methods to calculate the commission, you should have multiple methods that take input and convert them to a "standardized" value that the commission calculation method expects.

As often advised in these forums, start describing the logic using paper and pencil first. Then figure out how to do it in Java.

 
The moustache of a titan! The ad of a flea:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic