You are on page 1of 13
Unit- VI Mathematical Library Methods Learning Scope i i th.pow(x,y),Math.sqrt(x), . is of Math cl: viz, Math.min(a,b), Math.max(a,b), Mall ) yore Nome such as Math.ceil(x), Math.floor(x), Math.round(), Math-rint(x) S Math.exp() * Trigonometrical functions * Math.random( ) Introduction . . The library methods (functions) are the in-built methods designed by the developers. “These predefined functions help the user to perform certain tasks easily and quickly which are frequently used in Java programming. In this unit, you will learn some mathematical functions which are included in a class called ‘Math’ under ‘Java.lang package. Methods of Math Class The general syntax to use any method of Math class in Java program is as follows Math. ) Let us recapitulate some methods frequently used in Java programming. @ Math.min( )~ It returns the minimum of two numbers. It returns inVlong/double type value depending upon the type of arguments passed to it. Syntax: =; For example, double m= Math.min(-4.5, -5.4); The result will be: -5.4 » @ Math.max( ) : It returns the greatest value of two given arguments. It returns int/long/double type value depending upon the type of arguments passed to it. Syntax: =: For example, double m= Math.max(9.2, 9.45); The result will be: 9.45) #Math.pow( ) This method is used to find the power raised to a given base value. It always returns a double type value. Syntax: =; For example, double m= Math.pow(2.0, 4.0); The result will be: 16.0 he : Square root of a p> Mathsart() negative number is It is used to find the square root of a positive number. It \ 4” imaginary number. returns a double type value. S_ — < 68 | Understanding Computer Applications with BlueJ—X gyntix: Return dat types; The 9 Math.cbrt() * sed to find the cube root of a iti yris use be root of a positive or a negative number, It always returns a \ n double data type. yalue i syntax: asvariable> =; for example, double m= Math.cbrt(29,79 1); The result will be: 3.1 @ Math.log() ‘The Math.log() function is used to find the natural logarithmic value of a given argument jealways returns a double type value. ° : =; For example, double m= Math.log(6.25); ‘The result will be: 1.8325 ¢ Math.abs( ) > Inalways returns the absolute value of an argument (magnitude of the number without its sign, ic. @ positive value). The return value may be int/long/double data type depending upon the input arguments Syntax: =; For example, double m= Math.abs(-12.45); The result will be: 12.45 ¢ Math.round( ) This method returns the value of part is less than 0.5 then it returns the s higher integer. It returns the value in long or int type. It output for positive and negative numbers. Syntax: =; (i) For positive numbers: For example, double m=Math.round(8.5); The result will be: 9 (ii) For negative numbers: For example, double m=Math.round(-8.5); The result will be: -8) number rounded to its nearest integer. If the fractional me integer value, otherwise it returns the next is obvious that it returns different ~# Math.rint() This function returns the nearest integer of a given fractional number. The return data ‘ype will always be double. Syntax: =; * For positive numbers: For example, double m=Math.rint(8.5); The result will be: 8.0 Revision of Class IX Syllabs ees — G i + For negative numbers: For example, double m=Math.rint(-9.5); The result will be: -10.0 Note: 4 There are many differences between Mathsound( ) and Mathint( ). One of the vital difference is working with 0.5, In this case, Math.rint( ) function actually rounds down to nearest integer, while Math.round( ) rounds up to the nearest integer. However, in all other cases, they both exhibit the same rounding rules. Moreover, Math.round( ) returns: the value in iny or Jone type whereas, return data type of Math.rint( ) is always doe. *@ Math.ceil() > It returns the next higher integer number that is greater than or equal to the argument. It always retums the value as a double data type. Syntax: =; + For positive numbers: For example, double m=Math.ceil(8.5); The result will be: 9.0 + For negative numbers: For example, double m=Math.ceil(-8.5); The result will be: -8.0 ¢@ Math.floor( ) ~ It retums the lower integer number that is less than or equal to the argument. It always retums the value as a double data type. Syntax: =; + For positive numbers: For example, double The result will be: 8.0 * For negative numbers: For example, double m The result will be: -9.0 ou exp) Itresults in the exponential value of an argument x (ie. &* Syntax: ‘Math.floor(8.912); Math floor(-8.912); 8) =; For example, double m=Mathexp(6.25); The result will be: 518.0128 ~.9/Trigonometrical Functions (There are some trigonometrical functions which are fre ). returns a double type value. find the sine, cosine and tangent values respectively are passed as an argument to the function. Some of these functions are: i. Math.sin( ) ii, Math.cos( ) iii, Math.tan( ) <> | Understanding Computer Applications with BlueJ—X. &b ~ ‘quently used in mathematics to of a given angle (in radians). Angles @ Math.random( ) > , ata type value, It returns a random number between 0 and | in a Coe oe Syntax: = ; For example, double d=Math.random( ); ana { It will return any random double value for d between 0 an A Sample Program: ; 1 To display Random Number between 0 and public class Random { A public static void main(String args[]) ( double d; . System.out printIn(’Random Number between 0 and 1: d=Math.random(); System.out printIn(d); |] Random Nusbers between O and || 0.618078069100s862 | Some facts about random function fom number between 1 and n: nt) (Math.random( )*n)+1; where n is the upper limit of the integer i) To get an integer random mu mber between m and n where m and n are the lower and Upper limits of the integer numbers respectively: (int) ((Math.random( ) *(n-m))+m Special cases: (i) When Math.random( ) gi range becomes zero (0), Gi) When Math.random( ) gives its highest value as oO. j| the range becomes 1, (iii) The random function can be used to obtain the results ‘head’ or ‘tail’ randomly when a coin is tossed. The function can be used int n=(int)(Math.random( )*2); Here, the value of n (ie, 0 or 1) so obtained can be assumed as ‘Tail’ or ‘Head’ respectively. @ To get an integer rand intr = ves its lowest value as 0.1 then the lowe imeger value of the 9 then the highest integer value of | — single example using different mathe A ‘ matical functions y/ A program to show the use of different mathematical functions public class Maths. Functions { public static void main(String args{}) .75, p=15.625; system.out printIn("The output of different mathematical functions are:"); system.out printIn("Minimum value between 6.25 & 8.75 =" +Math.min(m.n)); system.out printIn("Maximum value between 6.25 & 8.75 system.out.printin("Square root of 6.25 = system.out printin("Cube root of 15.62: =" +Math.max(m,n)); +Math.sqrt(m)); +Math.cbri(p)); system.out.printIn("6.25 raised to the power 3 =" +Math.pow(m,3)): System.out.printIn("Absolute value of (6.25 — 8.75) =" +Math,abs(m-n)); system.out.printin("The floor value of 6.25 =" + Math.floor(m)); system.out.printIn("The ceil value of 6.25 =" + Math.ceil(m)); system.out.printIn("Integral value of 6.25 =" +Math.rint(m)); System.out.printIn("Rounded value of 6.25 =" + Math.round(m)); System.out.printIn("Exponent value of 6.25 =" +Math.exp(m)); System.out.printIn("Natural log of 6.25 =" + Math.log(m)); } } Output of the program The output of different Mathematical functions are: Minimum value between 6.25 & 8.75 =6.25 Maximum value between 6.25 6 8.75 =8.75 Square root of 6.25 =2.5 Cube root of 15.625 =2.5 6.25 raised to the power 3 =244.140625 Absolute value of (6-25 - 8.75) =2.5 The floor value of 6.25 =6.0 The ceil value of 6.25 =7.0 Integral value of 6-25 =6.0 Rounded value of 6.25 =6 Pxponent value of 6.25 =518-012824660342 Natural log of 6.25 =1-8325814637483102 Revision of Class IX Syllabus | “> =n Gamma, «SOLVED PROGRAMS ON MATHEMATICAL FUNCTIONS gummy “Prog.1 Write a program in Java to input three numbers and display the greatest number. Hint: Use Math.max( ) 1 To find the greatest number import java.util.*; public class Greatest { public static void main(String args{]) { Scanner in = new Scanner(System.in); int a.b.c.d.p; System.out.printIn("Enter three numbers"); azin.nextInt(; b=in.nextInt(); c=in.nextint(); lath.max(a,b); p=Math.max(c,d); System.out.printin("Greatest number ="+p); } } Output Greatest number =92 Prog. 2 Write a program in Java to calculat length, breadth and height as input, Hint: diagonal = {P+ 62 + 72 To find the diagonal of a cuboid import java.util.*; public class Diagonal fe the diagonal of a cuboid taking “4Math.sin(x)); Math.cos(y)): -4Math.tan(z)); System.out.printIn("The value of sin 30 degree System.outprintIn("The value of cos 60 degree System.out.printIn("The value of tan 45 degre ) } Output Options The Trigonometrical values The value of sin 30 degree The value of cos 60 degree The value of tan 45 degree .5001825021996698 .4996349289865546 .0006324445845896 Write a program to input a number and evaluate the results based on the number entered by the user: (a) Natural logarithm of the number (b) Absolute value of the number (c) Square root of the number (d) Cube of the number i (c) Random numbers between 0 (zero) and 1 (one). [ICSE 2006] 1 To display the output of different mathematical functions import java.util.*; public class Results { public static void main(String args[]) { Scanner in = new Scanner(System.in); int a, System.out printin("Enter 4 number"); a=in.nextint(); System.out.printin("The output of different Mathematical functions:"); System.out.printin("Natural log of "+ a 4"="+ Math.log(a)); , System.out.printin("Absolute value of " + a +"=" +Math.abs(a)); System.outprintin("Square root of " +a+"="+Math.sqrt(a)); System.out printin("Cube of " + a +" =" +(a*a*a)); System.out.printin("Random Numbers between 0 and 1 ar Output MATHEMATICAL FUNCTIONS AT A GLANCE [rancion | oeeripin Pret ann pe | snes] U/min(ad) Returns the smaller number between a and b. 7 max(ab) —Retums the greater number between a and b. ania) Retums the square root of a positive number. fn) Retums the cube root of a number. — /pow(a.b) —-Retums the value r po Retums the absolute value (magnitude) of any number. —round(a) Returns the rounded value up to i the nearest integer. Returns the rounded value down to the nearest integer. Returns the whole number greater ~ than or equal to the number. ) Returns a random number between Oand | value of natural thm of a number. puter Applications with BlueJ—X inviong/floa! double inviong/floal/ double double double double invlong/floa double invong double double double double double double double Math.min(a,b); Math.max(a,b); Math.sqr(a); Math.cbri(a); Math.pow(a,b); Math.abs(a); Math.round(a); Math. floor(a); Math.ceil(a); Math.rint(a); Math.random( ); Math.log(a); Math.exp(a); Math sin(a); Math.cos(a); Math.tan(a); ACS qa What values will be stored in the variables p and q codes? (a) double 35; double b = 14.74 double p = Math.abs(Math.ceil(a)); double q = Math.rint(Math.max(a,b)); = 6.0. () double b = -15.6; double p = Math.rint(Math.abs(b)); ‘Ans. p= 16.0 (0 int p = Math.abs(Math.min(-61,-79)); double q = Math.cbrt(4.913); Ans. p = 79 q=l7 (double a double p double q Ans. p = 100.0 q = 56.25 (0) double x=5.9, y=6.5; double p=(Math.min(Math.floor(x),y)); double q=(Math.max(Math.ceil(x),y)); Ans. 5.0 65 Q2 What will the following functions return? (2) Math.round(Math.pow(2.4,2)); (6) Math.abs(Math.max(-8-11)); () Math.min(-23,-29); (@) Math.ceil(14.8); Ans. (2) 6 8 () -29 (@) 15.0 Q3 What will the following functions display? (a) System.out printIn(Math floor(-0.48)); (8) System.out.printIn(Math.ceil(-0.45)); (0) System.out printIn(Math.ceil(6)); (@) System.out.printIn(Math.ceil(-8)); (a) -1.0 (b) -0.0 (2) 8.0 (d) 8.0 99.51, b = -56.25; Math.abs(Math.floor(a)); Math.sqrt(Math.abs(b)); Ans, x x aftr Jaxkcuting. the gudding of) \ Sy sa HS el eS Revision of Class 1X Syllabus | 79 > 1. Multiple Choice Questions: 1. Given: double a = ~ 8:35; double p = Mathabs(Math.floor(a)); 1 value stored in the variable p? What will be the final (a) 9.0 80 (70 () 85 2. Given: double m= Math. max(Math ceil(14.55) 15 5); What will be the final value stored in the variable m? (a) 15.0 140 (©) 155 (a) 145 3. Given: d= Math. min(-15.5, ~ 19.5); What data type will you refer for the variable d? (d) None (a) int (b) float (©) double 4. Given: double k= Math.rint(- 9.4)+Mathsqrt(9.0); What will be the final value stored in the variable k? (@ -50 (b) - 60 (0 - 64 (a) -54 5, Given: double b= Math.ceil(3.4)+Math.pow(2,3); What will be the final value stored in the variable b? (@) 120 ® 10 (14 @us IL, State whether the following statements are ‘True’ or ‘False’: 1. The return data type of Math.log( ) is double. 2. Java.Math class is used for different mathematical functions. 3. The output of Math.abs(-99.99) is 100.00. 4, Math.sqrt(-225) can’t be defined. 5, The output of Math.cbrt(-3) will be -27. 6. Math.round( ) returns the value in float data type. III. Write down the syntax for the following functions: 1. To find the natural log of q. 2. To find the cube root of a number m. v. vi. Distinguish between; 1, Math.ceil( ) and Math. floor, ) 2, Math.tint( ) and Math.rouna( Unsolved Programs: 1, Write a program to calculate ‘alculate the value of the pi au f the given expression: 142,3 ae Rte Take the values of 3 cack 7 the expres of a, b and ¢ as input from the console. Finally, expression to its nearest whole numer ole, Finally, display the result of 2. For every natural number mst; 2m, met a program to input the value Sample Input: 3 Then 2m=6, m™-1=8 and m2s41=10 Thus 6, 8, 10 form a ‘Pythagorean ‘Triplet’ 3. Write a program to input a number, Calculate display the result by rounding it off. Sample Input: 5 Square root of Rounded form= 2 Cube root of 5 = 1.709759 Rounded form= 2 4. The volume of a sphere is calculated by using formula: and m?+1 form a Pythagorean triplet. Write of ‘mv’ through console to display a ‘Pythagorean Triplet’ its square root and cube root. Finally, 2.2360679 Write a program to calculate the radius of a sphere by taking its volume as an input. int: radi 3[volume Hint: radius = 4] aa 5. A trigonometrical expression is given as: tan A-tan B T+tanAetanB Write a program to calculate the value of the given expression by taking the values of angles A and B (in degrees) as input. Hint: radian= 6. The standard form of quadratic equation is represented as: ax? + bx +0 =0 where d= b?- 4ac, known as ‘Discriminant’ of the equation. Write a program to input the values of a, b and ¢, Calculate the value of discriminant and display the output to the nearest whole number. 7. The sum of first n odd natural numbers can be calculated of odd natural. For example, Sum= 1+ 3 + 5 + 7; number of odd natural = 4 Therefore, Sum= 47=16 Write a program to inp! series: (a) 1+3+5+. (14 34+5474+ 8. Write a program to input years, when the rates for as n?, where n is the number ut number of odd natural terms and display sum of the given +29 +31 447 + 49 ‘the sum, Calculate and display the compound interest in 3 the successive years are 11%, r2% and 13% respectively. 2) + 2) CI=A-P] 1 Hint: [A = Pl + FE} + ago! * 300 Revision of Class 1X Syllabus | “>

You might also like