Mathematical Functions, Characters and Strings
Mathematical Functions, Characters and Strings
STRINGS
• The focus of this chapter is to introduce mathematical functions,
characters, string objects, and use them to develop programs.
• Java provides many useful methods in the Math class for
performing common mathematical Functions
• A method is a group of statements that performs a specific task.
You have already used the pow(a, b) method to compute ab in
Section 2.9.4, Exponent Operations and the random() method
for generating a random number in Section 3.7.
• This section introduces other useful methods in the Math class.
(Refer to page 119)
The Math Class
Class constants:
◦ PI
◦E
Class methods:
◦ Trigonometric Methods
◦ Exponent Methods
◦ Rounding Methods
◦ min, max, abs, and random Methods
2
Trigonometric Methods
sin(double a) Examples:
3
Exponent Methods
exp(double a) Examples:
Returns e raised to the power of a. Math.exp(1) returns 2.71
log(double a) Math.log(2.71) returns 1.0
Returns the natural logarithm of a. Math.pow(2, 3) returns 8.0
log10(double a) Math.pow(3, 2) returns 9.0
Returns the 10-based logarithm of a. Math.pow(3.5, 2.5) returns
22.91765
pow(double a, double b)
Math.sqrt(4) returns 2.0
Returns a raised to the power of b.
Math.sqrt(10.5) returns 3.24
sqrt(double a)
Returns the square root of a.
4
Rounding Methods
double ceil(double x)
x rounded up to its nearest integer. This integer is returned as a double value.
double floor(double x)
x is rounded down to its nearest integer. This integer is returned as a double
value.
double rint(double x)
x is rounded to its nearest integer. If x is equally close to two integers, the even
one is returned as a double.
int round(float x)
Return (int)Math.floor(x+0.5).
long round(double x)
Return (long)Math.floor(x+0.5).
5
Rounding Methods Examples
Math.ceil(2.1) returns 3.0 Math.rint(-2.1) returns -2.0
Math.ceil(2.0) returns 2.0 Math.rint(2.5) returns 2.0
Math.ceil(-2.0) returns –2.0 Math.rint(-2.5) returns -2.0
Math.ceil(-2.1) returns -2.0 Math.round(2.6f) returns 3
Math.floor(2.1) returns 2.0 Math.round(2.0) returns 2
Math.floor(2.0) returns 2.0 Math.round(-2.0f) returns -2
Math.floor(-2.0) returns –2.0 Math.round(-2.6) returns -3
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
6
min, max, and abs
max(a, b)and min(a, b) Examples:
Returns the maximum or
minimum of two parameters. Math.max(2, 3) returns 3
abs(a) Math.max(2.5, 3) returns 3.0
Returns the absolute value of Math.min(2.5, 3.6) returns 2.5
the parameter. Math.abs(-2) returns 2
random() Math.abs(-2.1) returns 2.1
Returns a random double
value in the range [0.0, 1.0).
7
The random Method
Generates a random double value greater than or equal to 0.0 and less than 1.0
(0 <= Math.random() < 1.0).
Examples:
In general,
a + Math.random() * b Returns a random number between
a and a + b, excluding a + b.
8
Formatting Output
Use the printf statement.
System.out.printf(format, items);
Where format is a string that may consist of substrings and
format specifiers. A format specifier specifies how an item
should be displayed. An item may be a numeric value,
character, boolean value, or a string. Each specifier begins
with a percent sign.
9
Examples of Specifying Width and Precision
Frequently-Used Specifiers
Specifier Output Example
%b a boolean value true or false
%c a character 'a'
%d a decimal integer 200
%f a floating-point number 45.460000
%e a number in standard scientific notation 4.556000e+01
%s a string "Java is cool"
int count = 5;
items
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set2 Set3 Set4 Set5
GuessBirthday
12
Problem: Guessing BirthDate Using Confirmation Dialogs
The program can guess your birth date. Code and Run to see
how it works.
= 19
1 3 5 7 2 3 6 7 4 5 6 7 8 9 10 11 16 17 18 19
9 11 13 15 10 11 14 15 12 13 14 15 12 13 14 15 20 21 22 23
17 19 21 23 18 19 22 23 20 21 22 23 24 25 26 27 24 25 26 27
25 27 29 31 26 27 30 31 28 29 30 31 28 29 30 31 28 29 30 31
Set1 Set2 Set3 Set4 Set5
GuessBirthdayUsingConfirmationDialog
13