Basic Java Income Tax -


the question:

write program prompts user enter his/her taxable income , calculates income tax due using 2014 tax table below. express tax 2 decimal places.

income tax brackets single-filers $9075          10% $9076 - $36900    15% $36901 - $89350       25% $89351 - $186350      28% $186351 - $405100   33% 

this have far. i'm new java keep in mind. specific question come after code.

import java.util.scanner; public class incometax {       public static void main(string[] args) {          scanner input = new scanner(system.in);          // prompt user enter taxable income         system.out.print("enter amount of taxable income year 2014: ");         double income = input.nextdouble();          // compute tax         double tax = 0;          if (income <= 9075)             tax = income * 0.10;         else if (income <= 9076)             tax = 9075 * 0.10 + (income - 36900) * 0.15;         else if (income <= 36901)             tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (income - 89350) * 0.25;         else if (income <= 89351)             tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (income - 186350) + 0.28;         else if (income <= 186351)             tax = 9075 * 0.10 + (9076 - 36900) * 0.15 + (36901 - 89350) * 0.25 + (89351 - 186350) + 0.28 + (income - 405100) + 0.33;          if (income <=  9075)             system.out.println("you have entered 10% bracket.");         else if (income <= 9076)             system.out.println("you have entered 15% bracket.");         else if (income <= 36901)             system.out.println("you have entered 25% bracket.");         else if (income <= 89351)             system.out.println("you have entered 28% bracket.");         else if (income <= 186351)             system.out.println("you have entered 33% bracket.");     } } 

the final output should this:

enter amount of taxable income year 2014. (user input here ->) 5000

you have entered 10% tax bracket.

your income $5,000.00, tax is: $500.00. income after tax is: $4,500.00


how output above output? decimals , $ sign.

the output code working on is:

system.out.println("your income is: " + "your taxx is: " + (int)(tax * 100) / 100.0) + "your income after tax is: " + ; 

simply append following part code:

decimalformat formatter = new decimalformat("###,###,###.00"); system.out.println("your inccome $"+formatter.format(income)+", tax is: $"+formatter.format(tax)+". income after tax is: $"+formatter.format(income-tax)); 

here decimalformatter technique format numbers (for instance commas separating thousands, , 2 digits after decimal dot).

but code quite sloppy , error-prone , doesn't seem make sense. can better subdivide calculation of taxes using different brackets:

double[] max = {0,9075,36900,89350,186350,405100}; double[] rate = {0,0.10,0.15,0.25,0.28,0.33}; double left = income; double tax = 0.0d; for(int = 1; < max.length && left > 0; i++) {     double df = math.min(max[i]-max[i-1],left);     tax += rate[i]*df;     left -= df; } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -