0

I wrote a program which calculates the tax you have to pay but there is something I didn't understand. It is possibly a simple thing that I don't know.

Here is my entire code:

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Type your income and press enter...");
    int income = in.nextInt();
    int tax, extra;

    if (income <= 10000) {
        tax = income * (15/100);
        System.out.println("The tax you have to pay is " + tax);
    } else if (income <= 25000) {
        income -= 10000;
        tax = income * (20/100) + 1500;
        System.out.println("The tax you have to pay is " + tax);
    } else if (income <= 58000) {
        income -= 25000;
        tax = income * (27/100) + 4500;
        System.out.println("The tax you have to pay is " + tax);
    } else {
        income -= 58000;
        tax = income * (35/100) + 13410;
        System.out.println("The tax you have to pay is " + tax);
    }
}

This way program doesn't calculate properly. For instance, if I write 9000, the output is 0. If I put, 3000, the output is 1500. Shortly, it omits the

income * (20/100)

part. Therefore, I just deleted parenthesis around them and did this:

tax = income * 20/100 + 1500;

and surprisingly it worked.

My question is simply why? I know parenthesis are not obligatory in this situation, however, I thought it could be easier to read when written in parenthesis. Why cannot I use them?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Haggra
  • 3,539
  • 2
  • 20
  • 28

4 Answers4

2

The problem is that integer division doesn't take into account fractional numbers. So 20 / 100 == 0.

In your specific example, since * and / have same precedence and are left-associative then

 income * 20 / 100

is evaluated as

(income * 20) / 100

That's why the result could seem correct (but it doesn't, since for an income <= 5 you will get 0 too.

Just turn one of the values into a float and then cast the result back, eg:

(int)(income * (15.0f/100))
Jack
  • 131,802
  • 30
  • 241
  • 343
1

You are performing integer division because the result of dividing two int(s) is an int. Make one term a double or float (also you only need to print once). Something like

double tax;
if (income <= 10000) {
    tax = income * (15 / 100.0);
} else if (income <= 25000) {
    income -= 10000;
    tax = income * (20 / 100.0) + 1500;
} else if (income <= 58000) {
    income -= 25000;
    tax = income * (27 / 100.0) + 4500;
} else {
    income -= 58000;
    tax = income * (35 / 100.0) + 13410;
}
System.out.printf("The tax you have to pay is %.2f%n", tax);
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Thank you both for explaining and making code shorter. I did not understand 15/100.0 part though. I tried that and it did not actually work and netbeans put a (int) automatically. – Haggra Jan 19 '15 at 00:22
  • @Haggra Sorry, `tax` should have changed to a `double`. Also, I'd use `printf`. Edited. – Elliott Frisch Jan 19 '15 at 00:25
1

Use double for the variable income and tax. If you use 20/100 , it is an integer division.

To define the percentage, you can use 0.2 for 20% or (20/100.0)

peterho
  • 74
  • 1
  • 3
1

With the parentheses, you are forcing integer division, which results in the 0. Without, the multiplication will be done first, resulting in a double participating in the division, so the desired fraction results. If you'd specified that either of the constants wasn't an integer (by adding .0), the result would have been the same with or without parentheses.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101