Page 104 - CA_Blue( J )_Class10
P. 104
6.1.12 The Math.exp( ) Method
The Math.exp( ) method returns Euler's number e raised to the power of the argument passed to it. The returned value
is the exponential value of the passed argument. It always returns the result in double data type. For example,
double d = Math.exp(2.5); // Output: 12.182493960703473
6.1.13 The Math.random( ) Method
The Math.random( ) method generates a random real number between 0.0 and 1.0. It returns a different random
number each time when a program is executed. It always returns a value of double data type. Following are some
examples of using the Math.random( ) method:
double d = Math.random(); // 0.001110128
double d = Math.random(); // 0.992883873
Some special calculations using Math.random() method:
• Using this method, we can generate a random number from 1 and x. Say, x=6:
int a = (int)(Math.random() * x) + 1;
It will return any integer number from 1 to 6.
• Using this method, we can generate a random number from a to (b-1). Say, a=4, b=8:
int i = (int)(Math.random() * (b - a)) + a;
It will return any integer number from 4 to 7.
Program 1 Let us use all the preceding methods to create a Java program.
1 class mathematicalfunction
2 {
3 public static void main()
4 {
5 System.out.println(" ----Mathematical Functions ----");
6 System.out.println("Math.min(3.4,5.8) : " + Math.min(3.4,5.8));
7 System.out.println("Math.max(3.4,5.8) : " + Math.max(3.4,5.8));
8 System.out.println("Math.sqrt(4.0) : " + Math.sqrt(4.0));
9 System.out.println("Math.cbrt(-27.0) : " + Math.cbrt(-27.0));
10 System.out.println("Math.pow(3,2) : " + Math.pow(3,2));
11 System.out.println("Math.ceil(3.4) : " + Math.ceil(3.4));
12 System.out.println("Math.floor(-5.8) : " + Math.floor(-5.8));
13 System.out.println("Math.exp(2) : " + Math.exp(2));
14 System.out.println("Math.abs(-4.4) : " + Math.abs(-4.4));
15 System.out.println("Math.log(1) : " + Math.log(1));
16 System.out.println("Math.round(3.4) : " + Math.round(3.4));
17 System.out.println("Math.rint(2.5) : " + Math.rint(2.5));
18 System.out.println("Math.random() : " + Math.random());
19 }
20 }
102102 Touchpad Computer Applications-X

