Math Library
Math Library
Question 1
Answer
A user-defined method is a method defined by the user whereas library methods or built-in
methods are the methods created by the developers of Java which are available in the form of
packages.
Question 2
Answer
Math.ceil( ) Math.floor( )
Returns the smallest double value that is greater Returns the largest double value that is less
than or equal to the argument and is equal to a than or equal to the argument and is equal to a
mathematical integer mathematical integer.
Question 3
Answer
(5/10) will lead to integer division as both numerator and denominator are integers. So result of
(5/10) will be 0 instead of 0.5 and the entire expression will always result in 0.
Answer
math should be written as Math. As Java is case-sensitive so it treats math and Math
differently.
Question 4
i. Math.abs()
Answer
Returns the absolute value of its argument. Its return type is same as the type of its arguments.
For example, Math.abs(-5) will return 5.
ii. Math.sqrt()
Answer
Returns the square root of its argument as a double value. For example, Math.sqrt(25) will
return 5.0.
iii. Math.cbrt()
Answer
Returns the cube root of its argument as a double value. For example, Math.cbrt(27) will return
3.0.
iv. Math.random()
Answer
Returns a positive double value, greater than or equal to 0.0 and less than 1.0.
v. Math.round()
Answer
Rounds off its argument to the nearest mathematical integer and returns its value as an int or
long type. If argument is float, return type is int, if argument is double, return type is long. At
mid-point, it returns the higher integer. For example, Math.round(2.5) will return 3.
vi. Math.ceil()
Answer
Returns the smallest double value that is greater than or equal to the argument and is equal to a
mathematical integer. For example, Math.ceil(65.5) will return 66.0.
Question 5
Answer
Answer
Answer
Math.sqrt(b * b + 4 * a * c)
Question 6
i. x2+5y333x2+5y3
Answer
Math.cbrt(x * x + 5 * y * y * y)
ii. ∣x+y∣∣x+y∣
Answer
Math.abs(x + y)
iii. ∣x3+y2−2xy∣∣x3+y2−2xy∣
Answer
iv. π6(z4−2π)6π(z4−2π)
Answer
Answer
Math.cbrt(z*z - Math.PI)
vi. x3−y344x3−y3
Answer
vii. amount∗rate1−1(1+rate)n1−(1+rate)n1amount∗rate
Answer
viii. (−b+b2−4ac)2a2a(−b+b2−4ac)
Answer
ix. 1LC−R24C244LC1−4C2R2
Answer
Question 7
Answer
System.out.println(Math.round(14.49));
ii. Print the absolute value of -0.09
Answer
System.out.println(Math.abs(-0.09));
iii. Print the largest of -67 and -50
Answer
System.out.println(Math.max(-67, -50));
iv. Print the smallest of -56 and -57.4
Answer
System.out.println(Math.min(-56, -57.4));
v. Print a random integer between 25 and 35
Answer
int range = 35 - 25 + 1;
int num = (int)(range * Math.random() + 25);
System.out.println(num);
vi. Print 47.5 raised to the power 6.3
Answer
System.out.println(Math.pow(47.5, 6.3));
vii. Print minimum of -4, -7
Answer
System.out.println(Math.min(-4, -7));
Question 8
Write a program in Java to find the maximum of three numbers using Math.max() method.
Answer
import java.util.Scanner;
Output
Question 9
i. x to the power y
Answer
double r2 = Math.sqrt(y);
System.out.println("Square root of y = " + r2);
}
}
Output
Question 10
1x2+1y3+1z4x21+y31+z41
Answer
import java.util.Scanner;
Question 11
i. 10 to 20 (both inclusive)
ii. 25 to 50 (both inclusive)
Answer
min = 25;
max = 50;
range = max - min + 1;
num = (int)(range * Math.random() + min);
System.out.println("Random number in the range 25 to 50: " + num);
}
}
Output
Question 12
x0, x 1, x 2, x 3, x 4, x 5
Answer
import java.util.Scanner;
Output
Question 13
Write the equivalent Java statements for the following, by using the mathematical functions:
Answer
System.out.println(Math.abs(-999));
ii. Store the value -3375 in a variable and print its cube root.
Answer
int a = -3375;
System.out.println(Math.cbrt(a));
iii. Store the value 999.99 in a variable and convert it into its closest integer that is greater than
or equal to 999.99.
Answer
double a = 999.99;
System.out.println(Math.round(a));
Question 14
Write a program in Java to compute the final velocity of a vehicle using the following formula:
v2=u2+2asv2=u2+2as
where, u = initial velocity, a = acceleration and s = distance covered; they are entered by the
user.
Answer
import java.util.Scanner;
public class KboatVelocity
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter initial velocity: ");
double u = in.nextDouble();
System.out.print("Enter acceleration: ");
double a = in.nextDouble();
System.out.print("Enter distance covered: ");
double s = in.nextDouble();
double v = Math.sqrt(u * u + 2 * a * s);
System.out.println("Final Velocity = " + v);
}
}
Output