Java Reference
In-Depth Information
Self-Test Exercises
44. What numbers could be generated by
randomGenerator.nextInt(5) + 10;
where
randomGenerator
is an object of type
Random
?
45. What numbers could be generated by
randomGenerator.nextDouble() * 3
+ 1;
where
randomGenerator
is an object of type
Random
?
46.
Use the method
Math.random()
to generate a random
double
that is greater
than or equal to 10.0 but less than 20.0.
Chapter Summary
•
The Java branching statements are the
if-else
statement and the
switch
statement
.
•
A
switch
statement is a multiway branching statement. You can also form multiway
branching statements by nesting
if-else
statements to form a multiway
if-else
statement.
•
Boolean expressions
are evaluated similar to the way arithmetic expressions are evalu-
ated. The value of a Boolean expression can be saved in a variable of type
boolean
.
•
The Java loop statements are the
while, do-while
, and
for
statements.
•
A
do-while
statement always iterates its loop body at least one time. Both a
while
statement and a
for
statement
might iterate its loop body zero times.
•
A
for
loop can be used to obtain the equivalent of the instruction “repeat the loop
body
n
times.”
•
Tracing variables is a good method for debugging loops.
•
An
assertion
check can be added to your Java code so that if the assertion is false, your
program halts with an error message.
•
The object
Random
can be used to generate
pseudorandom
integers or doubles.
Answers to Self-Test Exercises
1 .
if
(score > 100)
System.out.println("High")
else
System.out.println("Low");