Java Reference
In-Depth Information
SR4
. Write a conditional that stores
x
in
z
and
y
in
x
if
z
is greater than
0
.
SR5
. Write a conditional statement to set
z
to the minimum of
x+y
and
x-y
.
SR6
. In the following conditional statement, the else-part is written in English:
//
Set
d
to the minimum of
a
,
b
, and
c
if
(a <= b && a <= c) {
d= a;
}
else
{ //
the minimum is
b
or
c
Set
d
to the minimum of
b
and
c
}
Replace the else-part by a Java statement to accomplish that task. You will end
up with a
nested conditional statement
: a conditional statement that appears
within another conditional statement.
SR7
. In the following conditional statement, the then-part is written in English:
//
Set
d
to the maximum of
a
,
b
, and
c
if
(a<b||a<c) {
Set
d
to the maximum of
b
and
c
}
else
{ //
the maximum is
a
d= a;
}
Replace the then-part by a Java statement to accomplish that task. You will end
up with a
nested conditional statement
: a conditional statement that appears
within another conditional statement.
SR8
. Variables
b
,
c
, and
d
contain integers. Write a program segment that sets
boolean variable
t
to the value of “
b
,
c
, and
d
are the lengths of the sides of a tri-
angle”. Three integers are the lengths of the sides of some triangle if and only if
the sum of any two sides is at least the third side.
Answers to self-review exercises
For the answer to SR1, we provide class
Chapter2Exercises
and method
sr1
.
For the rest, we provide only the conditional code.
SR1
.
public class
Chapter2Exercises {
public static void
sr1(
int
x) {
if
(x < 0) {
x= x + 1;
}
}
}
SR2
. A block is a sequence of statements delimited by (enclosed in) braces
{
and
}
. It
aggregates
the sequence of statements into a single statement.
Search WWH ::
Custom Search