Write a program that takes as input the marital status (“single” or “married”, c
ID: 3620823 • Letter: W
Question
Write a program that takes as input the marital status (“single” or “married”, case insensitive) and the taxable income (double), and computes taxes for the following schedule. The program should use a constant (declared with the modifier final) to represent each base tax (highlighted in yellow) and tax rate (highlighted in red). Also, use format specifiers to display taxes with exactly two digits after decimal point.Hi, I'm a beginner Java user and I am having trouble with DecimalFormat and with the if else statements. I am receiving compiling errors for both and I am a little unsure how to continue on at this point. Any help or advice or suggestions would be most appreciated!
Errors involve the formulas I have put in to tax and the if(status.equals("Single"));
Here is my program:
// This program calculates the user's income tax
import java.util.Scanner;
import java.text.DecimalFormat;
public class Tax
{
public static void main(String [] args)
{
// Create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// Create a DecimalFormat object
DecimalFormat tax = new DecimalFormat("$0.00");
// Identifier declarations
double income;
int single, married;
// Marital Status: Single or Married
System.out.println("Enter your marital status (single or married): ");
int c = System.in.read();
switch ((char) c) {
case 's': case 'S':
System.out.println("Single");
break;
case 'm': case 'M':
System.out.println("Married");
break;
default:
System.out.println("illegal marital status");
break;
}
// Taxable Income
System.out.println("Enter your taxable income: ");
income = keyboard.nextDouble();
// Marital Status: Single
if(status.equals("Single"));
else if(income > 0 && income <= 8000)
tax = income*.10;
else if(income > 8000 && income <= 32000)
tax = (income - 8000)*.15 + 800;
else if(income > 32000)
tax = (income - 32000)*.25 + 4400;
else
System.out.println("-- illegal income --");
// Marital Status: Married
if(status.equals("Married"));
else if(income > 0 && income <= 16000)
tax = income*.10;
else if(income > 16000 && income <= 64000)
tax = (income - 16000)*.15 + 1600;
else if(income > 64000)
tax = (income - 64000)*.25 + 8800;
else
System.out.println("-- illegal income --");
// Display Results
System.out.println("Your income tax is" + tax.format(tax));
}
}
Explanation / Answer
// This program calculates the user's income tax import java.util.Scanner; public class Tax { // maps tax rate with maximum income // format: {[minimum income], [maximum income], [tax rate], [base tax]} // for married couples the minimum/maximum income and base tax are doubled private static final double[][] TAX_RATES = new double[][]{{0, 8000, 0.10, 0}, {8000, 32000, 0.15, 800}, {32000, Double.MAX_VALUE, 0.24, 4400}}; public static void main(String [] args) { // Create a scanner object to read from the keyboard Scanner keyboard = new Scanner(System.in); // Identifier declarations double income; int single, married; // Marital Status: Single or Married System.out.print("Enter your marital status (single or married): "); char c = keyboard.next().charAt(0); switch (c) { case 's': case 'S': married = 1; break; case 'm': case 'M': married = 2; break; default: System.out.println("illegal marital status"); return; } // marital status is assigned 1 for single or 2 for married, so that arithmetic rather than if statements can be used - more efficient // Taxable Income System.out.print("Enter your taxable income: "); income = keyboard.nextDouble(); // Marital Status: Single if(income < 0) { System.out.println("Illegal income"); return; } double tax = 0; // find income range and assign tax appropriately for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.