Java Reference
In-Depth Information
L ISTING 3.6 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(
10 "(0-single filer, 1-married jointly or qualifying widow(er),
11 + "\n2-married separately, 3-head of household)\n" +
12
"Enter the filing status: " );
input status
13
int status = input.nextInt();
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 ;
21
22 // 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 ;
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
if (status == 0 ) {
compute tax
else if (status == 1 ) {
// Left as 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 exercise
47 }
48
else if (status == 3 ) {
// Compute tax for head of household
49
// Left as exercise
50 }
51
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 }
else {
exit program
display output
Search WWH ::




Custom Search