Question

I am creating a simple tax bracket application in Java for a homework project.

It takes the gross income, subtracts how many exemptions the user has and as well subtracts standard deductions for a total that equals the taxable income, which puts it in the tax bracket and does the calculation to tell the user their tax due.

I have it the taxable and taxrate set up in IF statements that should do the proper calculation for the tax due, but its almost completely doubling the value.

The IF statements are:

if (taxable >= 1 && taxable <= 9075){
        taxrate = (int) (taxable * .10);
    } else if (taxable >= 9076 && taxable <= 36900) {
        taxrate = (int) (taxable * .15)  + 908;
    } else if (taxable >= 36901 && taxable <= 89350) {
        taxrate = (int) (taxable * .25)  + 5082;
    } else if (taxable >= 89351 && taxable <= 186350){
        taxrate = (int) (taxable * .28) + 18195;
    } else if (taxable >= 186351 && taxable <= 405100){
        taxrate = (int) (taxable * .33) + 45354;
    } else if (taxable >= 405101 && taxable <= 406750) {
        taxrate = (int) (taxable * .35) + 117541;
    } else if (taxable >= 406751) {
        taxrate = (int) (taxable * .396) +118119;
    }

The random add of numbers at the ends are the previous tax brackets tax due, which adds it to the next bracket and so on. I know it's probably a very simple mistake, so do any of the Java experts have a solution?

Also, the code for taxable is:

static int taxable = gross - exempt - standard;

gross could equal any number, same with exempt and standard

Was it helpful?

Solution

I think the general formula to calculate the taxes for a given income range (rangemin, rangemax) should be:

taxrate = (taxable - rangemin) * rangePerc + rangemin * previousRangePerc

So that, in your code:

if (taxable >= 1 && taxable <= 9075){
        taxrate = (int) (taxable * .10);
} else if (taxable >= 9076 && taxable <= 36900) {
        taxrate = (int) ((taxable - 9076) * .15)  + 908;
} else if (taxable >= 36901 && taxable <= 89350) {
    taxrate = (int) ((taxable - 36901) * .25)  + 5082;
} ... 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top