Professional Documents
Culture Documents
cow method
The Math.pow method returns first argument (called the base NOTE
number) raised to power of second-argument (called the Please note that you need
exponent). That is, return value of Math.powx, Y)
isX. not write statement:
sanl) melhod
root of a number given as its argument.
Vath.sqrt method returns the square
Consider this code fragment:
num 36.0;
Sq_root =Math.sqrt(num) "
cb_root Math.cbrt(num)
S y s t e m . o u t . p r i n t i n ( " T h e c u b e r o o t o f" + n u m + "is " + cb_root);
4. ceill) method
The Math.ceil method returns the ceiling of a number. In arithmetic, the
ceiling of a
is the closest integer that is greater or higher than the number considered.
numhar
er
Similarly, consider a number such as -24.56. As this number is negative, it is between -24
and
-25, with-24 being greater. The ceiling of -24.56 is -24 as -24> -24.56.
Ceiling numbers of some other example numbers are shown below:
ceil(1.2) =2
2 NOTE
The Math class' methods
ceil(7.8) =8 ceil(4) = 4
are defined as
static.
ceil (-7.8) = -7 simply means that they
ceil()-4 can be used directly by
ceil(0.5) = 1
the programmer without
Now consider this code the trouble of creating an
fragment object.
double valuel 125.45;
double value2 = -14.56;
example, consider a
but closest
integer value of a number adFof
consider
tloating-
point number say 12.345. This
integer 13 and its floor is 12 because number is r12
ween integer
betwe
and
12 is the
lowest and closest 12.345.
to the integer nu
NCTIONS
AL LIBRARY F U N .
T H é A T I C AL / B A
159
number such as -24.56.
sider a n u m b e r : As this number is
consbeing greater. The
larly, c o n s i c
floor(7.8) = 7 floor(4) =4
floor(-7.8) = -8 floor() =3
floor(0.5) =0
round) method
The Math.round method returns the value of its argument rounded to the nearest whole
number. Consider the following code fragment:
double x = 4.2, y = 4.8;
long a, b;
a =
Math.round (x) ;
b Math.round(y)
System.out.println("Rounded value of " + x + " is " + a);
executes, variable a will contain 7 and variable b will contain -6 and the output
will
will COde
be like
ROunded value of 7.2 is 7
ROunded value of -5.8 is -6
1.obsdl meihod
The
Math.abs method returns the absolute value of its argument, which is
a number.
Consider the
following
8 code fragment: code
double x= -7.2, y
à = Math.abs(x)
=
7.2, a, bi
b Math.abs(y)
System..println("Absolute
out.p
oUr.println("Absolute value of "
value of" +x+" is " +
+ y+ " is " +
a);
b);
160 COMPUTER APP
After this code executes, both the variables a and b will contain the value
and the cuty
will be
Absolute value of -7.2 is 7.2
Absolute value of 7.2 is 7.2
8. max) method
The Math.max method returns the maximum of two passed arguments. The argur
nts can
of int, long, float or double types.
Consider the following code fragment:
intnum1 = 80;
intnum2 = 23
S y s t e m . o u t . p r i n t i n ( " M a x i m u m of " + num1+" and " + num2 + " is " +leth.rax(nu
9. min )method
The Math.min method returns the minimum of two passed arguments. The arguments can
of int, long,float
or double
types
Consider the following code fragment:
intnum1 = 80
intnum2 = 23;
System.out.println("Minimum of " + num1 + "and " + num2 + " is " +
Minimum of 80 and 23 is 23
Math.f endom()
System.out.printin("Generated Random Number:" + rand);
The output produced by above code is like
A,
her
MeatingN u m b e r
between a
given Range
ult, Math.random method
od 161
b y d
ant to
enerate number in anygenerates
a nu
other
a
double number
you
for min, max values of the range? In that case, in the range 0.0 to
Ask
you should do 1.0. What if,
shoulo know the
minimum andrange. To get
maximum random number in
the
following
nefine range. Next, you need to
numbers of the a
specific range, you
(i) define the range.
range =maximum minimum+1 -
range as
follows
nuumber
Generate numb
randomly. Next, to get a
maximum limit
maxin is excluded, use formula like random number in given
: range where
Math.random() * range This is the
range that you
calculated in step 2.
hae code will generate a random number
in the range. ie.
minimum <= Math.random() < maximum
double rand Math.random()* range +min ; - Do not add min for generatung
System.out.println("Generated
Random Number:"+ rand);
like :
The above code will generate output somewhat
5.22715986724367854
Generated Random Number:
If vou
EeTate random number of int type in a given range random
number of double type.
the
must typecast
method generates then you
Pleas note
lease that the Math.random other
n u m e r i c datatype,
any of
want to generate random1 example
number
code:
shown in following
generatednumber. as
-
min + 1;
to 20
generated
int range = max the
fro
omm .10 ypecust
(inttype)
to desined datarype
number
andom
/generate min);
+ rand);
range "+
t h . r a n d o m ( ) *
N u m b e r :
rand (int) R a n d o m
o u t . p r i n t i n ( " G e n e r a t e o like :
s o m e w h a t
will be
code
The output pro uced
by
above
Math.PI *r*r;
Similarly, you can Math class' methods to write some complex mathematical expressions a
shown in table below
(i) a b-4ac a
=(Math.sqrt(b*b -4*a*c))/(2*a) ;
2a
(i) ax +b
C +d*
(a Math.pow(x, 5) + b)/(c*x + Math.pow(d, 4) )
(ii)
Math.PI*Math.pow(t, 2) + Math. PI*Math.pow(r,
Similarly you can use Math class
methods as ion.
per a
specific mathematical exp
Let us now apply this
knowledge practically in the form of some
programs.