42.5k views
1 vote
Write a program that does the following in order:

1. Asks the user to enter a name
2. Asks the user to enter a number "gross income"
3. Asks the user to enter a number "state tax rate"
4. Calculates the "Federal Tax". "FICA tax" and "State tax"
5. Calculates the "estimated tax" and round the value to 2 decimal places
6. Prints values for "name", "gross income" and "estimated tax"
The program should contain three additional variables to store the Federal tax, FICA tax, State tax, gross income, and estimated tax.
Federal Tax = gross income * 9.45%
FICA Tax = gross income * 7.65%
State Tax = gross income * your state tax percent
Estimated Tax = Federal tax + FICA tax + State tax
NOTE: Percentages must be converted to decimal values, for example:
15.9%=15.9*0.01=0.159
An example of the program's input and output is shown below:
Enter your name: Belinda Patton
Enter your gross income: 53398.12
Enter your state income tax rate: 4.27
Belinda Patton's estimated tax is $11411.08 based on a gross income of $53398.12

2 Answers

1 vote

Answer:

import java.util.Scanner;

import java.math.BigDecimal;

import java.math.RoundingMode;

public class TaxCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = scanner.nextLine();

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

BigDecimal grossIncome = scanner.nextBigDecimal();

System.out.print("Enter your state tax rate (%): ");

BigDecimal stateTaxRate = scanner.nextBigDecimal();

scanner.close();

BigDecimal federalTaxRate = new BigDecimal("9.45");

BigDecimal ficaTaxRate = new BigDecimal("7.65");

BigDecimal federalTax = grossIncome.multiply(federalTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal ficaTax = grossIncome.multiply(ficaTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal stateTax = grossIncome.multiply(stateTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal estimatedTax = federalTax.add(ficaTax).add(stateTax);

estimatedTax = estimatedTax.setScale(2, RoundingMode.HALF_UP);

System.out.println("Name: " + name);

System.out.println("Gross Income: $" + grossIncome);

System.out.println("Estimated Tax: $" + estimatedTax);

Explanation: When working with financial values it is highly recommended to use Big Decimal instead of a regular float or double to ensure there are no floating point precission issues.

User Phil Ninan
by
7.7k points
3 votes

Answer:

import java.util.Scanner;

import java.math.BigDecimal;

import java.math.RoundingMode;

public class TaxCalculator {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter your name: ");

String name = scanner.nextLine();

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

BigDecimal grossIncome = scanner.nextBigDecimal();

System.out.print("Enter your state tax rate (%): ");

BigDecimal stateTaxRate = scanner.nextBigDecimal();

scanner.close();

BigDecimal federalTaxRate = new BigDecimal("9.45");

BigDecimal ficaTaxRate = new BigDecimal("7.65");

BigDecimal federalTax = grossIncome.multiply(federalTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal ficaTax = grossIncome.multiply(ficaTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal stateTax = grossIncome.multiply(stateTaxRate.divide(BigDecimal.valueOf(100)));

BigDecimal estimatedTax = federalTax.add(ficaTax).add(stateTax);

estimatedTax = estimatedTax.setScale(2, RoundingMode.HALF_UP);

System.out.println("Name: " + name);

System.out.println("Gross Income: $" + grossIncome);

System.out.println("Estimated Tax: $" + estimatedTax);

}

}

Explanation: When working with financial values it is highly recommended to use BigDecimal instead of a regular float or double to ensure there are no floating point precission issues.

User Shreyas Patil
by
7.7k points

No related questions found