Results 1 to 17 of 17
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default [SOLVED] Noobie - Calculating Percentages

    Dear All,

    I am trying to write a method that interprets an argument (int tax) as a percentage, and then debits an account balance by that percentage of the balance and then return the amount debited. I have no experience of calculating percentages :-(

    The code so far is:

    public double tax(int tax)
    {
    double percentage;
    percentage = (tax)/100;
    {
    this.setBalance(this.getBalance() ..... at this point I am lost.

    Do I this.getBalance * percentage? and how do I show the amount debited as the answer.

    Any help would be gratefully appeciated.

    All the best - FMJ.

  2. #2
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    you need to return the percentage before you can use it. After that you can do this.setBalance(this.getBalance() * (tax + 1))
    Liberty has never come from the government.
    Liberty has always come from the subjects of government.
    The history of liberty is the history of resistance.
    The history of liberty is a history of the limitation of governmental power, not the increase of it.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    --REMOVED--
    reason: misunderstood the op.
    Last edited by angryboy; 05-22-2009 at 04:17 PM.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    First off when you do
    Java Code:
    percentage = tax / 100;
    The number is going to be truncated before it's assigned to percentage. Make sure tax is casted to a double or the constant is declared as a floating point primitive.

    Then just use simple math.

    Since you are debiting the account you can simply subtract the balance by the tax percentage to get the new balance.
    Java Code:
    balance -= balance*percentage

    Mr. Beans
    Last edited by Mr.Beans; 05-21-2009 at 11:25 PM. Reason: Wrong operand

  5. #5
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Did you try this?
    Java Code:
    this.getBalance() * percentage
    Why is there an extra open bracket ({)?

    Returning a Value from a Method (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  6. #6
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Oh, and don't use floating-point arithmetic for financial applications. Use a fixed-point system for exact and correct results.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  7. #7
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey All,

    So far I have:


    public double tax(int tax)
    {
    double percentage;
    percentage = (tax)/100;
    this.setBalance(this.getBalance() * percentage);
    }

    This does not work? I am unable to change int tax as this method uses an interface. Should it then be:

    public double tax(int tax)
    {
    float percentage;
    percentage = (tax)/100;
    this.setBalance(this.getBalance() * percentage);
    }

    I have tried this and it still does not work. Also, I'm unsure on how to show the balance that has been deducted. The only methods I have are setBalance and getBalance.

    Cheers - FMJ.

    PS - I am very very bad at Java, so it this seems really obvious please.. I appologise.

  8. #8
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Further to this...?

    If I simply change the method to the following:

    public double tax(int tax)
    {
    double percentage;
    percentage = (tax)*100;
    return percentage;
    }

    then run account.tax(2);

    I get 200 - This is correct 2 x 100.

    But if I change back to:

    public double tax(int tax)
    {
    double percentage;
    percentage = (tax)/100;
    return percentage;
    }

    account.tax(2); I get 0.0? Not 0.02?

    What can I be doing wrong?

    Cheers - FMJ.

  9. #9
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Cast the tax to a double, and divide by a double, to give a double.
    Java Code:
    (double)tax / 100.0;
    Using integers performs integer division, which ignores all fractional parts.

    Again, floating point arithmetic will give incorrect results when dealing with very small numbers and should not be used for financial applications.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  10. #10
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey OrangeDog,

    Your probably gonna scream! I'm sorry I'm not quite getting this but:

    public double tax(int tax); method header.

    If I change to:

    public double tax(double tax);

    I get the error "account is not abstract and does not override abstract method tax(int) in taxable.

    This class implements an interface so I cannot change it.

    How can I convert int tax into a double within the method body?

    I have tried:

    public double tax(int tax);
    {
    double percentage;
    double tax1;
    tax1= tax;
    percentage = tax1/100;
    }

    Does not work.

    I also tried your code:

    (double) tax / 100.0;

    I get the error message: not a statement.

    Please please ... I know that I am stupid with Java, but I don't think I am that far away.

    Cheers - FMJ. ..... (aka the penny has "almost" dropped)...

  11. #11
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    AAAAAAAAAAAAAAAAAAAAAAAAAA - there you go :p

    That is indeed not a statement, try adding "percentage = " to the front of it. The bracketed type operation is called a cast.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  12. #12
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    OrangeDog! Most excellent scream! Hehe

    Thanks for the advice! It works! Your a star!

    Thanks everyone for their help! I'm going for a beer :-)

    Cheers - FMJ.

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    hey fmj, i think its about time you learn to use code tags. [noparse];)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  14. #14
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    PS - How do I mark the threads as solved?

  15. #15
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Hey angryboy,

    Heheh! Agree totally! I will use the tags from now on! :-)

    Cheers - FMJ.

  16. #16
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Mark threads as solve by using thread tools menu at top of page.
    Code tags: [code] [/code] -- USE THEM
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  17. #17
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default

    Java Code:
    message understood :-)
    
    Thanks again all! :-)

Similar Threads

  1. [SOLVED] Noobie Help! Constructors
    By fullmetaljacket in forum New To Java
    Replies: 6
    Last Post: 05-14-2009, 03:52 AM
  2. Percentages
    By Ciwan in forum New To Java
    Replies: 3
    Last Post: 02-16-2009, 06:34 AM
  3. Qadratic Formula and Percentages
    By bbtgirl in forum JCreator
    Replies: 4
    Last Post: 02-07-2009, 04:07 AM
  4. Need help with calculating the time the user...
    By Smirre in forum New To Java
    Replies: 8
    Last Post: 11-20-2008, 02:15 PM
  5. Calculating sin of a double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 09:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •