Java Reference
In-Depth Information
Your program computes the tax for the taxable income based on the filing status. The filing
status can be determined using if statements outlined as follows:
if (status == 0 ) {
// Compute tax for single filers
}
else if (status == 1 ) {
// Compute tax for married filing jointly or qualifying widow(er)
}
else if (status == 2 ) {
// Compute tax for married filing separately
}
else if (status == 3 ) {
// Compute tax for head of household
}
else {
// Display wrong status
}
For each filing status there are six tax rates. Each rate is applied to a certain amount of
taxable income. For example, of a taxable income of $400,000 for single filers, $8,350 is
taxed at 10%, (33,950 - 8,350) at 15%, (82,250 - 33,950) at 25%, (171,550 - 82,250) at 28%,
(372,950 - 171,550) at 33%, and (400,000 - 372,950) at 35%.
Listing 3.5 gives the solution for computing taxes for single filers. The complete solution
is left as an exercise.
L ISTING 3.5
ComputeTax.java
1 import java.util.Scanner;
2
3 public class ComputeTax {
4 public static void main(String[] args) {
5 // Create a Scanner
6 Scanner input = new Scanner(System.in);
7
8 // Prompt the user to enter filing status
9 System.out.print( "(0-single filer, 1-married jointly or " +
10
"qualifying widow(er), 2-married separately, 3-head of " +
11
"household) Enter the filing status: " );
12
13
int status = input.nextInt();
input status
14
15 // Prompt the user to enter taxable income
16 System.out.print( "Enter the taxable income: " );
17
double income = input.nextDouble();
input income
18
19
// Compute tax
20
double tax = 0 ;
compute tax
21
22 if (status == 0 ) { // Compute tax for single filers
23 if (income <= 8350 )
24 tax = income * 0.10 ;
25 else if (income <= 33950 )
26 tax = 8350 * 0.10 + (income - 8350 ) * 0.15 ;
27 else if (income <= 82250 )
28 tax = 8350 * 0.10 + ( 33950 - 8350 ) * 0.15 +
29 (income - 33950 ) * 0.25 ;
30 else if (income <= 171550 )
31 tax = 8350 * 0.10 + ( 33950 - 8350 ) * 0.15 +
32 ( 82250 - 33950 ) * 0.25 + (income - 82250 ) * 0.28 ;
 
Search WWH ::




Custom Search