Java Reference
In-Depth Information
(0-single filer, 1-married jointly or qualifying widow(er),
2-married separately, 3-head of household)
Enter the filing status:
Enter the taxable income:
Tax is 117683.5
0
400000
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.
To test a program, you should provide the input that covers all cases. For this program,
your input should cover all statuses (
0
,
1
,
2
,
3
). For each status, test the tax for each of the six
brackets. So, there are a total of 24 cases.
System.exit(status)
test all cases
Tip
For all programs, you should write a small amount of code and test it before moving on
to add more code. This is called
incremental development and testing
. This approach
makes testing easier, because the errors are likely in the new code you just added.
incremental development and
testing
3.17
Are the following two statements equivalent?
✓
✓
Check
Point
if
(income <=
10000
)
tax = income *
0.1
;
else if
(income <=
20000
)
tax =
1000
+
(income -
10000
) *
0.15
;
if
(income <=
10000
)
tax = income *
0.1
;
else if
(income >
10000
&&
income <=
20000
)
tax =
1000
+
(income -
10000
) *
0.15
;
The logical operators
!
,
&&
,
||
, and
^
can be used to create a compound Boolean
expression.
Key
Point
Sometimes, whether a statement is executed is determined by a combination of several condi-
tions. You can use logical operators to combine these conditions to form a compound Boolean
expression.
Logical operators
, also known as
Boolean operators
, operate on Boolean values