Write a java application to calculate how much federal and state tax you need to
ID: 3622957 • Letter: W
Question
Write a java application to calculate how much federal and state tax you need to pay. The program should accomplish the following task: ask your name, yearly income, federal tax rate, state tax rate, and tax you need to pay. Your program needs to be able to catch user input errors.Sorry I can not find any of my previous written excersise on this or I would submit an example of what I have, I am very tired and foggy right now, getting over a bug that has had me layed out almost two weeks and trying to catch up on my classes. Any help will be appreciated. Is there a way to increase the Karma Awarded for answering questions?
Explanation / Answer
import java.util.Scanner;
class Tax
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Enter your first name: ");
String name = kb.next();
System.out.print("Enter your last name: ");
name += " "+kb.next();
double income, stateTaxRate, federalTaxRate;
while(true)
{
System.out.print("Enter your yearly income: ");
try
{
income = kb.nextDouble();
if(income >=0)
{
break;
}
else
{
System.out.println("Income cannot be negative.");
}
}
catch(NumberFormatException ex){}
}
while(true)
{
System.out.print("Enter your federal tax rate (in percent): ");
try
{
federalTaxRate = kb.nextDouble()/100;
if(federalTaxRate >= 0 && federalTaxRate < 1)
{
break;
}
else
{
System.out.println("Federal tax rate must be between 0% and 100%.");
}
}
catch(NumberFormatException ex){}
}
while(true)
{
System.out.print("Enter your state tax rate (in percent): ");
try
{
stateTaxRate = kb.nextDouble()/100;
if(stateTaxRate >= 0 && stateTaxRate < 1 && stateTaxRate + federalTaxRate < 1)
{
break;
}
else
{
System.out.println("State tax rate must be between 0% and 100%. Total tax cannot exceed 100%.");
}
}
catch(NumberFormatException ex){}
}
double stateTax = stateTaxRate*income;
double federalTax = federalTaxRate*income;
System.out.println(" Tax for "+name+":");
System.out.printf(" Federal tax: $%.2f ", federalTax);
System.out.printf(" State tax: $%.2f ", stateTax);
System.out.printf(" Total tax: $%.2f ", federalTax + stateTax);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.