Results 1 to 17 of 17
- 05-21-2009, 09:44 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
[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.
- 05-21-2009, 09:59 PM #2
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.
- 05-21-2009, 10:18 PM #3
--REMOVED--
reason: misunderstood the op.Last edited by angryboy; 05-22-2009 at 03:17 PM.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-21-2009, 10:19 PM #4
First off when you do
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.Java Code:percentage = tax / 100;
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. BeansLast edited by Mr.Beans; 05-21-2009 at 10:25 PM. Reason: Wrong operand
- 05-21-2009, 10:21 PM #5
Did you try this?
Why is there an extra open bracket ({)?Java Code:this.getBalance() * percentage
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
- 05-21-2009, 10:22 PM #6
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
- 05-21-2009, 10:33 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
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.
- 05-21-2009, 11:03 PM #8
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
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.
- 05-21-2009, 11:06 PM #9
Cast the tax to a double, and divide by a double, to give a double.
Using integers performs integer division, which ignores all fractional parts.Java Code:(double)tax / 100.0;
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
- 05-21-2009, 11:34 PM #10
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
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)...
- 05-21-2009, 11:48 PM #11
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
- 05-21-2009, 11:54 PM #12
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
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.
- 05-21-2009, 11:56 PM #13
hey fmj, i think its about time you learn to use code tags. [noparse];)
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 05-21-2009, 11:57 PM #14
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
PS - How do I mark the threads as solved?
- 05-21-2009, 11:58 PM #15
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
Hey angryboy,
Heheh! Agree totally! I will use the tags from now on! :-)
Cheers - FMJ.
- 05-22-2009, 01:01 AM #16
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Mark threads as solve by using thread tools menu at top of page.
Code tags: [code] [/code] -- USE THEMIf 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!
- 05-22-2009, 01:10 AM #17
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
Similar Threads
-
[SOLVED] Noobie Help! Constructors
By fullmetaljacket in forum New To JavaReplies: 6Last Post: 05-14-2009, 02:52 AM -
Percentages
By Ciwan in forum New To JavaReplies: 3Last Post: 02-16-2009, 05:34 AM -
Qadratic Formula and Percentages
By bbtgirl in forum JCreatorReplies: 4Last Post: 02-07-2009, 03:07 AM -
Need help with calculating the time the user...
By Smirre in forum New To JavaReplies: 8Last Post: 11-20-2008, 01:15 PM -
Calculating sin of a double value
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 08:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks