⇒ Write the output of the following:
class outputs { void get() {
System.out.println(Math.abs(Math.min(-2.83,-5.83));
System.out.println(Math.ceil(3.4)+Math.pow(2,3));
System.out.println(Math.sqrt(Math.floor(16.3)));
System.out.println(Math.round(Math.abs(-2.5)));
System.out.println(Math.cbrt(125)); }}
Answers
Basic Concept:
=> The functions involved are all part of the java.lang.Math class.
=> Math.abs() returns the absolute positive value of the value entered in it. It data type of the value returned depends on the data type of the value entered.
=> Math.min() returns the smaller value while Math.max() returns the larger value.
=>Math.ceil() returns the number rounded off to the higher value while Math.floor() rounds off to the lower value, both return in double.
=> Math.pow() raises the first value to the power of the second value and returns it as a double value.
=> Math.sqrt() returns the square root of the number entered while Math.cbrt() returns the cube root of the value entered into it. Both have return type as double.
=> Math.round() rounds of the value to the nearest integer and returns it. Its return type is int. It always rounds to the higher integer if a value such as 0.5 or 4.5 is entered into it, i.e. 1 and 5.
Working:
1) System.out.println(Math.abs(Math.min(-2.83,-5.83));
=>System.out.println(Math.abs(-5.83)); (-5.83 is the smaller value)
=> System.out.println( 5.83 ); (Absolute Value = 5.83)
OUTPUT: 5.83
2) System.out.println(Math.ceil(3.4)+Math.pow(2,3));
=> System.out.println(4.0 + 8.0); (Ceiling value of 3.4 = 4.0, 2^3 = 8.0)
=> System.out.println(12.0);
OUTPUT: 12.0
3) System.out.println(Math.sqrt(Math.floor(16.3)));
=> System.out.println(Math.sqrt(16.0)); ( Floor value of 16.3 = 16.0)
=> System.out.println(4.0);
OUTPUT: 4.0
4) System.out.println(Math.round(Math.abs(-2.5)));
=> System.out.println(Math.round(2.5)); ( Absolute value of -2.5 = 2.5)
=> System.out.println(3); (Math.round() returns in int)
OUTPUT: 3
5) System.out.println(Math.cbrt(125));
=> System.out.println(5.0); (Math.cbrt() returns cube root in double)
OUTPUT: 5.0
Final Output:
5.83
12.0
4.0
3
5.0