Java Reference
In-Depth Information
• On line 12, the variable
count
is no longer defined (its scope is limited to the
for
loop). It should be declared before the loop begins rather than inside the
loop's header.
• On line 12, too large a value is printed for the final odd number;
count
should
be printed, not
count + 2
.
• On line 20, it is illegal to try to assign a new value to a constant such as
MAX_ODD
. One way to fix this would be to write two methods: one to print the
odds up to 21 and a second to print the odds up to 11. (Admittedly, this solution
is redundant. A better solution to this kind of problem involves parameter pass-
ing, which will be demonstrated in later chapters.)
•
The last
print
statement in
main
should be a
println
statement.
27.
The result is: 55
28.
(a)
2 * line + (2 * SIZE)
(b)
4 * line + (3 * SIZE)
(c)
-line + (2 * SIZE + 3)
29.
line
\
!
/
1
0
22
0
2
2
18
2
3
4
14
4
4
6
10
6
5
8
6
8
6
10
2
10
•
expression for
\
and
/
:
2 * line - 2
•
expression for
!
:
-4 * line + 26
30.
Line
\
!
/
1
0
14
0
2
2
10
2
3
4
6
4
4
6
2
6
•
expression for
\
and
/
:
2 * line - 2
•
expression for
!
:
-4 * line + 18
•
generalized for constant:
-4 * line + (4 * SIZE + 2)
Chapter 3
1.
1 3 5
1 3 5 7 9 11 13 15
1 3 5 7 9 11 13 15 17 19 21 23 25
2.
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4
number = 8
Search WWH ::
Custom Search