Java Reference
In-Depth Information
18.
Casting
a variable of a type with a small range to a variable of a type with a larger
range is known as
widening a type.
19.
Casting a variable of a type with a large range to a variable of a type with a smaller
range is known as
narrowing a type.
20.
Widening a type can be performed automatically without explicit casting. Narrowing
a type must be performed explicitly.
21.
The character type
char
represents a single character.
22.
An
escape character
is a notation for representing a special character. An escape
character consists of a backslash (
\
) followed by a character or a character
sequence.
23.
The characters
' '
,
\t
,
\f
,
\r
, and
\n
are known as the whitespace characters.
24.
In computer science, midnight of January 1, 1970, is known as the
UNIX epoch.
T
EST
Q
UESTIONS
P
ROGRAMMING
E
XERCISES
Note
You can run all exercises by downloading
exercise9e.zip
from
exercise9e.zip Exercisei_j
to run Exercise
i
_
j.
For example, to run
Exercise 2.1, use
sample runs
java -cp exercise9e.zip Exercise02_01
This will give you an idea how the program runs.
Debugging TIP
The compiler usually gives a reason for a syntax error. If you don't know how to cor-
rect it, compare your program closely, character by character, with similar examples
in the text.
learn from examples
Pedagogical Note
Instructors may ask you to document your analysis and design for selected exercises.
Use your own words to analyze the problem, including the input, output, and what
needs to be computed, and describe how to solve the problem in pseudocode.
document analysis and design
Sections 2.2-2.12
2.1
(
Convert Celsius to Fahrenheit
) Write a program that reads a Celsius degree in a
double
value from the console, then converts it to Fahrenheit and displays the
result. The formula for the conversion is as follows:
fahrenheit = (9 / 5) * celsius + 32
Hint
: In Java,
9 / 5
is
1
, but
9.0 / 5
is
1.8
.
Here is a sample run: