0% found this document useful (0 votes)
17 views15 pages

Java Math Functions and Examples

The document provides a comprehensive overview of various mathematical functions in Java, including methods for calculating squares, square roots, absolute values, and more. It includes questions and answers related to the usage of these functions, along with examples and explanations of their outputs. Additionally, it features Java programming exercises that demonstrate the application of these mathematical methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views15 pages

Java Math Functions and Examples

The document provides a comprehensive overview of various mathematical functions in Java, including methods for calculating squares, square roots, absolute values, and more. It includes questions and answers related to the usage of these functions, along with examples and explanations of their outputs. Additionally, it features Java programming exercises that demonstrate the application of these mathematical methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Mathematical Library Methods

Question 1

Which of the following is false to find square of a number?

1.​ [Link](a,2)
2.​ a*a
3.​ [Link](a,2) ✓
4.​ All of the above

Question 2
What type of value is returned by [Link]( )?

1.​ int
2.​ float
3.​ double ✓
4.​ All

Question 3
Which of the following syntax is true to find the square root of a number?

1.​ sqrt(a)
2.​ [Link](a) ✓
3.​ Square root(a)
4.​ None

Question 4
Name the class that is used for different Mathematical functions.

1.​ [Link] ✓
2.​ [Link]
3.​ [Link]
4.​ None

Question 5
Give the output of the [Link](x); when x = -9.99

1.​ -9.99
2.​ 9.99 ✓
3.​ 0.99
4.​ None

Question 6
Give the output of [Link](x); when x = 9.0

1.​ 3
2.​ 3.0 ✓
3.​ 3.00
4.​ all

Predict the output

Question 1
[Link]([Link](10.24));

Output

3.2

Explanation

[Link] method gives the square root of a positive number. Square


root of 10.24 is 3.2 so it is the output.
Question 2
[Link]([Link](-99.4));

Output

-99.0

Explanation

[Link] method rounds off its argument to the nearest mathematical


integer and returns its value as a double type. The nearest integer to
-99.4 is -99.0 so that is the output. [Link] method behaves in a
particular way at the mid-point i.e. when the decimal part of the
argument is 0.5. In such cases, the result is the integer value that is
even. Let's understand this with an example. [Link](1.5) and
[Link](2.5) will both return 2.0. In the case of 1.5, both 1.0 and 2.0 are
equally close to 1.5. [Link] choses the integer that is even so 2.0 is
returned. In the case of 2.5, both 2.0 and 3.0 are equally close to 2.5.
[Link] again choses the integer that is even so 2.0 is returned.

Question 3
[Link]([Link](42.875));

Output

3.5

Explanation

[Link] method returns the cube root of its argument as a double


value. Cube root of 42.875 is 3.5 so it is the output.

Question 4
[Link]([Link](-25.5, -12.5));

Output

-25.5

Explanation

[Link] method returns the smaller of its 2 arguments. As -25.5 is


smaller than -12.5 so it is the output.

Question 5
[Link]([Link](-0.95));

Output

-0.0

Explanation

[Link] method returns the smallest double value that is greater than
or equal to the argument and is equal to a mathematical integer. If the
argument value is less than zero but greater than -1.0, then the result is
negative zero which is the case in this question.

Question 6
[Link]([Link](-18.51));

Output

-19

Explanation
[Link] method rounds off its argument to the nearest mathematical
integer and returns its value as an int or long type. At the mid-point i.e.
when the decimal part of the argument is 0.5, [Link] method
rounds up to the higher integer. In this case, the nearest integer to
-18.51 is -19 so it is the output.

Question 7
[Link]([Link](-77.66, -87.45));

Output

-77.66

Explanation

[Link] method returns the greater of its 2 arguments. As -77.66 is


greater than -87.45 so it is the output.

Question 8
[Link]([Link](-0.88));

Output

-1.0

Explanation

[Link] method returns the largest double value that is less than or
equal to the argument and is equal to a mathematical integer. As -1.0 is
the largest mathematical integer less than -0.88 so it is the output.

Question 9
[Link]([Link](98.5));
Output

98.0

Explanation

[Link] method rounds off its argument to the nearest mathematical


integer and returns its value as a double type. This method behaves in a
particular way at the mid-point i.e. when the decimal part of the
argument is 0.5. In such cases, the result is the integer value that is
even. Let's understand this with an example. [Link](97.5) and
[Link](98.5) will both return 98.0. In the case of 97.5, both 97.0 and
98.0 are equally close to 97.5. [Link] choses the integer that is even
so 98.0 is returned. In the case of 98.5, both 98.0 and 99.0 are equally
close to 98.5. [Link] again choses the integer that is even so 98.0 is
returned.

Question 10
[Link]([Link](65.5));

Output

66.0

Explanation

[Link] method returns the smallest double value that is greater than
or equal to the argument and is equal to a mathematical integer. Here
66.0 is the smallest mathematical integer greater than 65.5 so it is the
output.

Write down the syntax for the following functions

Question 1
To find the smaller between two numbers p and q

Answer

[Link](p, q)

Question 2
To find the absolute value of a number m

Answer

[Link](m)

Question 3
To find the exponent of a number k

Answer

[Link](k)

Question 4
To find the square root of a number d

Answer

[Link](d)

Question 5
To find the rounded-off of a number b

Answer

[Link](b)
Predict the return data type of the following
functions

Question 1
[Link]( );

Answer

double

Question 2
[Link]( );

Answer

double

Question 3
[Link]( );

Answer

double

Question 4
[Link]( );

Answer

int or long

Question 5
[Link]( );
Answer

double

Question 6
[Link]( )

Answer

double

Explain the following functions

Question 1
[Link]( )

Answer

Returns a positive double value, greater than or equal to 0.0 and less
than 1.0.

Question 2
[Link]( )

Answer

Returns the greater of its 2 arguments. Its return type is same as the
type of its arguments.

Question 3
[Link]( )

Answer

Returns the cube root of its argument as a double value.


Question 4
[Link]( )

Answer

Returns the absolute value of its argument. Its return type is same as the
type of its arguments.

Question 5
[Link]( )

Answer

Returns the natural logarithm of its argument. Both return type and
argument is of double data type.

Distinguish between them with suitable examples

Question 1
[Link]( ) and [Link]( )

Answer

[Link]( ) [Link]( )

Returns the smallest double Returns the largest double value


value that is greater than or equal that is less than or equal to the
to the argument and is equal to a argument and is equal to a
mathematical integer mathematical integer.

double a = [Link](65.5); double b = [Link](65.5);


In this example, a will be In this example, b will be
assigned the value of 66.0 as it is assigned the value of 65.0 as it
the smallest integer greater than is the largest integer smaller
65.5. than 65.5.

Question 2
[Link]( ) and [Link]( )

Answer

[Link]( ) [Link]( )

Rounds off its argument Rounds off its argument to the nearest
to the nearest mathematical integer and returns its
mathematical integer and value as an int or long type. If argument
returns its value as a is float, return type is int, if argument is
double type. double, return type is long.

At mid-point, it returns At mid-point, it returns the higher integer.


the integer that is even

double a = [Link](1.5); long a = [Link](1.5);

double b =[Link](2.5); long b = [Link](2.5);

Both, a and b will have a a will have a value of 2 and b will have a
value of 2.0 value of 3

Solutions to Unsolved Java Programs

Question 1
Write a program in Java to input three numbers and display the greatest
and the smallest of the two numbers.​
Hint: Use [Link]( ) and [Link]( )​
Sample Input: 87, 65, 34​
Sample Output: Greatest Number 87​
Smallest number 34
import [Link];

public class KboatGreatestNumber


{
public static void main(String args[]) {

Scanner in = new Scanner([Link]);

[Link]("Enter First Number: ");


int a = [Link]();
[Link]("Enter Second Number: ");
int b = [Link]();
[Link]("Enter Third Number: ");
int c = [Link]();

int g = [Link](a, b);


g = [Link](g, c);

int s = [Link](a, b);


s = [Link](s, c);

[Link]("Greatest Number = " + g);


[Link]("Smallest Number = " + s);
}
}

Output
Question 2
Write a program in Java to calculate and display the hypotenuse of a
Right-Angled Triangle by taking perpendicular and base as inputs.​
Hint: h = √p2 + b2
import [Link];

public class KboatHypotenuse


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Perpendicular: ");
double p = [Link]();
[Link]("Enter Base: ");
double b = [Link]();

double h = [Link]([Link](p, 2) + [Link](b, 2));

[Link]("Hypotenuse = " + h);


}
}

Output

Question 3
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​
(e) Random numbers between 0 (zero) and 1 (one).
import [Link];

public class KboatMathMethods


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Number: ");
double n = [Link]();

[Link]("Natural logarithm = " +


[Link](n));
[Link]("Absolute value = " + [Link](n));
[Link]("Square root = " + [Link](n));
[Link]("Cube root= " + [Link](n));
[Link]("Random number = " + [Link]());
}
}

Output

Question 4
In an examination, you have appeared for three subjects i.e. Physics,
Chemistry and Biology. Write a program in Java to calculate the average
mark obtained and finally display the marks in rounded-off form.​
Take Physics, Chemistry. and Biology marks as inputs.
import [Link];

public class KboatAvgMarks


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Marks");
[Link]("Physics: ");
int p = [Link]();
[Link]("Chemistry: ");
int c = [Link]();
[Link]("Biology: ");
int b = [Link]();

double avg = (p + c + b) / 3.0;


long roundAvg = [Link](avg);

[Link]("Rounded Off Avg Marks = " +


roundAvg);
}
}

Output

Question 5
You want to calculate the radius of a circle by using the formula:​
Area = (22/7) * r2; where r = radius of a circle​
Hence the radius can be calculated as:​
r = √((7 * area) / 22)​
Write a program in Java to calculate and display the radius of a circle by
taking area as an input.
import [Link];

public class KboatCircleRadius


{
public static void main(String args[]) {
Scanner in = new Scanner([Link]);
[Link]("Enter Area of Circle: ");
double area = [Link]();
double r = [Link](7 * area / 22);
[Link]("Radius of Circle = " + r);
}}

Common questions

Powered by AI

Math.rint returns the nearest even integer when the argument's decimal part is 0.5, prioritizing even numbers as a tie-breaking rule. For instance, Math.rint(1.5) and Math.rint(2.5) both return 2.0 because 2 is even. In contrast, Math.round simply rounds up to the next whole number at midpoints, so Math.round(1.5) returns 2 and Math.round(2.5) returns 3 .

The Math.sqrt function in Java returns NaN (Not a Number) when given a negative number as an argument because the square root of a negative number is not defined in the realm of real numbers, which Java's Math class adheres to. This can be problematic in applications that rely on real-number calculations, as it might lead to unexpected results or exceptions if not handled properly .

To generate random numbers between 1 and 10 using Math.random, you can scale and shift the output: (int) (Math.random() * 10 + 1). This expression converts the double [0.0, 1.0) to the desired integer range. However, the limitation lies in its pseudorandomness, unsuitable for secure applications where true randomness is essential, as Math.random's deterministic algorithm could lead to predictable sequences upon repetition .

Math.ceil returns the smallest integer greater than or equal to a given number, while Math.round returns the nearest integer, rounding up at the midpoint. Math.ceil returns a double, ensuring the type remains consistent with the input, while Math.round returns an int if the input is float and a long if the input is double, reflecting its aim to provide a whole number result .

Math.random generates a positive double value between 0.0 and 1.0, offering a simple way to obtain randomized numbers. Its utility lies in its ease of use for scaling to desired ranges and its suitability for basic randomization needs in applications. However, its limitations include being less suitable for cryptographic purposes due to predictable and deterministic outputs lacking true randomness .

Using Math.floor with negative numbers results in rounding towards the more negative integer, which is critical in applications involving financial computations or resource allocations, where the rounding down might lead to underutilization or deficits. In scheduling systems, this could adversely affect resource distribution, as allocations would favor reducing capacity rather than maximizing it, potentially impacting performance negatively .

Math.exp computes the exponential function e^x, offering direct computation of exponential growth or decay processes. Its distinctive use lies in scenarios requiring compound growth calculations, such as financial models predicting exponential interest or biological models simulating population growth. These applications benefit as Math.exp avoids iterative multiplications, reducing computational overhead while maintaining precision .

Math.cbrt is preferred when performance and precision are prioritized, as it directly computes the cube root without intermediate calculations, reducing floating-point precision errors. For example, Math.cbrt(42.875) precisely returns 3.5 without the inaccuracies that might arise from using Math.pow(42.875, 1.0/3.0), which involves divisions and power calculations that are susceptible to rounding issues .

Math.min and Math.max streamline decision logic by efficiently determining boundary conditions within iterative and conditional structures. For example, in an array processing algorithm to find extreme values, these methods can replace complex conditional logic, improving readability and performance. When iterating through a dataset, Math.min and Math.max can dynamically adjust bounds to current minima and maxima, aiding in operations like clipping two datasets within common limits .

Math.abs is crucial as it provides a straightforward way to ensure that a number is non-negative, regardless of user input, which is vital in scenarios where the magnitude but not the sign of the number is important, such as distance calculations or error measurements. This ensures robustness and consistency in algorithms by preventing negative value errors during processing .

You might also like