Java Reference
In-Depth Information
Exercise 3.8
What would happen if you replaced the
&&
operator in the test with
││
so that
it reads
if((replacementValue >= 0) ││ (replacementValue < limit))
Exercise 3.9
Which of the following expressions return
true
?
! (4 < 5)
! false
(2 > 2) ││ ((4 == 4) && (1 < 0))
(2 > 2) ││ (4 == 4) && (1 < 0)
(34 != 33) && ! false
After writing your answers on paper, open the Code Pad in BlueJ and try it out. Check your
answers.
Exercise 3.10
Write an expression using boolean variables
a
and
b
that evaluates to
true
when
a
and
b
are either both
true
or both
false
.
Exercise 3.11
Write an expression using boolean variables
a
and
b
that evaluates to
true
when only one of
a
and
b
is
true
, and that is
false
if
a
and
b
are both
false
or both
true
. (This is
also called an
exclusive or
.)
Exercise 3.12
Consider the expression (
a && b
). Write an equivalent expression (one that
evaluates to
true
at exactly the same values for
a
and
b
) without using the
&&
operator.
The next method,
getDisplayValue
, also returns the display's value, but in a different for-
mat. The reason is that we want to display the value as a two-digit string. That is, if the current
time is 3:05 a.m., we want the display to read
03:05
, and not
3:5
. To enable us to do this
easily, we have implemented the
getDisplayValue
method. This method returns the current
value as a string, and it adds a leading 0 if the value is less than 10. Here is the relevant section
of the code:
if(value < 10) {
return "0" + value;
}
else {
return "" + value;
}
Note that the zero (
"0"
) is written in double quotes. Thus, we have written the
string
0, not the
integer number
0. Then the expression
"0" + value
“adds” a string and an integer (because the type of
value
is integer). The plus operator, there-
fore, represents string concatenation again, as seen in Section 2.9. Before continuing, we will
now look at string concatenation a little more closely.
Search WWH ::
Custom Search