-
December 7th, 2009, 11:09 PM
#1
Please check my code
Hi everyone,
Could someone please check my answer forthe question below and tell me if it is correct.
Implement a method showTaxAmount to show the tax paid for payment amount from 1000 to 55000 with incremental of 100.
Answer
Code:
public void showTaxAmount(int payment)
{
if (payment >= 1000 && payment <= 55000 && (payment%100 == 0) )
{
System.out.println("Tax paid for " + payment + " is "+ payment*TAX_RATE);
}
else
{
System.out.println("Payment should be from 1000 to 55000 with incremental of 100");
}
}
Thank you all very much.
-
December 8th, 2009, 08:17 AM
#2
Re: Please check my code
First question to ask is 'Does it work?'.
Have you tried stepping through the code by hand, keeping track of the variables and their values on paper? Do you get the correct results?
Have you tried to run the code? If it compiles, then there are no syntax errors. If it runs correctly there are no logic errors. Have you tried running this code? Does it give the correct answers?
I can't tell if it is right, because I don't know what TAX_RATE is. If TAX_RATE is declared correctly, and has the correct value, then the code will work as expected.
Judge a man by his questions, rather than his answers...
Voltaire
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 8th, 2009, 10:36 AM
#3
Re: Please check my code
Thank you dlorde,
The code compiles and runs as expected.
But a forummer suggested that I need a loop for this question because it states "increments of 100".
So this question is solved.
Thank you for your input, highly appreciated.
-
December 8th, 2009, 07:37 PM
#4
Re: Please check my code
Originally Posted by kumaran21
But a forummer suggested that I need a loop for this question because it states "increments of 100".
I can understand that - it isn't clear in the question what 'incremental of 100' means - I guessed it from the code you wrote...
However, in programming, regular increments usually mean iteration with addition, so the question could be interpreted as asking for code that displays the tax paid for all the values from 1000 to 55000 in increments of 100, in which case you would have to wrap the current method code in a loop. It is a poorly worded and ambiguous question.
Good teaching is more a giving of the right questions than a giving of the right answers...
J. Albers
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|