1

my assignment is simple really. someone enter a number for their income, if its under 9000, its taxed at 10%, over that, its 15% etc.

but then one of the requirements says "you will organize the income ranges as an array in each method and select an array record to compute specific tax in a tax bracket"

how would that work? doesn't array only take specific values and not ranges of values??

this is my code so far

public static int calcTax(int status, int income)
{
      if (status == 0)
      {
         return singleFiling(income);
      }
      if (status == 1)
      {   
         return jointFiling(income);
      }
      if (status == 2)
      {   
         return headFiling(income);
      }
      else
          System.out.println("Error: invalid status. Enter a number between 0 to 2 only.");
    return income;

      }

static int ninek = 9075;
static int thrirtysixk = 36900;
static int eightyninek = 89350;
static int oneeightysixk = 186350;
static int fourofivek = 405100;
static int fourosixk = 406750;
static int eighteenk = 18150;
static int seventythreek = 73800;
static int onefortyeightk = 148850;
static int twotwentysixk = 226850;
static int fourfivesevenk = 457600;
static int twelveninefifty = 12950;
static int fourninek = 49400;
static int onetwentysevenk = 127550;
static int twoosixk = 206600;
static int fpurthreetwok = 432200;

 static double tax = 0;
 static int singleFiling(double userIncome)
{
    if (userIncome >= 1 && userIncome <= ninek )
        tax = userIncome * 0.10;    

System.out.println("Your payable Federal tax is: " + tax);

return 0;
}
 static int jointFiling(double userIncome)
    {
        if (userIncome >= 1 && userIncome <= oneeightysixk )
            tax = userIncome * 0.10;    

    System.out.println("Your payable Federal tax is: " + tax);

    return 0;
    }
  static int headFiling(double userIncome)
  {
      if (userIncome >= 1 && userIncome <= twelveninefifty )
            tax = userIncome * 0.10;    

    System.out.println("Your payable Federal tax is: " + tax);

    return 0;
  }
}
3
  • 2
    consider having two arrays one for income and one for tax rate, the index of the relevant income array matches the index for the tax rate e.g. income[0] == 9000 taxrate[0] == 12.5 Commented Feb 23, 2017 at 6:17
  • you can also take an array and calculate the tax regarding array index and store the tax into that array index Commented Feb 23, 2017 at 6:28
  • so the code would be something like if (userIncome >=1 && userIncome <= income[0]) tax = userIncome * taxrate[0]; would that work? Commented Feb 23, 2017 at 6:48

1 Answer 1

1

Taking the comments into account, your code could look like this:

static int[] incomeLevel = new int[]{9075, 186350, 12950};
static double[] taxRate = new double[]{.1, .15, .2}

static double tax = 0.0;

public static int calcTax(int status, int income) {
    if (status < 0 || status > 2) {
        System.out.println("Error: invalid status. Enter a number between 0 to 2 only.")
        return income;
    }
    if (income > 0 && income <= incomeLevel[status])
        tax = income * taxRate[status];
    System.out.println("Your payable Federal tax is: " + tax)
    return 0;
}

Of course the other values for the income levels have to been added, same for the tax rates.

Instead of computing every value in another method, choosing by status, you can use the status as the array index. That has the advantage that only need the code once instead of three times but with some other values.

Now you only need to check if your status is in your wanted range.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.