Java Reference
In-Depth Information
u = 12.5;
//Line 2
v = 3.0;
//Line 3
System.out.println("Line 4: " + u + " to "
+ "the power of " + v
+ " = " + Math.pow(u, v));
//Line 4
System.out.println("Line 5: Square root of "
+ "42.25 = "
+ Math.sqrt(42.25));
//Line 5
u = Math.pow(8.5, 2.0);
//Line 6
System.out.println("Line 7: u = " + u);
//Line 7
}
}
Sample Run:
Line 1: 2 to the power of 6 = 64.0
Line 4: 12.5 to the power of 3.0 = 1953.125
Line 5: Square root of 42.25 = 6.5
Line 7: u = 72.25
The preceding program works as follows. The statement in Line 1 uses the function
pow
to determine and output
2
6
. The statement in Line 2 sets
u
to
12.5
, and the statement in
Line 3 sets
v
to
3.0
. The statement in Line 4 determines and outputs
u
v
. The statement
in Line 5 uses the method
sqrt
, of the
class
Math
, to determine and output the square
root of
42.25
. The statement in Line 6 determines and assigns
8.5
2
to
u
. The statement
in Line 7 outputs the value of
u
.
A Precaution
In Chapter 2, you learned how to use the method
nextInt
of the
class
Scanner
to
input the next token, which can be expressed as an integer. In the preceding section, you
learned how to use the method
pow
of the
class
Math
.
Consider the following statement:
x = console.nextInt();
where
x
is a variable of type
int
. Notice the dot between
console
and
nextInt
; the name
of the object
console
and the name of the method
nextInt
are separated by the dot.
In Java, the dot (
.
) is an operator called the member access operator.
Omitting the dot between
console
and
nextInt
results in a syntax error. In the
statement:
x = consolenextInt();
Search WWH ::
Custom Search