Is it possible to write the following program using switch statements? If so, ca
ID: 3633773 • Letter: I
Question
Is it possible to write the following program using switch statements? If so, can someone help me out? Thanks.Here is the code:
import java.util.Scanner
public class ComputeTax
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(
"(0-single filer, 1-married jointly, " +
"2-married separately, 3-head of household) " +
"Enter the filing status: ");
int status = input.nextInt();
//Prompt user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
//Compute Tax
double tax = 0;
if (status == 0)
{
if (income <= 8350)
tax = income * 0.10;
else if (income <+ 33950)
tax = 8350 * 0.10 + (income - 8350) * 0.15;
else if (income <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(income - 33950) * 0.25;
else if (income <= 171550)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (income - 82250) * 0.28;
else if (income <= 372950)
tax = 8350 *0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(income - 171550) * 0.35;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +
(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(372950 - 171550) * 0.33 + (income - 372950) * 0.35;
}
else if (status == 1) { // left as exercise: compute tax for married filed jointly}
else if (status == 2) {//left as exercise: compute tax married filed separately}
else if (status == 3) {// left as exercise: compute tax for head of household}
else {
System.out.println("error: invalid status");
System.exit(0);
}
//Display the result
System.out.println("Tax is " + (int)(tax * 100)/ 100.0);
}
}
Explanation / Answer
please rate - thanks
only the if with specific values can be changed to a switch, not the one with a range.
you didn't provide the formulas so you'll have to do that
import java.util.Scanner;
public class ComputeTax
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print(
"(0-single filer, 1-married jointly, " +
"2-married separately, 3-head of household) " +
"Enter the filing status: ");
int status = input.nextInt();
//Prompt user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
//Compute Tax
double tax = 0;
switch(status)
{case 0: if (income <= 8350)
tax = income * 0.10;
else if (income <+ 33950)
tax = 8350 * 0.10 + (income - 8350) * 0.15;
else if (income <= 82250)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(income - 33950) * 0.25;
else if (income <= 171550)
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (income - 82250) * 0.28;
else if (income <= 372950)
tax = 8350 *0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(income - 171550) * 0.35;
else
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 +(82250 - 33950) * 0.25 + (171550 - 82250) * 0.28 +
(372950 - 171550) * 0.33 + (income - 372950) * 0.35;
break;
case 1: //left as exercise: compute tax for married filed jointly}
break;
case 2://left as exercise: compute tax married filed separately}
break;
case 3: // left as exercise: compute tax for head of household}
break;
default:
System.out.println("error: invalid status");
System.exit(0);
break;
}
//Display the result
System.out.println("Tax is " + (int)(tax * 100)/ 100.0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.