• Status: Solved
• Priority: Medium
• Security: Public
• Views: 336

# code debug...

This program produces:

run:

Tax for years:        2001            2009
---------------------------------------
0 - single
50000            -77920.5      4675.0
51000            -77570.5      4675.0
.
.
60000            -74420.5      4675.0

1 - married jointly
50000            -76105.5      1670.0
51000            -75755.5      1670.0
.
.

From 50000 to 60000. The amount for 2009 is correct but for 2001 give negative values that is not correct.

The problem is with:

static int [][] brackets2001 = {
{27050, 65550, 136750, 297350, 0},
{45200, 109250, 166500, 297350, 0},
{22600, 54625, 83250, 148675, 0},
{36250, 93650, 151650, 297350, 0},
};

This error exists because the last figure was missing compare to 2009. And in order to maintain the same array structure, the missing values are replaced by a zero. I have done this thinking the getTax() method will ignore the negatives as shown below:

v-- if inside () is positive do the calculation otherwise enter 0 to ignore it
((taxableIncome>bracket[status][4])?
(taxableIncome-bracket[status][4])*rates[5]:0);

But, somehow it fails.

Question: Can you see what causes the problem (problem: getting negative tax amounts for 2001)?

Maybe I need to simplify this method a bit.

Thank you.

``````public class TestTaxClass {

static int [][] brackets2001 = {
{27050, 65550, 136750, 297350, 0},
{45200, 109250, 166500, 297350, 0},
{22600, 54625, 83250, 148675, 0},
{36250, 93650, 151650, 297350, 0},
};

static int [][] brackets2009 = {
{8350, 33950, 82250, 171550, 372950},
{16700, 67900, 137050, 208850, 372950},
{8350, 33950, 82250, 104425, 186475},
{11950, 45500, 117450, 190200, 372950},
};

static double [] rates={0.10, 0.15, 0.25, 0.28, 0.33, 0.35};

static String[]status={"single", "married jointly", "married separately", "head of household"};

public static void main(String[] args){

Tax tax2001=new Tax();
Tax tax2009=new Tax();

System.out.println("\nTax for years:\t  2001\t\t2009");
System.out.print("---------------------------------------");

for(int j=0;j<4;j++){
Tax.setFilingStatus(j);
tax2001.setBracket(brackets2001);
tax2009.setBracket(brackets2009);
Tax.setRates(rates);

System.out.println("\n"+ j + " - " + status[j]);

for(int i=50000; i<=60000;i+=1000){
Tax.setTaxableIncome(i);

// display the result
System.out.println(i + "\t\t"+
(int)(tax2001.getTax()*100)/100.0+ "\t"+
(int)(tax2009.getTax()*100)/100.0);
}
}
}
}

class Tax {
private static int status;
private int[][] bracket;
private static double[] rates;
private static double taxableIncome;

public Tax(){}

public Tax(int status, int[][] bracket, double []rate){
this.status=status;
this.bracket=bracket;
this.rates=rate;
}

public int getFilingStatus(){
return status;
}

public static void setFilingStatus(int status){
Tax.status= status;
}

public int[][] getBracket(){
return bracket;
}

public void setBracket(int[][] bracket){
this.bracket= bracket;
}

public double[] getRates(){
return rates;
}

public static void setRates(double[] rates){
Tax.rates= rates;
}

public double getTaxableIncome(){
return taxableIncome;
}

public static void setTaxableIncome(double taxableIncome){
Tax.taxableIncome= taxableIncome;
}

public double getTax(){
return bracket[status][0]*rates[0]+
((taxableIncome>bracket[status][1])?
(bracket[status][1]-bracket[status][0])*rates[1]:0)+

((taxableIncome>bracket[status][2])?
(bracket[status][2]-bracket[status][1])*rates[2]:0)+

((taxableIncome>bracket[status][3])?
(bracket[status][3]-bracket[status][2])*rates[3]:0)+

((taxableIncome>bracket[status][4])?
(bracket[status][4]-bracket[status][3])*rates[4]:0)+

((taxableIncome>bracket[status][4])?
(taxableIncome-bracket[status][4])*rates[5]:0);
}
}
``````
0
Mike Eghtebas
1 Solution

IT Business Systems Analyst / Software DeveloperCommented:
A couple of things...

> From 50000 to 60000. The amount for 2009 is correct but

The 2009 values don't look very correct to me. Why would the tax stay the same for increasing taxable income?

> if inside () is positive do the calculation otherwise enter 0 to ignore it

Yes, however, for the case where the bracket value is 0, the bit inside ( ) is positive (since 50000 is greater than 0) and so the calculation does proceed. However, it is not the line that you picked out in the question that is causing your problem, it is the one above it (ie. lines 106-107) that is producing the negative values. Think about it, income, 50000, is greater than bracket[4], 0, and so the calc is bracket[4], 0, minus bracket[3], 297350, which equals -297350 and then times that by the rate[4], 0.33..... That is your negative!!

So... how do we fix both of these problems? First thing, you don't NEED to keep the same structure in the arrays. The beauty of Java is that it easy handles arrays of arrays with different dimensions, and that you can find out how many items are in each dimension of an array. So, start by defining the arrays like so...

``````static int [][] brackets2001 = {
{27050, 65550, 136750, 297350},
{45200, 109250, 166500, 297350},
{22600, 54625, 83250, 148675},
{36250, 93650, 151650, 297350},
};

static int [][] brackets2009 = {
{8350, 33950, 82250, 171550, 372950},
{16700, 67900, 137050, 208850, 372950},
{8350, 33950, 82250, 104425, 186475},
{11950, 45500, 117450, 190200, 372950},
};
``````

And now, your getTax method could look something like this... (I will leave it up to you to finish off the commented lines)

``````public double getTax(){
int[] statusBrackets = bracket[status];         // Makes it a bit clearer as to what we are doing

double taxAmount = 0.0;
double incomeRemaining = taxableIncome;
int previousBracketValue = 0;
int bracketIndex;
for (bracketIndex = 0; bracketIndex < statusBrackets.length; bracketIndex++) {              // Loop through each bracket that we have been supplied with

if(incomeRemaining <= 0)
break;

int bracketValue = statusBrackets[bracketIndex];
double incomeInBracket;

if (incomeRemaining > bracketValue) {
incomeInBracket = bracketValue - previousBracketValue;
}
else {
incomeInBracket = incomeRemaining;
}

//taxAmount += incomeInBracket .............;
//incomeRemaining -= .............;
//previousBracketValue = .............;
}

if(incomeRemaining > 0)
taxAmount += incomeRemaining * rates[bracketIndex];

return taxAmount;
}
``````

Now that example is quite terse, and there are opportunities to refine it a little, but it should explain what is going on.
0

Database and Application DeveloperAuthor Commented:
Thank you
0

## Featured Post

Tackle projects and never again get stuck behind a technical roadblock.