Today we are going to build an Income Tax Calculator Project in Java. Taxation systems can be complex! The amount of tax payable will vary depending on which slab your total income lies.

Look at this table to make things clear. tax data

Up to 300,000, no tax is required.

All incomes above 300,000 are subjected to a total of 2 % Education plus 1% Higher Education Cess on the total tax.

The total tax varies with the slab as per the given table.

Java code for income tax calculator (Console)

``````
public class IncomeTax2 {
static double calculateTax(double ti) { // ti -> total income
double total_tax = 0;
double total_cess = 0;
double tax1=0, tax2=0, tax3=0;
if (ti > 300000) { // slab 1
double amt = 0;
if ((ti - 300000) > 200000) // if ti > slab 1
amt = 200000;
else
amt = ti - 300000;
tax1 = (0.1 * amt); // total tax in slab 1
total_tax += tax1;
System.out.println("Tax Payable for slab 300,000-500,000: " + tax1);
}
if (ti > 500000) { // slab 2
double amt = 0;
if ((ti - 500000) > 500000) // if ti > slab 2
amt = 500000;
else
amt = ti - 500000;
tax2 = 0.2 * amt; // total tax in slab 2
total_tax += tax2;
System.out.println("Tax Payable for slab 500,000-1,000,000: " + tax2);
}
if (ti > 1000000) { // slab 3
tax3 = 0.3 * (ti - 1000000); // total tax in slab 3
total_tax += tax3;
System.out.println("Tax Payable for slab 1,000,000-above: " + tax3);
}
total_cess = 0.03 * total_tax; // all slabs have same 3% cess
System.out.println("Total tax = "+tax1+" + "+tax2+" + "+tax3+" = "+total_tax);
System.out.println("Total cess = 3% of income tax = " + total_cess);
}

public static void main(String[] args) {
System.out.println("total tax: " + calculateTax(2000000)); // calculating tax for 20,00,000
}
}
``````

OUTPUT
`Tax Payable for slab 300,000-500,000: 20000.0 Tax Payable for slab 500,000-1,000,000: 100000.0 Tax Payable for slab 1,000,000-above: 300000.0 Total tax = 20000.0 + 100000.0 + 300000.0 = 420000.0 Total cess = 3% of income tax = 12600.0 total tax: 432600.0`

At first glance, the entire code might seem overwhelming for beginners. But the overall look tells us that there are three conditional blocks to check for the three taxable slabs:

• slab 1: 300,001 – 500,000
• slab 2: 500,001 – 1,000,000
• slab 3: 1,000,001 – above.

For total income (ti) less than 300,001 no tax is levied.

In each block, we use variables tax1, tax2, and tax3 to calculate the tax amount for each slab.

If the total income lies in the 2nd or 3rd slab, we need to calculate the applicable tax for the previous slabs too. For instance, if the total income is 2,000,000, we must calculate the tax for all three slabs.

The total tax amount for 1st slab will be 20,000: 10% of 200,000 (500,000-300,000).

Now we understand, that we can simply add 20,000 with the income tax in slab 2 to obtain the total tax of an income lying in the range of slab 2 (500,000 – 1,000,000).

The above code can be reduced to fewer lines with proper conditions and an if-else block.

I have expanded the code purposefully to make the logic straightforward. Here’s the simpler version of the above code:

## Optimized Solution

``````
import java.util.Scanner;

public class IncomeTax {
static void calculateTax(double ti) { // ti -> total income
double tax=0,cess=0;
if(ti > 300000 && ti <= 500000) tax = (ti-300000)*0.1; // slab 1
else if(ti > 500000 && ti <= 1000000) tax = 200000 + (ti-500000)*0.2; // slab 1 + slab 2
else if(ti > 1000000) tax = 1200000 + (ti-1000000)*0.3; // slab 1 + slab 2 + slab 3
cess = tax * 0.03;
System.out.println("Total tax: "+tax+" Total cess: "+cess);
System.out.println("Tax payable:"+(tax+cess));
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // scanner object
System.out.print("Enter Total Income: ");
double total_income = sc.nextDouble(); // accept console input
calculateTax(total_income); // calculate tax
sc.close(); // close scanner
}
}
``````

OUTPUT
`Enter Total Income: 2000000 Total tax: 420000.0 Total cess: 12600.0 Tax payable:432600.0`

EXPLANATION

In the above code, we accept console-based user input for the total income using the Scanner class.

Then we call our `calculateTax()` method with the provided value.

In this case, 2,000,000. Check that the total tax for both programs is the same.

Therefore, as a Programmer, we shall readily jump into the latter example. But the first code will help you understand better how the taxation system works in most countries.

## Income tax calculator project in Java (GUI)

Now let’s improve our project from a console-based interaction to a GUI (Graphic User Interface). Java provides JFrame class to create GUI applications.

The javax.swing.JFrame class inherits the java.awt.Frame class.

JFrame works like the main window where swing components like labels, buttons, textfields are added to create a GUI. Let’s dive into the code.

code

``````
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class IncomeTaxGUI {
/* elements used in our GUI app */
private JFrame frame;
private JPanel panel;
private JLabel label;
private JTextField input, output;
private JButton button;

/* public constructor to configure app */
public IncomeTaxGUI() {
System.out.println("starting app...\n\n");
frame = new JFrame("Income Tax Calculator Java"); // create JFrame
panel = new JPanel(); // create JPanel
panel.setLayout(new FlowLayout()); // set panel layout

label = new JLabel("Enter Total Income"); // create a label with proper message
input = new JTextField(); // text field for user input
input.setPreferredSize(new Dimension(200, 24)); // input text field dimensions
output = new JTextField(); // text field for output (total tax)
output.setPreferredSize(new Dimension(200, 24)); // output text field dimensions
output.setEditable(false); // don't let user edit the output field
button = new JButton(); // create button to calculate tax
button.addActionListener(new ActionListener() { // on button click this action will be performed

@Override
public void actionPerformed(ActionEvent e) { // call calculate tax method and show total tax in output
// textfield
try {
double ti = Double.parseDouble(input.getText()); // total income
double tax = calculateTax(ti); // total tax
output.setText("" + tax);
} catch (NumberFormatException e1) { // handle non-numeric inputs
String errMsg = "total income must be numeric\n\n";
output.setText(errMsg);
System.err.println(errMsg);
}
}
});
button.setText("Calculate Tax"); // set message to be displayed on button

/* add all elements to the panel */

/* set frame properties */
frame.setSize(400, 200);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

public double calculateTax(double ti) { // ti -> total income
double total_tax = 0;
double total_cess = 0;
double tax1=0, tax2=0, tax3=0;
if (ti > 300000) { // slab 1
double amt = 0;
if ((ti - 300000) > 200000) // if ti > slab 1
amt = 200000;
else
amt = ti - 300000;
tax1 = (0.1 * amt); // total tax in slab 1
total_tax += tax1;
System.out.println("Tax Payable for slab 300,000-500,000: " + tax1);
}
if (ti > 500000) { // slab 2
double amt = 0;
if ((ti - 500000) > 500000) // if ti > slab 2
amt = 500000;
else
amt = ti - 500000;
tax2 = 0.2 * amt; // total tax in slab 2
total_tax += tax2;
System.out.println("Tax Payable for slab 500,000-1,000,000: " + tax2);
}
if (ti > 1000000) { // slab 3
tax3 = 0.3 * (ti - 1000000); // total tax in slab 3
total_tax += tax3;
System.out.println("Tax Payable for slab 1,000,000-above: " + tax3);
}
total_cess = 0.03 * total_tax; // all slabs have same 3% cess
System.out.println("Total tax = "+tax1+" + "+tax2+" + "+tax3+" = "+total_tax);
System.out.println("Total cess = 3% of income tax = " + total_cess);
System.out.println("total tax: " + (total_tax + total_cess));
System.out.println("\n");
}

public static void main(String s[]) throws IOException {
IncomeTaxGUI app = new IncomeTaxGUI();
}
}
``````

OUTPUT EXPLANATION

Execution starts at the main in line 105.

Here we have called the constructor of the IncomeTaxGUI class.

Now in this constructor, we have configured the application. Altogether we have used 6 components:

• private JFrame frame: Used to create the window
• private JPanel panel: Used to hold components like textfields, buttons and labels.
• private JLabel label: To display a message to the user.
• private JTextField input: For user input (total income)
• private JTextField output: To display output (total tax payable)
• private JButton button: When clicked tax is calculated

For calculating the tax we have used the same method explained at first in the Java Income Tax Calculator Project (Console).

Therefore we can also see the tax per slab printed in the console. Here we have also handled non-numeric inputs and have displayed suitable messages to the users using a simple try-catch block (see line 44).

If the entered input in the text field is not a valid number then `Double.parseDouble(input.getText())` is going to throw a NumberFormatException.

In this case, the execution in the try block will stop at line 41 and move to the catch block. Hence, we can conclude that tax laws may seem tricky but at the end of the day, they are nothing but simple if-else blocks.

Try to adjust the code according to the tax rates in your country to have a better understanding.

Have a good day!

Also, read our articles related to Java Leap Year Java Program or may want to check out our java homework help service.