Java Reference
In-Depth Information
name starts with a lowercase letter. If the name contains several words, the first
letter of each word, except the first, is capitalized, e.g.
numberOfApples
.
Initializing declarations
You can combine the declaration and assignment statement
int
price;
price= 5;
into an
initializing declaration
:
int
price= 5;
1.2.1
Self-review exercises
In these exercises, you get practice with the assignment statement and using vari-
ables in expressions. DrJava provides an easy way to get this practice (by far the
easiest that we are aware of, in fact), but it can be done in other IDEs as well.
SR1.
Type in this assignment statement and expression. What does Java say?
What does that tell you about the initial value of a variable?
int
rooms;
rooms
SR2.
Type in these two declarations. What does Java say? What do you infer
from this?
int
temperature= 20;
int
temperature= temperature + 1;
SR3
. Type this declaration into Java:
int
windChill= 5;
What assignment statement will add
10
to variable
windChill
? Type the assign-
ment into Java and have Java evaluate the expression
windChill
to see whether
you were right; the new value of
windChill
should be
15
. In the same way, write
assignments (and execute and test whether they worked) to double the value of
windChill
, to set the value of
windChill
to
-10
, and to square it.
SR4
. Type this declaration and assignment into Java:
int
daysOfRain= 45;
daysOfRain= daysOfRain / 2;
and then see what value
daysOfRain
has. Now try the same assignment state-
ment again, but this time use the literal
2.0
instead of
2
:
daysOfRain= daysOfRain / 2.0;
What happens? Can you fix it by putting in a suitable type cast? Try to change
Search WWH ::
Custom Search