Problem:
Rewrite a program that compute taxes using arrays. For each filing status, there are six tax rates. Each rate is applied to a certain amount of taxable income. For example, from the taxable income of $400,000 for a single filer, $8,350 is taxed at 10%, (33,950 – 8,350) at 15%, (82,250 – 33,950) at 25%, (171,550 – 82,550) at 28%, (372,550 – 82,250) at 33%, and (400,000 – 372,950) at 36%. The six rates are the same for all filing statuses, which can be represented in the following array:
The brackets for each rate for all the filing statuses can be represented in a two dimensional array as follows:
int[][] brackets = {
{8350, 33950, 82250, 171550, 372950}, // Single filer
{16700, 67900, 137050, 20885, 372950}, // Married jointly
{8350, 33950, 68525, 104425, 186475}, // Married separately
{11950, 45500, 117450, 190200, 372950} // Head of household
};
Suppose the taxable income is $400,000 for single filers. The tax can be computed
as follows:
tax = brackets[0][0] * rates[0] +
(brackets[0][1] – brackets[0][0]) * rates[1] +
(brackets[0][2] – brackets[0][1]) * rates[2] +
(brackets[0][3] – brackets[0][2]) * rates[3] +
(brackets[0][4] – brackets[0][3]) * rates[4] +
(400000 – brackets[0][4]) * rates[5]
Output:
How much money do you make? 10000
Enter 0 if you are single filder,
1 if you are married jointly,
2 if you are married seperately,
and 3 if you are head of household.
0
Taxes are 1082.5
Enter 0 if you are single filder,
1 if you are married jointly,
2 if you are married seperately,
and 3 if you are head of household.
0
Taxes are 1082.5
Solution:
import java.util.Scanner; public class ComputingTax { public static void main (String[] args) { Scanner scan = new Scanner (System.in); System.out.print("How much money do you make? "); int salary = scan.nextInt(); System.out.println("Enter 0 if you are single filder,"); System.out.println("1 if you are married jointly,"); System.out.println("2 if you are married seperately,"); System.out.println("and 3 if you are head of household."); int i = scan.nextInt(); double tax = 0; int[][] brackets = { {8350, 33950, 82250, 171550, 372950}, // Single filer {16700, 67900, 137050, 20885, 372950}, // Married jointly {8350, 33950, 68525, 104425, 186475}, // Married separately {11950, 45500, 117450, 190200, 372950} // Head of household }; double[] rates = {0.10, 0.15, 0.25, 0.28, 0.33, 0.35}; if (salary > brackets[i][4]) tax = ((brackets[i][0] - 000000000000000) * rates[0]) + ((brackets[i][1] - brackets[i][0]) * rates[1]) + ((brackets[i][2] - brackets[i][1]) * rates[2]) + ((brackets[i][3] - brackets[i][2]) * rates[3]) + ((brackets[i][4] - brackets[i][3]) * rates[4])+ ((salary - brackets[i][4]) * rates[5]); else if (salary > brackets[i][3]) tax = ((brackets[i][0] - 000000000000000) * rates[0]) + ((brackets[i][1] - brackets[i][0]) * rates[1]) + ((brackets[i][2] - brackets[i][1]) * rates[2]) + ((brackets[i][3] - brackets[i][2]) * rates[3]) + ((salary - brackets[i][3]) * rates[4]); else if (salary > brackets[i][2]) tax = ((brackets[i][0] - 000000000000000) * rates[0]) + ((brackets[i][1] - brackets[i][0]) * rates[1]) + ((brackets[i][2] - brackets[i][1]) * rates[2]) + ((salary - brackets[i][2]) * rates[3]); else if (salary > brackets[i][1]) tax = ((brackets[i][0] - 000000000000000) * rates[0]) + ((brackets[i][1] - brackets[i][0]) * rates[1]) + ((salary - brackets[i][1]) * rates[2]); else if (salary > brackets[i][0]) tax = ((brackets[i][0] - 000000000000000) * rates[0]) + ((salary - brackets[i][0]) * rates[1]); System.out.println("Taxes are " + tax); } }
This is a truly good site post. Not too many people would actually, the way you just did. I am really impressed that there is so much information about this subject that have been uncovered and you’ve done your best, with so much class. If wanted to know more about green smoke reviews, than by all means come in and check our stuff. Tucson Certified Public Accountant
ReplyDelete