Java Reference
In-Depth Information
33 else if (income <= 372950 )
34 tax = 8350 * 0.10 + ( 33950 - 8350 ) * 0.15 +
35 ( 82250 - 33950 ) * 0.25 + ( 171550 - 82250 ) * 0.28 +
36 (income - 171550 ) * 0.33 ;
37 else
38 tax = 8350 * 0.10 + ( 33950 - 8350 ) * 0.15 +
39 ( 82250 - 33950 ) * 0.25 + ( 171550 - 82250 ) * 0.28 +
40 ( 372950 - 171550 ) * 0.33 + (income - 372950 ) * 0.35 ;
41 }
42
else if (status == 1 ) { // Left as an exercise
43
// Compute tax for married file jointly or qualifying widow(er)
44 }
45
else if (status == 2 ) { // Compute tax for married separately
46
// Left as an exercise
47 }
48
else if (status == 3 ) { // Compute tax for head of household
49
// Left as an exercise
50 }
51 else {
52 System.out.println( "Error: invalid status" );
53 System.exit( 1 );
54 }
55
56 // Display the result
57 System.out.println( "Tax is " + ( int )(tax * 100 ) / 100.0 );
58 }
59 }
exit program
display output
(0-single filer, 1-married jointly or qualifying widow(er),
2-married separately, 3-head of household)
Enter the filing status: 0
Enter the taxable income: 400000
Tax is 117683.5
line#
status
income
tax
output
13
0
17
400000
20
0
38
117683.5
57
Tax is 117683.5
The program receives the filing status and taxable income. The multi-way if-else state-
ments (lines 22, 42, 45, 48, 51) check the filing status and compute the tax based on the filing
status.
System.exit(status) (line 53) is defined in the System class. Invoking this method
terminates the program. The status 0 indicates that the program is terminated normally. A
nonzero status code indicates abnormal termination.
An initial value of 0 is assigned to tax (line 20). A compile error would occur if it had
no initial value, because all of the other statements that assign values to tax are within the
if statement. The compiler thinks that these statements may not be executed and therefore
reports a compile error.
System.exit(status)
 
Search WWH ::




Custom Search