-2

First question:

I wrote a program to calculate gift tax. When tested with input value: 14491 the output was -25436.3. I can't figure out how this number was calculated. Can anyone help explain this? If I'm not mistaken the code should have calculated parentheses then multiplied and then added. I'm also a noob, so I'm probably wrong but would love feedback.

Second question:

Is there a better way to have written this code?

import java.util.Scanner;
 
public class GiftTax {
 
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
 
        System.out.println("Value of the gift?");
        double amt = scan.nextDouble();
 
        if (amt < 5000) {
            System.out.println("No tax!");
        } else if (amt == 5000 && amt <= 25000) {
            System.out.println("Tax: " + (100 + (amt - 5000) * .08));
        } else if (amt == 25000 && amt <= 55000) {
            System.out.println("Tax: " + (1700 + (amt - 25000) * .10));
        } else if (amt == 55000 && amt <= 200000) {
            System.out.println("Tax: " + (4700 + (amt - 55000) * .12));
        } else if (amt == 200000 && amt <= 1000000) {
            System.out.println("Tax: " + (22100 + (amt - 200000)* .15));
        } else {
            System.out.println("Tax: " + (142100 + (amt - 1000000) * .17));
        }
    }
}

I changed "==" in the code to ">=" and it worked perfectly. I just don't understand how it came up with that initial negative number and it's driving me mad...

Thanks everyone!

luk2302
  • 55,258
  • 23
  • 97
  • 137
Glitch
  • 3
  • 3
  • 1
    Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Dec 13 '22 at 18:19
  • In particular for this code... Separate your individual operations into their own lines of code and observe the results of each operation. Does any one operation produce a result you don't expect? – David Dec 13 '22 at 18:19
  • Can 14491 be at the same time exactly equal to 5000 and less than 25000? Is that what you really want to test? – zapl Dec 13 '22 at 18:20
  • 1
    Since you have "amt <=" in your If statements, you don't need the other check on "amt", which should be a >, not == anyway. – computercarguy Dec 13 '22 at 18:24
  • 1
    `amt == 5000 && amt <= 25000` should have been `amt >= 5000 && amt <= 25000` but is better written as `amt <= 25000`. Etcetera. – Joop Eggen Dec 13 '22 at 18:32

1 Answers1

0

I just don't understand how it came up with that initial negative number and it's driving me mad...

Since 14491 is not less than 5000, it runs through all the else/if checks but 14491 is not EXACTLY equal to 5000, 25000, 55000, or 200000, so only the LAST ELSE block is what gets executed:

} else {
    System.out.println("Tax: " + (142100 + (amt - 1000000) * .17));
}

The calculation is:

(142100 + (amt - 1000000) * .17)
(142100 + (14491- 1000000) * .17)
(142100 + (-985,509) * .17)
(142100 + (-167,536.53))
-25,436.53
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40