Attached is Ex. 3.13 from back of the chapter exercise. Using this program, crea
ID: 3827914 • Letter: A
Question
Attached is Ex. 3.13 from back of the chapter exercise. Using this program, create a method to be called in the main method that does the tax calculation. You need to create just one method. Name this method taxCalc. taxCalc must be a value returning method of type double. It must take six parameters of type double. These parameters are income, tax level1, tax level2, tax level3, tax level4, tax level5 for each filling status. The main method should call method taxCalc for each and every status choice.
public class Exercise03_13 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt the user to enter filing status
System.out.print(
"(0-single filer, 1-married jointly or qualifying widow(er),"
+ " 2-married separately, 3-head of household) " +
"Enter the filing status: ");
int status = input.nextInt();
// Prompt the user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
// Compute tax
double tax = 0;
if (status == 0) { // Compute tax for single filers
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.33;
} 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) { // Compute tax for married file jointly
if (income <= 16700) {
tax = income * 0.10;
} else if (income <= 67900) {
tax = 16700 * 0.10 + (income - 16700) * 0.15;
} else if (income <= 137050) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (income - 67900) * 0.25;
} else if (income <= 208850) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25
+ (income - 137050) * 0.28;
} else if (income <= 372950) {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25
+ (208850 - 137050) * 0.28 + (income - 208850) * 0.33;
} else {
tax = 16700 * 0.10 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25
+ (171950 - 137050) * 0.28 + (372950 - 208850) * 0.33
+ (income - 372950) * 0.35;
}
} else if (status == 2) { // Compute tax for married separately
if (income <= 8350) {
tax = income * 0.10;
} else if (income <= 33950) {
tax = 8350 * 0.10 + (income - 8350) * 0.15;
} else if (income <= 68525) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (income - 33950) * 0.25;
} else if (income <= 104425) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25
+ (income - 68525) * 0.28;
} else if (income <= 186475) {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25
+ (104425 - 68525) * 0.28 + (income - 104425) * 0.33;
} else {
tax = 8350 * 0.10 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25
+ (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33
+ (income - 186475) * 0.35;
}
} else if (status == 3) { // Compute tax for head of household
if (income <= 11950) {
tax = income * 0.10;
} else if (income <= 45500) {
tax = 11950 * 0.10 + (income - 11950) * 0.15;
} else if (income <= 117450) {
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (income - 45500) * 0.25;
} else if (income <= 190200) {
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25
+ (income - 117450) * 0.28;
} else if (income <= 372950) {
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25
+ (190200 - 117450) * 0.28 + (income - 190200) * 0.33;
} else {
tax = 11950 * 0.10 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25
+ (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33
+ (income - 372950) * 0.35;
}
} else {
System.out.println("Error: Wrong filing status");
System.exit(1);
}
// Display the result
System.out.println("Tax is " + (int) (tax * 100) / 100.0);
}
}
Explanation / Answer
// Exercise03_13.java
public class Exercise03_13
{
public static double taxCalc(double income, double level1, double level2, double level3, double level4, double level5, int status)
{
double tax = 0;
if (status == 0)
{ // Compute tax for single filers
if (income <= level1)
{
tax = income * 0.10;
} else if (income <= level2)
{
tax = level1 * 0.10 + (income - level1) * 0.15;
} else if (income <= level3)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (income - level2) * 0.25;
} else if (income <= level4)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (income - level3) * 0.28;
} else if (income <= level5)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (income - level4) * 0.33;
} else
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (level5 - level4) * 0.33
+ (income - level5) * 0.35;
}
} else if (status == 1)
{ // Compute tax for married file jointly
if (income <= level1)
{
tax = income * 0.10;
} else if (income <= level2)
{
tax = level1 * 0.10 + (income - level1) * 0.15;
} else if (income <= level3)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (income - level2) * 0.25;
} else if (income <= level4)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (income - level3) * 0.28;
} else if (income <= level5)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (income - level4) * 0.33;
} else
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (level5 - level4) * 0.33
+ (income - level5) * 0.35;
}
}
else if (status == 2)
{ // Compute tax for married separately
if (income <= level1)
{
tax = income * 0.10;
} else if (income <= level2)
{
tax = level1 * 0.10 + (income - level1) * 0.15;
} else if (income <= level3)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (income - level2) * 0.25;
} else if (income <= level4)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (income - level3) * 0.28;
} else if (income <= level5)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (income - level4) * 0.33;
} else
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (level5 - level4) * 0.33
+ (income - level5) * 0.35;
}
} else if (status == 3)
{ // Compute tax for head of household
if (income <= level1)
{
tax = income * 0.10;
} else if (income <= level2)
{
tax = level1 * 0.10 + (income - level1) * 0.15;
} else if (income <= level3)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (income - level2) * 0.25;
} else if (income <= level4)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (income - level3) * 0.28;
} else if (income <= level5)
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (income - level4) * 0.33;
} else
{
tax = level1 * 0.10 + (level2 - level1) * 0.15 + (level3 - level2) * 0.25
+ (level4 - level3) * 0.28 + (level5 - level4) * 0.33
+ (income - level5) * 0.35;
}
}
else
{
System.out.println("Error: Wrong filing status");
System.exit(1);
}
return tax;
}
public static void main(String[] args)
{
java.util.Scanner input = new java.util.Scanner(System.in);
// Prompt the user to enter filing status
System.out.print(
"(0-single filer, 1-married jointly or qualifying widow(er),"
+ " 2-married separately, 3-head of household) " +
"Enter the filing status: ");
int status = input.nextInt();
// Prompt the user to enter taxable income
System.out.print("Enter the taxable income: ");
double income = input.nextDouble();
double level1 = 0, level2 = 0, level3 = 0, level4 = 0, level5 = 0;
if(status == 0)
{
level1 = 8350;
level2 = 33950;
level3 = 82250;
level4 = 171550;
level5 = 372950;
}
else if(status == 1)
{
level1 = 16700;
level2 = 67900;
level3 = 137050;
level4 = 208850;
level5 = 372950;
}
else if(status == 2)
{
level1 = 8350;
level2 = 33950;
level3 = 68525;
level4 = 104425;
level5 = 186475;
}
else if(status == 3)
{
level1 = 11950;
level2 = 45500;
level3 = 117450;
level4 = 190200;
level5 = 372950;
}
// Compute tax
double tax = taxCalc(income,level1, level2, level3, level4, level5, status);
// Display the result
System.out.println("Tax is " + (int) (tax * 100) / 100.0);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.