Programming and Problem Solving with Java

(やまだぃちぅ) #1
Summary | 197

12.Check for integer overflow. The value of an intvariable may have exceeded
Integer.MAX_VALUEin the middle of a calculation. Java doesn’t display an error
message when this problem happens.
13.Check the conditions in branching statements to confirm that the correct
branch is taken under all circumstances.
14.Design your classes to be encapsulated so they can be tested independently of
user code.

Summary


Using logical expressions is a way of asking questions while code is executing. The
computer evaluates each logical expression, producing the value trueif the
expression is true or the value falseif the expression is not true.
The ifstatement allows you to take different paths through the code based on the
value of a logical expression. The if-elsestatement is used to choose between two
courses of action; the ifstatement is used to choose whether to take a particular
course of action. The branches of an ifor if-elsecan be any statement, simple or com-
pound. They can even be another ifstatement.
Encapsulation is an approach to implementing a class that is reusable in other con-
texts. By starting with an abstraction of an object and then designing an interface that
is a general representation of the object, we create a design that is logically complete
and that hides its implementation details from the user.
The algorithm walk-through is a manual simulation of the algorithm at the design
phase. By testing our design in the problem-solving phase, we can eliminate errors
that can be more difficult to detect in the implementation phase.
An execution trace allows us to find errors once we’ve entered the implementation
phase. It’s a good idea to trace your code before you run it, so that you have some sam-
ple results against which to check the output. A written test plan is an essential part
of any application development effort.


Quick Check


1.Given that Aholds trueand B holds false, what are the values of A && B,A || B,
and !A? (pp. 161–163)
2.How does an ifstatement let us affect the flow of control? (pp. 167–169)
Free download pdf