Java Reference
In-Depth Information
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.
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 ;
3.10 Logical Operators
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
to create a new Boolean value. Table 3.3 lists the Boolean operators. Table 3.4 defines the
not ( ! ) operator, which negates true to false and false to true . Table 3.5 defines the and
( && ) operator. The and ( && ) of two Boolean operands is true if and only if both operands are
true . Table 3.6 defines the or ( || ) operator. The or ( || ) of two Boolean operands is true
if at least one of the operands is true . Table 3.7 defines the exclusive or ( ^ ) operator. The
exclusive or ( ^ ) of two Boolean operands is true if and only if the two operands have differ-
ent Boolean values. Note that p1 ^ p2 is the same as p1 != p2 .
T ABLE 3.3
Boolean Operators
Operator
Name
Description
!
not
logical negation
&&
and
logical conjunction
||
or
logical disjunction
exclusive or
logical exclusion
^
T ABLE 3.4
Truth Table for Operator !
p
!p
Example (assume age = 24, weight = 140 )
true
false
!(age > 18) is false , because (age > 18) is true .
false
true
!(weight == 150) is true , because (weight == 150)
is false .
 
 
 
Search WWH ::




Custom Search