Java code to calculate income tax for company or for employee – The following income tax calculator on java has been written in 4 different ways. Suitable examples and sample programs have been included in order to make you understand simply.
The methods used in this article are as follows:
- Using Scanner Class
- Using a Static Method
- Using Separate Class
- Using Command-Line Arguments
Income varies from person to person depending on their ranges of yearly income.
As you can see, these are the most basic slabs in the application as far as the Indian Government is concerned. Those are as follows:
- Up to 3-LPA
- Rs. 300001 – Rs. 500000
- Rs. 500001 – Rs. 1000000
- Rs. 1000000 and above
Java Income Tax – Using Scanner Class
To find the income tax, we will first require to know the income as, based on the range in which the income falls, the amount to be paid and the calculations vary.
To get the input of income at runtime, we make use of the Scanner class of Java. With this, the user can give inputs in the console screen itself and doesn’t require to come to the code part.
After getting the value of income, we’ll have to figure out in which range it falls. Based on this condition, the formula to be used to determine the income tax varies as stated in the above table.
So, based on our input income, if checks which of the above conditions satisfy and following that formula we derive our resultant output income tax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.Scanner; class Income { public static void main(String args[]) { double tax=0,it; Scanner sc=new Scanner(System.in); System.out.println("Enter income "); it=sc.nextDouble(); if(it<=200000) tax=0; else if(it<=300000) tax=0.1*(it-200000); else if(it<=500000) tax=(0.2*(it-300000))+(0.1*100000); else if(it<=1000000) tax=(0.3*(it-500000))+(0.2*200000)+(0.1*100000); else tax=(0.4*(it-1000000))+(0.3*500000)+(0.2*200000)+(0.1*100000); System.out.println("Income tax amount is "+tax); } } |
Output:
1 2 3 |
Enter income 100000 Income tax amount is 0.0 |
Using Static Method
In the above Java method, the entire logic along with input output statements were written within the main method itself.
Let us consider that, in future we would like to make use of the same logic somewhere else in the code. Under that situation the same set of statements have to be rewritten again.
To avoid this, if the set of statements containing the main logic of the code (discussed in the above method) is placed in a separate block or what is called the static method (incomeTax) then, just making a function call of this method would be sufficient and by passing the arguments for which the calculation is to be made.
Thereby, making use of a static method helps us reuse a block of statements wherever required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class Income { public static void main(String args[]) { double t=0; Scanner sc=new Scanner(System.in); System.out.println("Enter income "); double i=sc.nextDouble(); t=incomeTax(i); System.out.println("Income tax amount is "+t); } static double incomeTax(double i) { double tax; if(i<=200000) tax=0; else if(i<=300000) tax=0.1*(i-200000); else if(i<=500000) tax=(0.2*(i-300000))+10000; else if(i<=1000000) tax=(0.3*(i-500000))+50000; else tax=(0.4*(i-1000000))+200000; return tax; } } |
Output:
1 2 3 |
Enter income 1300000 Income tax amount is 320000.0 |
Using Command Line Arguments
In all the above methods used to solve this problem, we have made use of the Scanner class to read input at runtime. In this method we’ll see another way to read input at runtime. This method is nothing but, making use of command line arguments.
Here, while we give the run command, after the name of the Java code to be run we’ll pass input arguments (arg[0]) with a space in between. As we require only one input i.e., income, we’ll pass only single argument.
This argument is of string type so, we’ll convert it into our desired type i.e., double by parsing it as follows:
1 |
it=Double.parseDouble(arg[0]); |
This value is stored in a variable and acts as our input. Now that the input value is read, we’ll follow the same set of instructions as given above to calculate the resultant output income tax.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Income { public static void main(String arg[]) { double tax=0,it; it=Double.parseDouble(arg[0]); System.out.println("Annual Income is : "+it); if(it<=200000) tax=0; else if(it<=300000) tax=0.1*(it-200000); else if(it<=500000) tax=(0.2*(it-300000))+(0.1*100000); else if(it<=1000000) tax=(0.3*(it-500000))+(0.2*200000)+(0.1*100000); else tax=(0.4*(it-1000000))+(0.3*500000)+(0.2*200000)+(0.1*100000); System.out.println("Income tax amount is "+tax); } } |
Output:
1 2 |
Annual Income is : 500000.0 Income tax amount is 50000.0 |