0% found this document useful (0 votes)
3 views

Chapter 6 computer

Chapter 6 discusses the Java Math class in the java.lang package, detailing various mathematical methods including basic operations, trigonometric functions, rounding methods, and logarithmic/exponential calculations. It highlights the use of constants like Math.PI and Math.E, and provides examples of how to implement these methods in Java programs. The chapter also includes practice problems to reinforce understanding of the Math class functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Chapter 6 computer

Chapter 6 discusses the Java Math class in the java.lang package, detailing various mathematical methods including basic operations, trigonometric functions, rounding methods, and logarithmic/exponential calculations. It highlights the use of constants like Math.PI and Math.E, and provides examples of how to implement these methods in Java programs. The chapter also includes practice problems to reinforce understanding of the Math class functionalities.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Chapter 6: Mathematical Library Methods

Introduction

 Java provides a Math class in the java.lang package, which contains methods to
perform mathematical operations.
 These methods are static and can be accessed using the class name
(Math.methodName()).

Commonly Used Math Methods

1. Basic Mathematical Methods

Method Description Example


Math.abs(x) Returns the absolute value of x. Math.abs(-5) → 5
Math.max(a, b) Returns the maximum of a and b. Math.max(10, 20) → 20
Math.min(a, b) Returns the minimum of a and b. Math.min(10, 20) → 10
Math.pow(a, b) Returns a raised to the power of b. Math.pow(2, 3) → 8.0
Math.sqrt(x) Returns the square root of x. Math.sqrt(16) → 4.0

2. Trigonometric Methods

Method Description Example


Math.sin(x) Returns sine of angle x (in radians). Math.sin(Math.PI / 2) → 1.0
Math.cos(x) Returns cosine of angle x (in radians). Math.cos(0) → 1.0
Math.tan(x) Returns tangent of angle x (in radians). Math.tan(Math.PI / 4) → 1.0

 Conversion between degrees and radians:


o Math.toRadians(deg) → Converts degrees to radians.
o Math.toDegrees(rad) → Converts radians to degrees.

3. Rounding Methods

Method Description Example


Math.ceil(x) Returns the smallest integer ≥ x. Math.ceil(4.3) → 5.0
Math.floor(x) Returns the largest integer ≤ x. Math.floor(4.7) → 4.0
Math.round(x) Rounds x to the nearest integer. Math.round(4.5) → 5

4. Logarithmic and Exponential Methods


Method Description Example
Math.log(x) Returns the natural logarithm of x. Math.log(1) → 0.0
Math.log10(x) Returns the base-10 logarithm of x. Math.log10(100) → 2.0
Math.exp(x) Returns e raised to the power of x. Math.exp(1) → 2.718

Constants in the Math Class

Constant Value Description


Math.PI 3.141592653589793 Represents the value of π.
Math.E 2.718281828459045 Represents the base of natural logarithms.

Key Points

1. The return type of most Math methods is double.


2. For integer-only results, cast the output to int where required.
3. Some methods expect input in radians (not degrees). Use Math.toRadians() for
conversion.

Examples

Example 1: Using Math.pow() and Math.sqrt()

public class MathExample {


public static void main(String[] args) {
double result = Math.pow(3, 2); // 3^2 = 9.0
double squareRoot = Math.sqrt(result); // √9 = 3.0
System.out.println("Result: " + result);
System.out.println("Square Root: " + squareRoot);
}
}

Example 2: Using Math.max() and Math.abs()

public class MathExample2 {


public static void main(String[] args) {
int a = -25, b = 40;
int max = Math.max(a, b); // Max of -25 and 40 = 40
int absValue = Math.abs(a); // Absolute value of -25 = 25
System.out.println("Max: " + max);
System.out.println("Absolute Value: " + absValue);
}
}

Practice Problems

1. Write a program to calculate the area of a circle using Math.PI and Math.pow().
2. Find the larger of two given numbers using Math.max().
3. Write a program to convert an angle from degrees to radians and calculate its sine
value.
4. Calculate the cube root of a number using Math.pow().

You might also like