top of page

Java Programs for Practice - Tax Calculator

Updated: Dec 17, 2023


Java Programs for Practice

Explore a collection of Java programs for practice, designed to enhance your coding skills. From basic to advanced exercises, our practice Java programs provide hands-on experience and knowledge to help you master Java programming.

Write a example Java program for Tax Calculator that calculates income taxes for employees based on their income, tax brackets, and deductions.


Core Java Programming

Java Back-End Development


TaxCalculator.java package tax_calculator;

import java.util.Scanner;


public class TaxCalculator {

public static void main(String[] args) {

// Create a Scanner for user input

Scanner scanner = new Scanner(System.in);


// Prompt the user for income and deductions

System.out.print("Enter your annual income: $");

double income = scanner.nextDouble();

System.out.print("Enter your total deductions: $");

double deductions = scanner.nextDouble();


// Calculate taxable income

double taxableIncome = income - deductions;


// Calculate and display the income tax

double incomeTax = calculateIncomeTax(taxableIncome);

System.out.println("Your estimated income tax is: $" + incomeTax);


// Close the scanner

scanner.close();

}


// Method to calculate income tax based on taxable income

private static double calculateIncomeTax(double taxableIncome) {

double tax = 0.0;


if (taxableIncome <= 10000) {

// 10% tax rate

tax = taxableIncome * 0.1;

} else if (taxableIncome <= 40000) {

// 10% up to 10,000 and 20% on the rest

tax = 10000 * 0.1 + (taxableIncome - 10000) * 0.2;

} else if (taxableIncome <= 100000) {

// 10% on the first 10,000, 20% from 10,001 to 40,000, and 30% on the rest

tax = 10000 * 0.1 + 30000 * 0.2 + (taxableIncome - 40000) * 0.3;

} else {

// 10% up to 10,000, 20% from 10,001 to 40,000, 30% from 40,001 to 100,000, and 40% on the rest

tax = 10000 * 0.1 + 30000 * 0.2 + 60000 * 0.3 + (taxableIncome - 100000) * 0.4;

}

return tax;

}

} Explanation:

  1. We create a Scanner to obtain user input.

  2. The user is prompted to input their annual income and total deductions, which are stored in the income and deductions variables, respectively.

  3. The program calculates the taxable income by subtracting deductions from the income.

  4. The calculateIncomeTax method is used to compute the income tax based on the taxable income.

  5. Inside the calculateIncomeTax method, the program calculates the tax based on predefined tax brackets. The tax rates increase progressively for different income ranges.

  6. The calculated income tax is displayed to the user.

  7. We close the Scanner to free system resources.

This simple program calculates income tax based on a set of tax brackets and rates. Users enter their income and deductions, and the program provides an estimated income tax amount.

19 views0 comments

Comments


bottom of page