Java Reference
In-Depth Information
SR 3.17
Passing a positive integer parameter
x
to the
nextInt
method of a
Random
object returns a random number in the range of 0 to
x
−1. So a
call to
nextInt(20)
will return a random number in the range 0 to 19,
inclusive.
SR 3.18
The ranges of the expressions are:
a. From 0 to 49 b. From 10 to 14 c. From 5 to 14
d. From -25 to 24
SR 3.19
The expressions to generate the given ranges are:
a.
generator.nextInt (31);
// range is 0 to 30
b.
generator.nextInt (10) + 10;
// range is 10 to 19
c.
generator.nextInt (11) - 5;
// range is -5 to 5
3.5
The
Math
Class
SR 3.20
A class or static method can be invoked through the name of the class
that contains it, such as
Math.abs
. If a method is not static, it can be
executed only through an instance (an object) of the class.
SR 3.21
The values of the expressions are:
a. 20 b. 16.0 c. 16.0 d. 243.0 e. 125.0 f. 4.0
SR 3.22
The following statement prints the sine of an angle measuring 1.23
radians:
System.out.println (Math.sin(1.23));
SR 3.23
The following declaration creates a
double
variable and initializes it to
5 raised to the power 2.5:
double
result = Math.pow(5, 2.5);
SR 3.24
Examples of methods that are not listed in Figure 3.5 include:
static int
min(
int
a,
int
b)
static float
max(
long
a,
long
b)
static long
round(
double
a)
3.6
Formatting Output
SR 3.25
To obtain a
NumberFormat
object for use within a program, you request
an object using one of the static methods provided by the
NumberFormat
class. The method you invoke depends upon your intended use of the
Search WWH ::
Custom Search