[go: up one dir, main page]

0% found this document useful (0 votes)
1 views5 pages

Chapter 7

This document covers mathematical library methods in Java, detailing user-defined versus library methods, and explaining various Math functions such as Math.abs(), Math.sqrt(), and Math.random(). It includes assignment questions with answers, Java expressions, and sample programs demonstrating the use of these methods for calculations and generating random numbers. The document serves as a guide for understanding and applying mathematical operations in Java programming.

Uploaded by

Saradha S
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)
1 views5 pages

Chapter 7

This document covers mathematical library methods in Java, detailing user-defined versus library methods, and explaining various Math functions such as Math.abs(), Math.sqrt(), and Math.random(). It includes assignment questions with answers, Java expressions, and sample programs demonstrating the use of these methods for calculations and generating random numbers. The document serves as a guide for understanding and applying mathematical operations in Java programming.

Uploaded by

Saradha S
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
You are on page 1/ 5

CHAPTER 7

Mathematical Library Methods


Section 3: Assignment Questions

1. How do user-defined methods differ from library methods?


Ans. The methods defined by the user are called user-defined methods. These methods are defined as
per the need of the user to create customized and reusable blocks of code.Whereas, the library methods
or built-in methods are defined by the developers of Java. They are also known as system-defined
methods. For example, Math.sqrt() and Math.round().

2. Distinguish between Math.ceil() and Math.floor() methods.


Ans. Math.ceil() - Returns the smallest integer that is greater than or equal to the argument.
Math.floor() - Returns the largest integer that is less than or equal to the argument.

3. What is wrong with the following statements? Explain.


i. result = (5/10) * Math.sqrt( a );
Ans. For different values of ‘a’ the value displayed for result will be 0.0 every time as (5/10) will return 0
always.

ii. result = math.sqrt(b * b – 4 * a * c) / ( 2 * a );


Ans. math.sqrt has to be written as Math.sqrt()

4. Explain the following Math functions in Java:

i. Math.abs() Ans. Returns the absolute value of the argument.

ii. Math.sqrt() Ans. Returns the square root value of the argument ( √ ).

iii. Math.cbrt() Ans. Returns the cube root value of the argument ( √ ).

iv. Math.random() Ans. Returns a double value greater than or equal to 0.0 and less than 1.0.

v. Math.round() Ans. Returns the closest int or long (as per the argument). If the argument type is
double, the return type is long. If the argument type is float, the return type is int. www.bhuvantechs.com

vi. Math.ceil() Ans. Returns the smallest integer that is greater than or equal to the argument.

5. Write Java expressions for the following:

i. The square root of a + 5b3 Ans. Math.sqrt(a+5*Math.pow(b,3))

ii. The cube root of x3 + y3 + z3 Ans. Math.cbrt(Math.pow(x,3)+Math.pow(y,3)+Math.pow(z,3))

iii. The square root of b2 + 4ac Ans. Math.sqrt(Math.pow(b,2)+4*a*c)


6. Write the following as Java expressions: .

i. 2 + 5∗ 3 Ans. Math.cbrt(Math.pow(x,2)+5*Math.pow(y,3))

ii. |x + y| Ans. Math.abs(x+y)

iii. |x3 + y2 − 2xy| Ans. Math.abs(Math.pow(x,3)+Math.pow(y,2)-2*x*y)

iv. π/6(z4 - 2π) Ans. Math.PI/6(Math.pow(z,4)-2*Math.PI)

v. √ − Ans. Math.cbrt(Math.pow(z,2)-Math.PI)

vi. − Ans. Math.sqrt(Math.sqrt((Math.pow(x,3)-Math.pow(y,3))

vii. ((amount*rate)/1-(1/(1+rate)n)) Ans. ((amount*rate)/(1-(1/Math.pow((1+rate),n))))

viii. – + √ −4 /2 Ans. ((-b+Math.sqrt(Math.pow(b,2)-4*a*c))/2*a)

ix. (1/ )−( /4 ) Ans. Math.sqrt(Math.sqrt((1/L*C)-(Math.pow(R, 2)/4*Math.pow(C,2))))

7. Write valid statements for the following in Java:

i. Print the rounded off value of 14.49 Ans. Math.round(14.49)


ii. Print the absolute value of -0.09 Ans. Math.abs(-0.09)
iii. Print the largest of -67 and -50 Ans. Math.max(-67,-50)
iv. Print the smallest of -56 and -57.4 Ans. Math.min(-56,-57.4)
v. Print a random integer between 25 and 35
Ans. int min=25,max=35; int range = max-min+1;
int num1=(int)(range*Math.random() + min);
System.out.println("Random number is " + num1);
vi. Print 47.5 raised to the power 6.3 Ans. Math.pow(47.5,6.3)
vii. Print minimum of -4, -7 Ans. Math.min(-4,-7)

8. Write a program in Java to find the maximum of three numbers using Math.max() method.
Ans.
import java.util.Scanner;
www.bhuvantechs.com

public class Findmin


{
public static void main(String[] args)
{
double num1=9,num2=13,num3=10;
System.out.println("Maximum number is " + Math.max(num3,Math.min(num1,num2)));
}
}

Mathematical Library Methods ~2~


9. Write a program to print: The values of x and y are 81 and 3, respectively.
i. x to the power y
Ans.
import java.util.Scanner;
public class FindPower
{
public static void main(String[] args)
{
double x=81,y=3;
System.out.println("power(x,y) is " + Math.pow(x,y));
}
}

ii. The square root of y


Ans.
import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
double y=3;
System.out.println("Square root of "+y+ " is " + Math.sqrt(y));
}
}

10. Write a program to compute and display the value of expression: 1/x2+1/y3+1/z4 where, the values
of x, y and z are entered by the user.
Ans.
import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter value of x: ");
int x = in.nextInt();
System.out.print("Enter value of y: ");
int y = in.nextInt();
www.bhuvantechs.com

System.out.print("Enter value of z: ");


int z = in.nextInt();
double f;
f = ((1/Math.pow(x,2))+(1/Math.pow(y,3))+(1/Math.pow(z,4)));
System.out.println("Result"+f);
}
}

Mathematical Library Methods ~3~


11. Write a program to generate random integers in the following ranges: i. 10 to 20 (both inclusive).
Ans. import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
int min=10,max=20;
int range = max-min+1;
int num1=(int)(range*Math.random() + min);
int num2=(int)(range*Math.random() + min);
System.out.println("First number is " + num1);
System.out.println("Second number is " + num2);
}
}

ii. 25 to 50 (both inclusive)


Ans. import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
int min=25,max=50;
int range = max-min+1;
int num1=(int)(range*Math.random() + min);
int num2=(int)(range*Math.random() + min);
System.out.println("First number is " + num1);
System.out.println("Second number is " + num2);
}
}

12. Write a program that accepts a number x and then prints: x0, x1, x2, x3, x4, x5
Ans. import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter value of x: ");
int x = in.nextInt();
www.bhuvantechs.com

int i=0;
double tot=0;
while (i<=5) {
tot=Math.pow(x,i);
System.out.println(" "+tot);
i=i+1;
}
}
}

Mathematical Library Methods ~4~


13. Write the equivalent Java statements for the following, by using the mathematical functions:
Ans.
i. Print the positive value of -999.
Ans. Math.abs(-999)

ii. Store the value -3375 in a variable and print its cube root.
Ans. double min=-3375;
System.out.println(Math.cbrt(min));

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.
Ans. double min=999.99;
System.out.println(Math.ceil(min));

14. Write a program in Java to compute the final velocity of a vehicle using the following formula:
V2 = u2 + 2as where, u = initial velocity, a = acceleration and s = distance covered; they are entered by
the user.
Ans.
import java.util.Scanner;
public class FindSquareroot
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter initial velocity: ");
double u = in.nextInt();
System.out.print("Enter acceleration: ");
double a = in.nextInt();
System.out.print("Enter distance: ");
double s = in.nextInt();
double v=0;
v=Math.pow(u,2)+2*a*s;
System.out.println("Final Velocity "+Math.pow(v,2));
}
}

www.bhuvantechs.com

Mathematical Library Methods ~5~

You might also like