Blue Ridge Public School
Blue Ridge Public School
Standard IX
Computer Applications
Ch 6 – Math Library
1
Blue Ridge Public School
Topics
• Methods in Math Class
2
Methods in Math Class
Blue Ridge Public School
• The operators +, -, *, /, and % give us a way to
add, subtract, multiply, divide, and "mod"
values together in Java, but having to
implement some of the more sophisticated
functions of mathematics (like the square root
or sine functions) with only these operators
would be challenging
• The [Link] class contains methods for
performing basic numeric operations such as
the elementary exponential, logarithm, square
root, and trigonometric functions
• As such, the [Link] class provides us 3
access to many of these functions
Methods in Math Class – cont.
Blue Ridge Public School
• sqrt()/cbrt()
• The [Link](double a) returns the correctly rounded positive
square root of a double value
• Following is the declaration for [Link]() method
public static double sqrt(double a)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers numbers
double x = 9;
double y = 25;
// print the square root of these doubles
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
}
}
• Output
• [Link](9)=3.0 4
• [Link](25)=5.0
Methods in Math Class – cont.
Blue Ridge Public School
• pow()
• The [Link](double a, double b) returns the value of the first
argument raised to the power of the second argument
• Following is the declaration for [Link]() method
• public static double pow(double a, double b)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 2.0;
double y = 5.4;
// print x raised by y and then y raised by x
[Link]("[Link](" + x + "," + y + ")=" + [Link](x, y));
[Link]("[Link](" + y + "," + x + ")=" + [Link](y, x));
}
}
• Output 5
• [Link](2.0, 5.4)=42.22425314473263
• [Link](5.4, 2.0)=29.160000000000004
Blue Ridge Public School
Methods in Math Class – cont.
• random()
• The [Link]() returns a double value with a positive sign, greater
than or equal to 0.0 and less than 1.0
• Following is the declaration for [Link]() method
public static double random()
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two random double numbers
double x = [Link]();
double y = [Link]();
// print the numbers and print the higher one
[Link]("Random number 1:" + x);
[Link]("Random number 2:" + y);
}
}
• Output 6
• Random number 1:0.11501691809557013
• Random number 2:0.15726642068533314
Methods in Math Class – cont.
Blue Ridge Public School
• max()
• The [Link](double a, double b) returns the greater of
two double values
• Following is the declaration for [Link]() method
public static double max(double a, double b)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 60984.1;
double y = 1000;
// print the larger number between x and y
[Link]("[Link](" + x + "," + y + ")=" + [Link](x,
y));
}
} 7
• Output
• [Link](60984.1, 1000)=60984.1
Methods in Math Class – cont.
Blue Ridge Public School
• min()
• The [Link](double a, double b) returns the smaller of
two double values
• Following is the declaration for [Link]() method
public static double min(double a, double b)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 9875.875;
double y = 154.134;
// print the smaller number between x and y
[Link]("[Link](" + x + "," + y + ")=" + [Link](x,
y));
} 8
}
• Output
• [Link](9875.875, 154.134)=154.134
Methods in Math Class – cont.
Blue Ridge Public School
• abs()
• The [Link](double a) returns the absolute value of a double value
• Following is the declaration for [Link]() method
public static double abs(double a)
import [Link];
public class MathDemo {
public static void main(String[] args) {
// get some doubles to find their absolute values
double x = 4876.1874d;
double y = -0.0d;
// get and print their absolute values
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
[Link]("[Link](-9999.555d)=" + [Link](-9999.555d));
}
}
9
Output
[Link](4876.1874d)=4876.1874
[Link](-0.0d)=0.0
Methods in Math Class – cont.
Blue Ridge Public School
• ceil()
• The [Link](double a) returns the smallest (closest to negative
infinity) double value that is greater than or equal to the argument and is
equal to a mathematical integer
• Following is the declaration for [Link]() method
• public static double ceil(double a)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 125.9;
double y = 0.4873;
// call ceal for these these numbers
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
}
}
• Output 10
• [Link](125.9)=126.0
• [Link](0.4873)=1.0
Methods in Math Class – cont.
Blue Ridge Public School
• floor()
• The [Link](double a) returns the largest (closest to positive
infinity) double value that is less than or equal to the argument and is equal to a
mathematical integer
• Following is the declaration for [Link]() method
public static double floor(double a)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 60984.1;
double y = -497.99;
// call floor and print the result
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
}
}
• Output
11
• [Link](60984.1)=60984.0
• [Link](-497.99)=-498.0
Methods in Math Class – cont.
Blue Ridge Public School
• round()
• The [Link](double a) returns the closest long to the
argument. The result is rounded to an integer by adding 1/2, taking the
floor of the result, and casting the result to type long
• Following is the declaration for [Link]() method
• public static long round(double a)
import [Link].*;
public class MathDemo {
public static void main(String[] args) {
// get two double numbers
double x = 1654.9874;
double y = -9765.134;
// find the closest long for these doubles
[Link]("[Link](" + x + ")=" + [Link](x));
[Link]("[Link](" + y + ")=" + [Link](y));
}
}
• Output 12
• [Link](1654.9874)=1655
• [Link](-9765.134)=-9765
Thank You!!
13
Blue Ridge Public School