Java Reference
In-Depth Information
2.9.4 Exponent Operations
The
Math.pow(a, b)
method can be used to compute
a
b
. The
pow
method is defined in
the
Math
class in the Java API. You invoke the method using the syntax
Math.pow(a, b)
(e.g.,
Math.pow(2, 3)
), which returns the result of
a
b
(2
3
). Here,
a
and
b
are parameters
for the
pow
method and the numbers
2
and
3
are actual values used to invoke the method. For
example,
Math.pow(a, b)
method
System.out.println(Math.pow(
2
,
3
));
// Displays 8.0
System.out.println(Math.pow(
4
,
0.5
));
// Displays 2.0
System.out.println(Math.pow(
2.5
,
2
));
// Displays 6.25
System.out.println(Math.pow(
2.5
,
-2
));
// Displays 0.16
Chapter 5 introduces more details on methods. For now, all you need to know is how to
invoke the
pow
method to perform the exponent operation.
2.10
✓
✓
Find the largest and smallest
byte
,
short
,
int
,
long
,
float
, and
double
. Which
of these data types requires the least amount of memory?
Check
Point
2.11
Show the result of the following remainders.
56
%
6
78
%
-4
-34
%
5
-34
%
-5
5
%
1
1
%
5
2.12
If today is Tuesday, what will be the day in 100 days?
2.13
What is the result of
25 / 4
? How would you rewrite the expression if you wished
the result to be a floating-point number?
2.14
Show the result of the following code:
System.out.println(
2
* (
5
/
2
+
5
/
2)
);
System.out.println(
2
*
5
/
2
+
2
*
5
/
2
);
System.out.println(
2
* (
5
/
2
));
System.out.println(
2
*
5
/
2
);
2.15
Are the following statements correct? If so, show the output.
System.out.println(
"25 / 4 is "
+
25
/
4
);
System.out.println(
"25 / 4.0 is "
+
25
/
4.0
);
System.out.println(
"3 * 2 / 4 is "
+
3
*
2
/
4
);
System.out.println(
"3.0 * 2 / 4 is "
+
3.0
*
2
/
4
);
2.16
Write a statement to display the result of 2
3.5
.
2.17
Suppose
m
and
r
are integers. Write a Java expression for mr
2
to obtain a floating-
point result.
A
literal
is a constant value that appears directly in a program.
Key
Point
For example,
34
and
0.305
are literals in the following statements:
literal
int
numberOfYears
=
34
;
double
weight =
0.305
;
Search WWH ::
Custom Search